Linux中的权限修改指令及正则表达式

1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限

[root@centos6 ~]# cp -r /etc/skel/ /home/tuser1
[root@centos6 ~]# ls -la /home/tuser1
total 20
drwxr-xr-x. 2 root root 4096 Oct 23 18:20 .
drwxr-xr-x. 5 root root 4096 Oct 23 18:20 ..
-rw-r--r--. 1 root root   18 Oct 23 18:20 .bash_logout
-rw-r--r--. 1 root root  176 Oct 23 18:20 .bash_profile
-rw-r--r--. 1 root root  124 Oct 23 18:20 .bashrc
[root@centos6 ~]# chmod -R g-r,o-r /home/tuser1
[root@centos6 ~]# ls -la /home/tuser1/
total 20
d-wx--x--x. 2 root root 4096 Oct 23 18:20 .
drwxr-xr-x. 5 root root 4096 Oct 23 18:20 ..
-rw-------. 1 root root   18 Oct 23 18:20 .bash_logout
-rw-------. 1 root root  176 Oct 23 18:20 .bash_profile
-rw-------. 1 root root  124 Oct 23 18:20 .bashrc

2、编辑/etc/group文件,添加组hadoop

[root@centos6 ~]# vim /etc/group
 root:x:0:
 bin:x:1: bin,daemon
 daemon:x:2:bin,daemon
 sys:x:3:bin,adm
 adm:x:4:adm,daemon
 tty:x:5:
 disk:x:6:
 lp:x:7:daemon
 mem:x:8:
...

编辑模式下输入G

...
oprofile:x:16:
slocate:x:21:
mariadb:x:498:
mageedu:x:5000:
fedora:x:501:

输入o,在光标所在行的下方新增一行,进入输入模式

...
fedora:x:501:

-- INSERT --

输入Hadoop:x:502:后,按esc退出至编辑模式,在编辑模式下输入:进入末行模式,键入wq,保存退出。

fedora:x:501:
hadoop:x:502:
:wq

3、手动编辑/etc/passwd文件新增一行,添加用户Hadoop,其基本组ID为Hadoop组的ID号,其家目录为/home/hadoop。

[root@centos6 ~]# vim /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
...

在编辑模式下键入G,将光标移至最后行,键入o后在当前行下方新增一行,并进入输入模式

mariadb:x:498:498::/home/mariadb:/sbin/nologin
fedora:x:501:501::/users/fedora:/bin/bash

-- INSERT --

输入hadoop:x:502:502::/home/hadoop:/bin/bash

...
fedora:x:501:501::/users/fedora:/bin/bash
hadoop:x:502:502::/home/hadoop:/bin/bash

4、复制/etc/skel目录为/home/hadoop,要求修改Hadoop目录的属组和其它用户没有任何访问权限。

root@centos6 ~]# cp -r /etc/skel/ /home/hadoop/
[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop  502 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root 4096 Oct 23 18:20 ..
-rw-r--r--. 1 hadoop  502   18 Jul 18  2013 .bash_logout
-rw-r--r--. 1 hadoop  502  176 Jul 18  2013 .bash_profile
-rw-r--r--. 1 hadoop  502  124 Jul 18  2013 .bashrc
drwxr-xr-x. 2 root   root 4096 Oct 23 23:43 skel
[root@centos6 ~]# chmod -R g-r,o-r /home/hadoop/
[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop  502 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root 4096 Oct 23 18:20 ..
-rw-------. 1 hadoop  502   18 Jul 18  2013 .bash_logout
-rw-------. 1 hadoop  502  176 Jul 18  2013 .bash_profile
-rw-------. 1 hadoop  502  124 Jul 18  2013 .bashrc
drwx--x--x. 2 root   root 4096 Oct 23 23:43 skel

5、修改/home/hadoop目录及其内部所有文件的属主为hadoop,属组为hadoop

[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop  502 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root 4096 Oct 23 18:20 ..
-rw-------. 1 hadoop  502   18 Jul 18  2013 .bash_logout
-rw-------. 1 hadoop  502  176 Jul 18  2013 .bash_profile
-rw-------. 1 hadoop  502  124 Jul 18  2013 .bashrc
drwx--x--x. 2 root   root 4096 Oct 23 23:43 skel
[root@centos6 ~]# chown -R hadoop:hadoop /home/hadoop/
[root@centos6 ~]# ls -la /home/hadoop/
total 24
drwx------. 3 hadoop hadoop 4096 Oct 23 23:43 .
drwxr-xr-x. 5 root   root   4096 Oct 23 18:20 ..
-rw-------. 1 hadoop hadoop   18 Jul 18  2013 .bash_logout
-rw-------. 1 hadoop hadoop  176 Jul 18  2013 .bash_profile
-rw-------. 1 hadoop hadoop  124 Jul 18  2013 .bashrc
drwx--x--x. 2 hadoop hadoop 4096 Oct 23 23:43 skel

6、显示/proc/meminfo文件中以大写或小写S开头的行,用两种方式。

[root@centos6 ~]# cat /proc/meminfo | grep "^[S,s]"
SwapCached:            0 kB
SwapTotal:       2031608 kB
SwapFree:        2031608 kB
Shmem:               232 kB
Slab:              80628 kB
SReclaimable:      26364 kB
SUnreclaim:        54264 kB
[root@centos6 ~]# cat /proc/meminfo | grep -e "^[s|S]"
SwapCached:            0 kB
SwapTotal:       2031608 kB
SwapFree:        2031608 kB
Shmem:               232 kB
Slab:              80636 kB
SReclaimable:      26364 kB
SUnreclaim:        54272 kB
[root@centos6 ~]# cat /proc/meminfo | grep -i "^S"
SwapCached:            0 kB
SwapTotal:       2031608 kB
SwapFree:        2031608 kB
Shmem:               232 kB
Slab:              80624 kB
SReclaimable:      26364 kB
SUnreclaim:        54260 kB

7、显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户

[root@centos6 ~]# cat /etc/passwd | grep -v "/sbin/nologin$" |cut -d: -f1
root
sync
shutdown
halt
fedora
hadoop

8、显示/etc/passwd文件中其默认shell为/bin/bash的用户

[root@centos6 ~]# cat /etc/passwd | grep "/bin/bash$"|cut -d: -f1
root
fedora
hadoop

9、找出/etc/passwd文件中的一位数或两位数

[root@centos6 ~]# cat /etc/passwd | grep -o "\<[[:digit:]][[:digit:]]\?\>"
0
0
1
1
2
2
...

10、显示/boot/grub/grub.conf中以至少一个空白字符开头的行

[root@centos6 ~]# cat /boot/grub/grub.conf | grep "^[[:space:]]\+"
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_centos6-lv_root rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos6/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos6/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.el6.x86_64.img

11、显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后面又有至少一个非空白字符的行

[root@centos6 ~]# cat /etc/rc.d/rc.sysinit | grep "^#[[:space:]]\+[^[:space:]]\+"
# /etc/rc.d/rc.sysinit - run once at boot time
# Taken in part from Miquel van Smoorenburg's bcheckrc.
# Check SELinux status
# Print a text banner.
# Only read this once.
# Initialize hardware
# Set default affinity
# Load other user-defined modules
# Load modules (for backward compatibility with VARs)
# Configure kernel parameters
...

12、打出netstat -tan命令执行结构中以‘LISTEN’,后面跟空白字符结尾的的行

[root@centos6 ~]# netstat -tan | grep "LISTEN[[:space:]]\+$"
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:56339               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 :::111                      :::*                        LISTEN      
tcp        0      0 :::22                       :::*                        LISTEN      
tcp        0      0 ::1:631                     :::*                        LISTEN      
tcp        0      0 :::38177                    :::*                        LISTEN

13、添加用户bash,testbash,basher,nologin(此一个用户的shell为/sbin/nologin),而后找出当前系统上其用户名和默认shell相同的用户的信息

[root@centos6 ~]# cat /etc/passwd | grep "^\(\<[[:alnum:]]\+\>\).*\1$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:503:503::/home/bash:/bin/bash
nologin:x:506:506::/home/nologin:/sbin/nologin

原创文章,作者:lucklyme,如若转载,请注明出处:http://www.178linux.com/53590

(0)
lucklymelucklyme
上一篇 2016-10-24
下一篇 2016-10-24

相关推荐

  • 千万不要把 bool 设计成函数参数

    我们有很多Coding Style 或 代码规范。但这一条可能会经常被我们所遗忘,就是我们经常会在函数的参数里使用bool参数,这会大大地降低代码的可读性。不信?我们先来看看下面的代码。 当你读到下面的代码,你会觉得这个代码是什么意思? widget->repaint(false); 是不要repaint吗?还是别的什么意思?看了文档后,我们才知道这个…

    Linux干货 2016-07-10
  • vsftpd

    练习:完成vsftpd配置 (1) 禁锢系统用户于家目录 [root@node3 ~]# yum -y install vsftpd [root@node3 ~]# vim /etc/vsftpd/vsftpd.conf chroot_local_user=YES &n…

    Linux干货 2016-10-16
  • linux基础学习-第九天(shell基础)

    2016-08-10 授课内容: shell脚本基础: 变量 运算 bash测试(数字测试、字符测试、文件测试、组合测试) read命令 变量作用: 1、数据存储格式 2、参与的运算 3、表示的数据范围 变量类型: 1、本地变量 2、环境(全局)变量 3、特殊变量     驼峰命名变量:每个单词一个字母大写 本地变量:…

    Linux干货 2016-08-11
  • 第五周:grep和find的使用练习

    1、显示当前系统上root、fedora或user1用户的默认shell; grep -E "^(root|fedora|user1)" /etc/passwd | cut -d: -f7 grep -E "^root|fedora|use…

    Linux干货 2016-10-25
  • 8.5_Linux习题练习和作业

    课堂练习题 1、找出ifconfig命令结果中本机的所有IPv4地址 # ifconfig | grep -oE "([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|2…

    Linux干货 2016-08-10
  • linux根下的文件

    /boot:系统启动时要加载引导的静态文件,内核和ramdisk及grub等 /bin:系统自身启动和运行时可能用到的核心二进制程序,不能关联至独立分区。 /sbin:管理类基本命令,不能关联至独立分区,系统启动便会用到的程序。 /lib:基本共享库文件,以及内核模块文件 /lib64:专用于x86_64系统上的辅助共享库文件存放位置 /etc:大多数应用程…

    Linux干货 2017-03-30

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-27 13:02

    写的很棒! 加油!