UNIX® 環境下處理帳戶的命令相當多,最常用的是這幾支:
| adduser(8) | 在命令列新增使用者 |
| rmuser(8) | 在命令列刪除使用者 |
| chpass(1) | 修改使用者資料庫資訊的彈性工具 |
| passwd(1) | 修改密碼的簡便命令列工具 |
| pw(8) | 功能強大、可修改帳戶的系統工具 |
adduser(8) 會建立一組新帳戶:寫入 passwd 與 group 檔、建立家目錄、從 /usr/share/skel 複製預設設定檔(dotfiles),並寄一封歡迎信給新使用者。
在 FreeBSD 5.0 之後,adduser(8) 由 Perl 腳本改寫為 shell 腳本,內部包裝 pw(8),因此用法在 4.X 與 5.X 上略有差異。若要建立初始化設定檔,用 adduser -s -config_create。
例 13-1. FreeBSD 4.X 下設定 adduser 並新增帳戶
# adduser -v Use option ``-silent'' if you don't want to see all warnings and questions. Check /etc/shells Check /etc/master.passwd Check /etc/group Enter your default shell: csh date no sh tcsh zsh [sh]: zsh Your default shell is: zsh -> /usr/local/bin/zsh Enter your default HOME partition: [/home]: Copy dotfiles from: /usr/share/skel no [/usr/share/skel]: Send message from file: /etc/adduser.message no [/etc/adduser.message]: no Use passwords (y/n) [y]: y Write your changes to /etc/adduser.conf? (y/n) [n]: y Ok, let's go. Don't worry about mistakes. I will give you the chance later to correct any input. Enter username [a-z0-9_-]: jru Enter full name []: J. Random User Enter shell csh date no sh tcsh zsh [zsh]: Enter home directory (full path) [/home/jru]: Uid [1001]: Enter login class: default []: Login group jru [jru]: Login group is ``jru''. Invite jru into other groups: guest no [no]: wheel Enter password []: Enter password again []: Name: jru Password: **** Fullname: J. Random User Uid: 1001 Gid: 1001 (jru) Class: Groups: jru wheel HOME: /home/jru Shell: /usr/local/bin/zsh OK? (y/n) [y]: y Added user ``jru'' Copy files from /usr/share/skel to /home/jru Add another user? (y/n) [y]: n Goodbye! #
這裡把預設 shell 設為 Ports Collection 裡的 zsh、關掉歡迎信,存檔後建立 jru 帳戶,並確定他在 wheel 組(這樣才能用 su(1) 變成 root)。
注意:輸入密碼時不會回顯任何字元或星號,請不要打錯。
注意:此後用 adduser(8) 不必再改預設值;若真要改,先離開再用 -s 選項。
例 13-2. FreeBSD 5.X 下新增帳戶
# adduser Username: jru Full name: J. Random User Uid (Leave empty for default): Login group [jru]: Login group is jru. Invite jru into other groups? []: wheel Login class [default]: Shell (sh csh tcsh zsh nologin) [sh]: zsh Home directory [/home/jru]: Use password-based authentication? [yes]: Use an empty password? (yes/no) [no]: Use a random password? (yes/no) [no]: Enter password: Enter password again: Lock out the account after creation? [no]: Username : jru Password : **** Full Name : J. Random User Uid : 1001 Class : Groups : jru wheel Home : /home/jru Shell : /usr/local/bin/zsh Locked : no OK? (yes/no): yes adduser: INFO: Successfully added (jru) to the user database. Add another user? (yes/no): no Goodbye! #
rmuser(8) 會徹底清除一個帳戶,依序執行:
注意:若某群組變成空的,且群組名與使用者名相同,該群組會一併刪除——adduser(8) 為每個使用者建立專屬群組就是這個原因。
注意:rmuser(8) 不能刪除超級使用者帳戶,那對系統是毀滅性的。預設使用互動模式,讓你清楚每一步在做什麼。
例 13-3. 互動模式刪除帳戶
# rmuser jru Matching password entry: jru:*:1001:1001::0:0:J. Random User:/home/jru:/usr/local/bin/zsh Is this the entry you wish to remove? y Remove user's home directory (/home/jru)? y Updating password file, updating databases, done. Updating group file: trusted (removing group jru -- personal group is empty) done. Removing user's incoming mail file /var/mail/jru: done. Removing files belonging to jru from /tmp: done. Removing files belonging to jru from /var/tmp: done. Removing files belonging to jru from /var/tmp/vi.recover: done. #
chpass(1) 可改密碼、shell 與個人資訊等資料庫欄位。只有超級使用者能改別人的資料;除了選擇性的使用者名參數外不需要其他選項,執行後會開啟編輯器。
注意:在 FreeBSD 5.X 上,非超級使用者編輯結束後會被要求輸入自己的密碼。
例 13-4. 以超級使用者執行 chpass
#Changing user database information for jru. Login: jru Password: * Uid [#]: 1001 Gid [# or name]: 1001 Change [month day year]: Expire [month day year]: Class: Home directory: /home/jru Shell: /usr/local/bin/zsh Full Name: J. Random User Office Location: Office Phone: Home Phone: Other information:
普通使用者只能改其中很少的幾個欄位。
例 13-5. 以普通使用者執行 chpass
#Changing user database information for jru. Shell: /usr/local/bin/zsh Full Name: J. Random User Office Location: Office Phone: Home Phone: Other information:
注意:chfn(1) 與 chsh(1) 只是 chpass(1) 的符號連結,ypchpass(1)、ypchfn(1)、ypchsh(1) 同理。NIS 會自動被支援,不必在指令前加 yp。
passwd(1) 是一般使用者改自己密碼、超級使用者改任何密碼的常用方式。
注意:使用者改密碼前必須輸入原密碼,避免使用者離開終端時被他人趁機改掉密碼。
例 13-6. 改自己的密碼
% passwd Changing local password for jru. Old password: New password: Retype new password: passwd: updating the database... passwd: done
例 13-7. 超級使用者改他人密碼
# passwd jru Changing local password for jru. New password: Retype new password: passwd: updating the database... passwd: done
注意:yppasswd(1) 也只是 passwd(1) 的連結,所以 NIS 環境下用任何一個名稱都能運作。
pw(8) 用來建立、刪除、修改、顯示使用者與群組,也能直接編輯系統帳戶檔。它的命令列選項非常強大,但也因此比前面幾支指令複雜得多,新手可能會覺得不親切。
備註:adduser(8) 的 -s 選項對應 -config_create;想改預設值則用 -v。