Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 課後作業 4:權限設定、帳號檔手動編輯與 grep/egrep 正則實戰

1. 複製 /etc/skel 並移除群組與其他人權限

cp -r /etc/skel/ /home/tuser1
chmod -R u-r,o-r tuser1/
ll
d-wxr-x--x. 2 root root 4096 Aug 14 13:58 tuser1

驗證:切換到 zengzy 後 cd /home/tuser1/ && ls 會出現 Permission denied。

2. 編輯 /etc/group 新增群組

vim /etc/group
hadoop:x:499:
echo 'hadoop1:x:505:' >> /etc/group

3. 手動編輯 /etc/passwd 新增使用者

欄位依序為:帳號、密碼佔位、UID、GID、說明、家目錄、shell。

hadoop:x:505:499::/home/hadoop:/bin/bash
id hadoop
uid=505(hadoop) gid=499(hadoop) groups=499(hadoop)

4. 複製 skel 為 /home/hadoop 並收緊權限

cp -r /etc/skel /home/hadoop
chmod g-r,o-r hadoop/
ll
drwx--x--x. 2 root root 4096 Aug 14 14:22 hadoop

5. 變更擁有者與群組

chown -R hadoop:hadoop hadoop/
drwx--x--x. 2 hadoop hadoop 4096 Aug 14 14:22 hadoop

6. 顯示 /proc/meminfo 中以 S 或 s 開頭的行(兩種寫法)

grep "^[sS]" /proc/meminfo
grep -i "^s" /proc/meminfo

7. 列出預設 shell 不是 /sbin/nologin 的帳號

grep -v "/sbin/nologin$" /etc/passwd | cut -d: -f1

8. 列出預設 shell 為 /bin/bash 的帳號

grep "/bin/bash$" /etc/passwd | cut -d: -f1

9. 找出 /etc/passwd 中的一位數或兩位數

egrep "\<[0-9]{1,2}\>" /etc/passwd
grep --color=auto "\<[0-9]\{1,2\}\>" /etc/passwd

< 與 > 是詞首/詞尾邊界,確保不會比中 100 這類位數。

10. 顯示 grub.conf 中以空白字元開頭的行

grep "^[[:space:]]\+" /boot/grub/grub.conf

11. 以 # 開頭、後接空白、再接非空白字元的行

grep "^#[[:space:]]\+[^[:space:]]\+" /etc/rc.d/rc.sysinit

12. 篩出 netstat 中的 LISTEN 行

netstat -tan | egrep "LISTEN|[[:space:]]$"

13. 新增帳號並找出「帳號名與 shell 名相同」的帳號

useradd bash
useradd testbash
useradd basher
useradd -s /sbin/nologin nologin
grep --color=auto "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd

這裡的 \1 是反向引用,讓同一行中後段的內容必須與第一個捕獲組相同,正好對應「帳號名=shell 名」這種特殊案例(如 sync、shutdown、halt、bash、nologin)。

Copyright © Linux教程網 All Rights Reserved