用戶權限管理與正則表達式練習
完成命令:
cp -r /etc/skel /home/tuser1
chmod -R g=,o= /home/tuser1
示例:
[root@localhost ~]# cp -r /etc/skel /home/tuser1 [root@localhost ~]# ls -al /home/tuser1 總用量 12 drwxr-xr-x. 2 root root 62 3月 25 18:29 . drwxr-xr-x. 9 root root 107 3月 25 18:29 .. -rw-r--r--. 1 root root 18 3月 25 18:29 .bash_logout -rw-r--r--. 1 root root 193 3月 25 18:29 .bash_profile -rw-r--r--. 1 root root 231 3月 25 18:29 .bashrc [root@localhost ~]# chmod -R g=,o= /home/tuser1 [root@localhost ~]# ls -al /home/tuser1 總用量 12 drwx------. 2 root root 62 3月 25 18:29 . drwxr-xr-x. 9 root root 107 3月 25 18:29 .. -rw-------. 1 root root 18 3月 25 18:29 .bash_logout -rw-------. 1 root root 193 3月 25 18:29 .bash_profile -rw-------. 1 root root 231 3月 25 18:29 .bashrc
2.編輯/etc/group文件,添加組hadoop。
完成命令:vim /etc/group
向文本末行添加 ‘‘hadoop:x:1000‘‘
示例:
[root@localhost ~]# vim /etc/group mem:x:8: 10 kmem:x:9: 11 wheel:x:10: 12 cdrom:x:11: 13 mail:x:12:postfix ······ 37 postfix:x:89: 38 sshd:x:74: 39 chrony:x:995: 40 hadoop:x:1000: 添加組名及組的ID號 :wq 輸入wq,保存並退出! [root@localhost ~]# tail -1 /etc/group 查看是否添加成功。 hadoop:x:1000:
3.手動編輯/etc/passwd文件新增一行,添加用戶hadoop,其基本組ID為hadoop組的id號;其家目錄為/home/hadoop
完成命令:vim /etc/passwd
向文本末行添加"hadoop:x:1000:1000::/home/hadoop:/bin/bash"
示例:
[root@localhost ~]# vim /etc/passwd lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ...... chrony:x:997:995::/var/lib/chrony:/sbin/nologin hadoop:x:1000:1000::/home/hadoop:/bin/bash 添加用戶名,用戶和組ID號,家目錄及默認shell路徑 :wq 保存並退出! [root@localhost ~]# tail -1 /etc/passwd hadoop:x:1000:1000::/home/hadoop:/bin/bash [root@localhost ~]# id hadoop uid=1000(hadoop) gid=1000 組=1000 用戶已成功創建
4.復制/etc/skel目錄為/home/hadoop,要求修改hadoop目錄的屬組和其他用用戶沒有任何訪問權限。
完成命令:
cp -r /etc/skel /home/hadoop
chmod g=,o= /hom/hadoop
示例:
[root@localhost ~]# cp -r /etc/skel /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop
drwxr-xr-x. 2 root root 62 3月 25 16:50 /home/hadoop
[root@localhost ~]# chmod g=,o= /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop
drwx------. 2 root root 62 3月 25 16:50 /home/hadoop
5.修改home/hadoop目錄及其內部所有文件的屬主為hadoop,屬組為hadoop。
完成命令:chown -R hadoop:hadoop /home/hadoop
示例:
[root@localhost ~]# chown -R hadoop:hadoop /home/hadoop
[root@localhost ~]# ls -ld /home/hadoop
drwx------. 2 hadoop hadoop 62 3月 25 16:50 /home/hadoop
[root@localhost ~]# ls -al /home/hadoop
總用量 12
drwx------. 2 hadoop hadoop 62 3月 25 16:50 .
drwxr-xr-x. 4 root root 36 3月 25 16:50 ..
-rw-r--r--. 1 hadoop hadoop 18 3月 25 16:50 .bash_logout
-rw-r--r--. 1 hadoop hadoop 193 3月 25 16:50 .bash_profile
-rw-r--r--. 1 hadoop hadoop 231 3月 25 16:50 .bashrc
6.顯示/proc/meminfo文件中以大寫或小寫s開頭的行;用兩種方式;
完成命令:
grep -i ^[s] /proc/meminfo
grep ^[S,s] /proc/meminfo
示例:
[root@localhost ~]# grep -i ^[s] /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 6896 kB
Slab: 80692 kB
SReclaimable: 43120 kB
SUnreclaim: 37572 kB
[root@localhost ~]# grep ^[S,s] /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 6896 kB
Slab: 80692 kB
SReclaimable: 43120 kB
SUnreclaim: 37572 kB
7.顯示/etc/passwd文件中其默認shell為非/sbin/nologin的用戶;
完成命令:
grep -v ‘/sbin/nologin‘ /etc/passwd | cut -d: -f1
示例:
[root@localhost ~]# grep -v ‘/sbin/nologin‘ /etc/passwd | cut -d: -f1
root
sync
shutdown
halt
hadoop
8.顯示/etc/passwd文件中其默認shell為/bin/bash的用戶;
完成命令:
grep ‘/bin/bash‘ /etc/passwd | cut -d: -f1
示例:
[root@localhost ~]# grep ‘/bin/bash‘ /etc/passwd | cut -d: -f1
root
hadoop
9.找出/etc/passwd文件中的一位數或兩位數;
完成命令:grep ‘\<[0-9]{1,2}\>‘ /etc/passwd
示例:
[root@localhost ~]# grep ‘\<[0-9]\{1,2\}\>‘ /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
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
10.顯示/boot/grub2/grub.cfg中以至少一個空白字符開頭的行;
完成命令:
grep ‘^[[:space:]]+‘ /boot/grub2/grub.cfg
示例:
[root@localhost ~]# grep ‘^[[:space:]]\+‘ /boot/grub2/grub.cfg
load_env
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
set default="${saved_entry}"
menuentry_id_option="--id"
menuentry_id_option=""
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
......
11.顯示/etc/rc.d/rc.local文件中以#開頭,後面跟上至少一個空白字符,而後又有至少一個非空白字符的行。
完成命令:
grep ‘^#[[:space:]] \ + [^[:space:]] \ + ‘ /etc/rc.d/rc.local
示例:
[root@localhost ~]# grep ‘^#[[:space:]]\+[^[:space:]]\+‘ /etc/rc.d/rc.local
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
# Please note that you must run ‘chmod +x /etc/rc.d/rc.local‘ to ensure
# that this script will be executed during boot.
12.打出netstat -tan命令執行結果中以‘LISTEN‘,後或跟空白字符結尾的行;
完成命令:netstat -tan | grep ‘LISTEN[[:space:]] $‘*
示例:
[root@localhost ~]# netstat -tan | grep ‘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
13.添加用戶bash,testbash,basher,nologin(此一個用戶的shell為/sbin/nologin),而後找出當前系統上其用戶名和默認shell相同的用戶信息;
完成命令:
useradd ‘username’
grep -E "^( [^:]+ \ >). \1$" /etc/passwd*
示例:
[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbash
[root@localhost ~]# useradd basher
[root@localhost ~]# useradd nologin
[root@localhost ~]# usermod -s /sbin/nologin nologin
[root@localhost ~]# tail -4 /etc/passwd
bash:x:1001:1001::/home/bash:/bin/bash
testbash:x:1002:1002::/home/testbash:/bin/bash
basher:x:1003:1003::/home/basher:/bin/bash
nologin:x:1004:1004::/home/nologin:/sbin/nologin
[root@localhost ~]# grep -E "^([^:]+\>).*\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:1001:1001::/home/bash:/bin/bash
nologin:x:1004:1004::/home/nologin:/sbin/nologin
用戶權限管理與正則表達式練習