正則表達式用戶管理

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 78 Jun?3 22:05 /home/tuser1/

[root@localhost ~]#?ls -al /home/tuser1

total 12

drwx——. 3 root root?78 Jun?3 22:05 .

drwxr-xr-x. 9 root root 112 Jun?3 22:05 ..

-rwx——. 1 root root?18 Jun?3 22:05 .bash_logout

-rwx——. 1 root root 193 Jun?3 22:05 .bash_profile

-rwx——. 1 root root 231 Jun?3 22:05 .bashrc

drwx——. 4 root root?39 Jun?3 22:05 .mozilla

[root@localhost ~]#?

2、編輯/etc/group文件,添加組hadoop。

[root@localhost ~]# vim /etc/group

# 在打開的文件中最后編輯添加一行

hadoop:x:5216:

#組hadoop添加成功

[root@localhost ~]# groupadd hadoop

3、手動編輯/etc/passwd文件新增一行,添加用戶hadoop,其基本組ID為hadoop組的id號;其家目錄為/home/hadoop。

[root@localhost?~]# vim /etc/passwd

#在打開的文件最后編輯新增一行

hadoop:x:5216:5216::/home/hadoop:/bin/bash

#新用戶hadoop添加成功

4、復制/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 12

drwx——.?3 root root?78 Jun?3 22:16 .

drwxr-xr-x. 10 root root 126 Jun?3 22:16 ..

-rwx——.?1 root root?18 Jun?3 22:16 .bash_logout

-rwx——.?1 root root 193 Jun?3 22:16 .bash_profile

-rwx——.?1 root root 231 Jun?3 22:16 .bashrc

drwx——.?4 root root?39 Jun?3 22:16 .mozilla

5、修改/home/hadoop目錄及其內部所有文件的屬主為hadoop,屬組為hadoop。

[root@localhost ~]# chown hadoop:hadoop /home/hadoop -R

[root@localhost ~]# ls -al /home/hadoop

total 12

drwx——.?3 root root?78 Jun?3 22:16 .

drwxr-xr-x. 10 root root 126 Jun?3 22:16 ..

-rwx——.?1 root root?18 Jun?3 22:16 .bash_logout

-rwx——.?1 root root 193 Jun?3 22:16 .bash_profile

-rwx——.?1 root root 231 Jun?3 22:16 .bashrc

drwx——.?4 root root?39 Jun?3 22:16 .mozilla

6、顯示/proc/meminfo文件中以大寫或小寫S開頭的行;用兩種方式;

[root@localhost ~]#?grep -i “^[s]” /proc/meminfo

SwapCached:??????60 kB

SwapTotal:????2097148 kB

SwapFree:????2093328 kB

Shmem:???????7260 kB

Slab:???????125012 kB

SReclaimable:???60684 kB

SUnreclaim:????64328 kB

[root@localhost ~]#?grep -E “^[sS]” /proc/meminfo

SwapCached:??????60 kB

SwapTotal:????2097148 kB

SwapFree:????2093328 kB

Shmem:???????7260 kB

Slab:???????125012 kB

SReclaimable:???60684 kB

SUnreclaim:????64328 kB

7、顯示/etc/passwd文件中其默認shell為非/sbin/nologin的用戶;

[root@localhost ~]# grep -E -v “/sbin/nologin$” /etc/passwd | cut -d: -f1

root

sync

shutdown

halt

gentoo

liulian

user3

mageia

8、顯示/etc/passwd文件中其默認shell為/bin/bash的用戶;

[root@localhost ~]# grep -E “/bin/bash$” /etc/passwd | cut -d: -f1

root

gentoo

liulian

user3

mageia

9、找出/etc/passwd文件中的一位數或兩位數;

[root@localhost ~]# grep -E -o “\<[0-9]{1,2}\>” /etc/passwd

0

0

1

1

2

2

3

4

4

7

5

0

6

0

7

0

8

12

11

0

12

14

50

99

99

81

81

32

32

38

38

27

27

59

59

42

42

29

29

89

89

74

74

72

72

[root@localhost ~]# grep -o “\<[[:digit:]]\{1,2\}\>” /etc/passwd

0

0

1

1

2

2

3

4

4

7

5

0

6

0

7

0

8

12

11

0

12

14

50

99

99

81

81

32

32

38

38

27

27

59

59

42

42

29

29

89

89

74

74

72

72

10、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行;

[root@localhost ~]# grep -E “^[[:space:]]+.*” /boot/grub/grub.conf

grep: /boot/grub/grub.conf: No such file or directory

11、顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符,而后又有至少一個非空白字符的行;

[root@localhost ~]# grep -E “^#[[:space:]]+[^[:space:]]+” /etc/rc.d/rc.sysinit

grep: /etc/rc.d/rc.sysinit: No such file or directory

12、打出netstat -tan命令執行結果中以‘LISTEN’,后或跟空白字符結尾的行;

[root@localhost ~]# netstat -tan|grep -E “LISTEN[[:space:]]*$”

tcp????0???0 0.0.0.0:111???????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 127.0.0.1:25??????0.0.0.0:*????????LISTEN???

tcp6????0???0 :::111?????????:::*??????????LISTEN???

tcp6????0???0 :::22??????????:::*??????????LISTEN???

tcp6????0???0 ::1:631?????????:::*??????????LISTEN???

tcp6????0???0 ::1:25?????????:::*??????????LISTEN???

13、添加用戶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文件最后出現四個用戶]

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

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

polkitd:x:999:997:User for polkitd:/:/sbin/nologin

colord:x:998:996:User for colord:/var/lib/colord:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

libstoragemgmt:x:997:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin

rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin

setroubleshoot:x:996:993::/var/lib/setroubleshoot:/sbin/nologin

rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin

pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

chrony:x:995:990::/var/lib/chrony:/sbin/nologin

mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin

tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin

geoclue:x:994:989:User for geoclue:/var/lib/geoclue:/sbin/nologin

sssd:x:993:988:User for sssd:/:/sbin/nologin

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

gentoo:x:1000:1000::/home/gentoo:/bin/bash

liulian:x:1001:1001::/home/liulian:/bin/bash

user3:x:1002:1002::/home/user3:/bin/bash

mageia:x:100:2017::/home/linux:/bin/bash

bash:x:1003:1003::/home/bash:/bin/bash

testbash:x:1004:1004::/home/testbash:/bin/bash

basher:x:1005:1005::/home/basher:/bin/bash

nologin:x:1006:1006::/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:1003:1003::/home/bash:/bin/bash

nologin:x:1006:1006::/home/nologin:/sbin/nologin

[root@localhost ~]#?

 

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/100295

(0)
laolian2018laolian2018
上一篇 2018-06-04 09:26
下一篇 2018-06-04 11:54

相關推薦

欧美性久久久久