Linux用户、组以及正则表达式练习题

Linux用户、组以及正则表达式练习题


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

    [root@localhost ~]# cp -r /etc/skel /home/tuser1

[root@localhost ~]# chmod 700 /home/tuser1 -R
[root@localhost ~]# ls -ld /home/tuser1/
drwx------. 3 root root 74 Jun 2 10:18 /home/tuser1/
[root@localhost ~]# ls -al /home/tuser1
total 16
drwx------. 3 root root 74 Jun 2 10:18 .
drwxr-xr-x. 14 root root 4096 Jun 2 10:18 ..
-rw-------. 1 root root 18 Jun 2 10:18 .bash_logout
-rw-------. 1 root root 193 Jun 2 10:18 .bash_profile
-rw-------. 1 root root 231 Jun 2 10:18 .bashrc
drwx------. 4 root root 37 Jun 2 10:18 .mozilla

  1. 编辑/etc/group文件,添加组hadoop
[root@localhost ~]# vim /etc/group
# 在打开的文件中最后编辑添加一行
hadoop:x:5216:
#组hadoop添加成功
[root@localhost ~]# groupadd hadoop
groupadd: group ‘hadoop’ already exists

  1. 手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop
[root@localhost ~]# vim /etc/passwd
#在打开的文件最后编辑新增一行
hadoop:x:5216:5216::/home/hadoop:/bin/bash
#新用户hadoop添加成功

  1. 复制/etc/skel目录为/home/hadoop,要求修改hadoop目录的属组和其他用户没有任何权限
[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# chmod 700 /home/hadoop -R
[root@localhost ~]# ls -la /home/hadoop/
total 16
drwx------. 3 root root 74 Jun 2 11:08 .
drwxr-xr-x. 15 root root 4096 Jun 2 11:08 ..
-rwx------. 1 root root 18 Jun 2 11:08 .bash_logout
-rwx------. 1 root root 193 Jun 2 11:08 .bash_profile
-rwx------. 1 root root 231 Jun 2 11:08 .bashrc
drwx------. 4 root root 37 Jun 2 11:08 .mozilla

  1. 修改/home/hadoop目录及其内部的所有文件的属主为hadoop,属组为hadoop
[root@localhost ~]# chown hadoop:hadoop /home/hadoop -R
[root@localhost ~]# ls -al /home/hadoop
total 16
drwx------. 3 hadoop hadoop 74 Jun 2 11:08 .
drwxr-xr-x. 15 root root 4096 Jun 2 11:08 ..
-rwx------. 1 hadoop hadoop 18 Jun 2 11:08 .bash_logout
-rwx------. 1 hadoop hadoop 193 Jun 2 11:08 .bash_profile
-rwx------. 1 hadoop hadoop 231 Jun 2 11:08 .bashrc
drwx------. 4 hadoop hadoop 37 Jun 2 11:08 .mozilla

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

[root@localhost ~]# grep -i "^[s]" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7272 kB
Slab: 111076 kB
SReclaimable: 54184 kB
SUnreclaim: 56892 kB
```
[root@localhost ~]# grep -E "^[sS]" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7272 kB
Slab: 111100 kB
SReclaimable: 54192 kB
SUnreclaim: 56908 kB

  1. 显示/etc/passwd文件中其默认shell为非/sbin/nologin的用户
[root@localhost ~]# grep -E -v "/sbin/nologin$" /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
inspur
archlinux
umb1
umb3
gentoo
cehtos
centos
mageia
slackware
hadoopoop

  1. 显示/etc/passwd文件中其默认shell为/bin/bash的用户
[root@localhost ~]# grep -E "/bin/bash$" /etc/passwd | cut -d: -f1
root
inspur
archlinux
umb1
umb3
gentoo
cehtos
centos
mageia
hadoop

  1. 找出/etc/passwd文件中的一位数或两位数
[root@localhost ~]# grep -E -o "\<[0-9]{1,2}\>" /etc/passwd
[root@localhost ~]# grep -o "\<[[:digit:]]\{1,2\}\>" /etc/passwd

  1. 显示/boot/grub/grub.conf中以至少一个空白字符开头的行
[root@localhost ~]# grep -E "^[[:space:]]+.*" /boot/grub/grub.conf

  1. 显示/etc/rc.d/rc.sysinit文件中以#开头,后面跟至少一个空白字符,而后又有至少一个非空白字符的行
[root@localhost ~]# grep -E "^#[[:space:]]+[^[:space:]]+" /etc/rc.d/rc.sysinit

  1. 打处netstat -tan命令执行结果中以‘LISTEN’,后或跟空白字符结尾的行
[root@localhost ~]# netstat -tan|grep -E "LISTEN[[:space:]]*$"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN

  1. 添加用户bash,testbash,basher,nologin(此一个用户的shell为/sbin/nologin),而后找出当前系统上其他用户名和默认shell相同的用户的信息
#添加用户
[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd -s /sbin/nologin nologin
#查看是否添加成功
[root@localhost ~]# cat /etc/passwd
#/etc/passwd文件最后出现四个用户
bash:x:5217:5217::/home/bash:/bin/bash
testbash:x:5218:5218::/home/testbash:/bin/bash
basher:x:5219:5219::/home/basher:/bin/bash
nologin:x:5220:5220::/home/nologin:/sbin/nologin
###
[root@localhost ~]# grep -E "^([[:alnum:]]+\>).*\1$" /etc/passwd
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:5217:5217::/home/bash:/bin/bash
nologin:x:5220:5220::/home/nologin:/sbin/nologin

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/99998

(1)
上一篇 2018-06-02 10:08
下一篇 2018-06-02 16:39

相关推荐

评论列表(1条)

  • 马哥教育
    马哥教育 2018-06-07 09:44

    总结的很不错。