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

相关推荐

  • 正则表达式及grep使用

    正则表达式分为:“基本正则表达式 BRE”跟“扩展正则表达式 ERE” 基本正则表达式:     (1)字符匹配               "."任意单个字符 &nbs…

    Linux干货 2016-07-10
  • LVS NAT模型演示

    LVS NAT模型演示 这里我们刻意将两个RS的web页面文件,提供为不同的内容(实际应用中,每个RS页面的内容要完全相同) 基本配置 [root@web1 ~]# route add default gw 192.168.110.130  –> web1服务器…

    2016-10-29
  • 网络N23期第一周(计算机的组成及功能及Linux的发行版等)

    一. 描述计算机的组成及功能 计算机由五大硬件组成:” 运算器 + 控制器 + 储存器 + 输入设备 + 输出设备” 。 运算器(Datapath):运算器的功能是对数据进行各种算术运算和逻辑运算,即对数据进行加工处理。 控制器(Control):是整个计算机的中枢神经,其功能是对程序规定的控制信息进行解释,根据其要求进行控制,调度程序、数据、地址,协调计算…

    Linux干货 2016-09-22
  • Linux用户、组管理和正则表达式的基础命令

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何权限 [root@localhost skel]# cp -r /etc/skel /home/tuser1 [root@localhost skel]# ls -al /home/tuser1 total 12 drwxr-xr-x.…

    Linux干货 2017-07-20
  • SSH服务器实现telnet请求转发

    本次实验做的工作如下图: 中间一台CentOS6通过SSH转发两边两台CentOS7的telnet通信。 首先,在右侧CentOS7上安装telnet服务 [root@Shining ~]# yum install -y telnet telnet-server 启动telnet服务 [root@Shining ~]# systemctl start tel…

    Linux干货 2016-12-04
  • 马哥教育网络班22期+第二周课程练习

    1、Linux文件管理类命令     cp:copy files and directories 复制文件和目录         单源复制:cp [OPTION]… [-T] SOURCE DEST   &n…

    Linux干货 2016-08-22

评论列表(1条)

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

    写的很棒! 加油!