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.www58058.com/99998

(1)
夜風夜風
上一篇 2018-06-02
下一篇 2018-06-02

相關推薦

評論列表(1條)

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

    總結的很不錯。

欧美性久久久久