本週作業前 9 題為文字處理指令的綜合應用,第 10 題則整理帳號與群組管理指令,並完成 12 個操作練習。
who | cut -d " " -f1 | sort -u
w | tail -1
sort -t: -k7 /etc/passwd | cut -d: -f7 | uniq -c | sort -n | tail -1
sort -nt: -k3 /etc/passwd | tail -10 | tr [a-z] [A-Z] >/tmp/maxusers.txt
ifconfig | grep "inet addr.*Bc" | cut -d: -f2 | cut -d " " -f1
ll /etc | grep ".conf$" | tr -s " " | cut -d " " -f9 | tr 'a-z' 'A-Z' >/tmp/etc.conf
echo $[`ll /var | wc -l` -1]
sort -nt: -k3 /etc/group | head -10 | cut -d: -f1
cat /etc/fstab /etc/issue >/tmp/etc.test
useradd 新增帳號,格式 useradd [options] LOGIN,常用選項:-c 註解、-d 家目錄、-D 更改預設值、-e 帳號過期時間、-f 密碼到期後仍可用的天數、-g 基本群組、-G 附加群組、-M 不建立家目錄、-r 建立系統帳號、-s 登入 shell、-u UID。
userdel 刪除帳號及其相關檔案,-r 連相關檔案一併刪除。usermod 修改帳號資訊,-a 為 append,只能與 -G 搭配使用,其餘選項同 useradd。
groupadd 建立群組,-g 指定 GID、-r 建立系統群組。groupdel 刪除群組。groupmod 修改群組,-g 改 GID。
chage 管理密碼老化政策,格式 chage [options] [LOGIN]:-m 密碼可更改的最小天數(0 代表隨時可改)、-M 密碼有效的最長天數、-W 到期前提前警告的天數、-E 帳號到期日、-d 上次更改日期、-I 到期後仍可用的時間、-l 顯示目前設定。
groupadd distro -g 2016 useradd mandriva -u 1005 -g distro useradd mageia -u 1100 -d /home/linux passwd mageia # 輸入 mageedu 兩次 userdel mandriva useradd slackware -u 2002 -g distro -G peguin usermod -s /bin/tcsh slackware usermod -a -G admins slackware passwd slackware chage -m 3 -M 180 -W 3 slackware useradd openstack -u 3003 -g clouds -G peguin,nova useradd -r mysql -s /sbin/nologin echo "openstack" | passwd --stdin openstack
第 9 題要求「警告 3 天」,用 chage -W 3 比 passwd -w 3 更直觀;兩者效果一致,挑順手的即可。第 6 題的附加群組若原本已有其他群組,記得加 -a 以免被覆蓋。