FlossDoc: 請協力 GPLv3 的中文翻譯工作。
管理使用者
FlossDoc,自由中文開源知識庫
目录 |
新增使用者
大部份 Unix/Linux 系統都是使用 /etc/passwd 檔案來存放所有使用者的資訊。此檔案是一個格式非常簡單的文字檔案,對大部份 Unix/Linux 您甚至可以直接使用一般的文字編輯器直接編輯此檔案來增減使用者。當然為安全和方便起見,我們一般都會使用系統提供的工具去進行使用者管理的工作而非直接編輯此檔案。大部份 GNU/Linux 都提供 useradd 或 adduser 命令來進行新增使用者的工作。留意不同 GNU/Linux 中的 useradd 和 adduser 命令表現會有分別。
adduser (Debian/Ubuntu)
Debian 系 GNU/Linux (包括 Ubuntu, Linux Mint 等) 的系統管理員最好使用 adduser 命令來新增使用者,因為它們的 adduser 提供較佳的介面。在超級使用者 root 身份下輸入命令 adduser 新使用者名稱:
# adduser johndoe Adding user `johndoe' ... Adding new group `johndoe' (1003) ... Adding new user `johndoe (1003) with group `johndoe ... Creating home directory `/home/johndoe' ... Copying files from `/etc/skel' ... Enter new UNIX password: 輸入新密碼 Retype new UNIX password: 重新輸入密碼 passwd: password updated successfully Changing the user information for johndoe Enter the new value, or press ENTER for the default Full Name []: John Doe Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] y
adduser 會自動編排使用者識別碼 (user identifer, UID,如例子中的 1003)、和使用者名稱同名的預設使用者群組 (user group) 以及家目錄 (home directory, 如例子中的 /home/johndoe) 給新使用者,並要求設定新使用者的密碼、全名、辦公室編號、辦公室電話號碼、住宅電話號碼等資訊。除密碼外,其他資料都可以漏空。基於安全和私隱問題,現代一般人都不會填上辦公室編號、電話號碼等資訊。
useradd (CentOS/Fedora/RHEL)
Red Hat 系 GNU/Linux 的系統管理員主最好使用命令 useradd 來新增使用者。在超級使用者 root 身份下輸入命令 useradd 新使用者名稱:
# useradd johndoe
如果沒有任何錯誤訊息,即表示已成功新增新使用者了。useradd 會自動編排使用者識別碼 (user identifer, UID,如例子中的 1003)、和使用者名稱同名的預設使用者群組 (user group) 以及家目錄 (home directory, 如例子中的 /home/johndoe) 給新使用者。留意這個新使用者並沒有設定密碼,任何人都無法登入。您需要使用命令 password 使用者名稱 設定密碼。
useradd (SuSE/OpenSuSE)
OpenSuSE 同樣使用命令 useradd,不過預設不會建立家目錄。要建立家目錄需要加入選項 -m,例如:
# useradd -m johndoe
和 Red Hat 系一樣,useradd 不會設定密碼,您仍要使用命令 password 使用者名稱 設定密碼,使用者才可以登入。
# passwd johndoe Changing password for user cent. New Password: 輸入新密碼 Reenter New Password: 重新輸入密碼 Password changed. #
adduser (FreeBSD)
FreeBSD 的系統管理員最好使用 adduser 命令來新增使用者,因為它們的 adduser 提供較佳的介面。在超級使用者 root 身份下輸入命令 adduser:
# adduser Username: johndoe Full name: John Doe Uid (Leave empty for default): Login group [johndoe]: Login group is johndoe. Invite johndoe into other groups? []: wheel Login class [default]: Shell (sh csh tcsh zsh nologin) [sh]: tcsh Home directory [/home/johndoe]: 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 : johndoe Password : **** Full Name : John Doe Uid : 1003 Class : Groups : johndoe wheel Home : /home/johndoe Shell : /usr/local/bin/zsh Locked : no OK? (yes/no): yes adduser: INFO: Successfully added (johndoe) to the user database. Add another user? (yes/no): no Goodbye! #
adduser 會自動編排使用者識別碼 (user identifer, UID,如例子中的 1003)、和使用者名稱同名的預設使用者群組 (user group) 以及家目錄 (home directory, 如例子中的 /home/johndoe) 給新使用者。除密碼外,其他資料都可以漏空。
刪除使用者
userdel (GNU/Linux)
在超級使用者 root 身份下輸入命令 userdel 使用者名稱
userdel johndoe
如果沒有任何錯誤訊息,即表示已成功刪除使用者了。
rmuser (FreeBSD)
在超級使用者 root 身份下輸入命令 rmuser 使用者名稱
# rmuser johndoe Matching password entry: johndoe:*:1003:1003::0:0:John Doe:/home/johndoe:/usr/local/bin/zsh Is this the entry you wish to remove? y Remove user's home directory (/home/johndoe)? y Updating password file, updating databases, done. Updating group file: trusted (removing group johndoe -- personal group is empty) done. Removing user's incoming mail file /var/mail/johndoe: done. Removing files belonging to johndoe from /tmp: done. Removing files belonging to johndoe from /var/tmp: done. Removing files belonging to johndoe from /var/tmp/vi.recover: done. #
如果沒有任何錯誤訊息,即表示已成功刪除使用者了。
更改使用者密碼 (passwd)
要更改使用者密碼可以使用命令 passwd。一般使用者輸只可以更改自己帳戶的密碼,而超級使用者 root 則可以更改任何使用者的密碼。當一般使用者輸入命令 passwd 後,passwd 首先會要求使用者輸入當前的密碼以確認身份,然後讓輸入兩次新密碼來確認新密碼。留意輸入新舊密碼時,Unix/Linux 不會像現代一般軟件每按一鍵顯示星號 (“*”),請不要擔心。
$ passwd Changing password for user johndoe. Current UNIX password: 輸入目前密碼 New Password: 輸入新密碼 Reenter New Password: 重新輸入密碼 Password changed. $
超級使用者 root 可以在 passwd 後加上使用者名稱設定或更改任何使用者的密碼 (和一般使用者不同,passwd 自然要會問該使用者目前的密碼):
# passwd johndoe Changing password for user johndoe. New Password: 輸入新密碼 Reenter New Password: 重新輸入密碼 Password changed. #
更改使用者資料
設定使用者全名 (chfn)
您可以使用命令 chfn 更改自己帳戶的使用者全名、辦公室編號、辦公室電話號碼、住宅電話號碼等資訊:
chfn 使用者 更改使用者全名、辦公室編號、辦公室電話號碼、住宅電話號碼等資訊:
$ chfn Changing the user information for johndoe Enter the new value, or press ENTER for the default Full Name [John Doe]: Dr. John Doe Room Number []: 1302 Work Phone []: 413 6823 Home Phone []: Other []:
超級管理員root 可以在 chfn 後加上使用者名稱更改任何使用者的資料:
chfn johndoe
亦可以使用選項 -f 使用者全名、-r 辦公室編號、-w 辦公室電話號碼、-h 住宅電話號碼 和 -o 其他資訊 直接在命令上指定要更改的資料,較適合在 shell script 中使用:
chfn -f "Dr. John Doe" -r 1302 -w "413 6823" johndoe
此外,命令 usermod -c 全名 使用者 亦可以有相同的作用:
usermod -c "Dr. John Doe, 1302, 413 6823" johndoe
chpass (FreeBSD)
FreeBSD 一般使用 chpass 命令更改使用者資料。一般使用者使用 chpass 時只可以更改自己帳戶的 shell、全名、辦公室編號、辦公室電話號碼和住宅電話號碼等資料。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:
離開文字編輯器時 chpass 會要求使用者輸入密碼確認,然後更改相關資料。
超級使用者 root 可以在 chpass 後加上使用者名稱更改任何使用者的資料:
chpass johndoe
除了一般資料,chpass 讓超級使用者更改包括密碼、使用者識別編號 (UID)、首要群組等重要資料。
#Changing user database information for jru. Login: johndoe Password: * Uid [#]: 1003 Gid [# or name]: 1003 Change [month day year]: Expire [month day year]: Class: Home directory: /home/johndoe Shell: /usr/local/bin/zsh Full Name: John Doe Office Location: Office Phone: Home Phone: Other information:
請參看
外部連結
- 維基百科: shadow password - 維基百科對 shadow password 的說明
- GNU/Linux Manpage: useradd(8) - Create a new user or update default new user information


[SuSE][OpenSuSE] - GNU/Linux 中的 useradd(8) 說明文件
- GNU/Linux Manpage: userdel(8) - Delete a user account and related files


[SuSE][OpenSuSE] - GNU/Linux 中的 userdel(8) 說明文件
- GNU/Linux Manpage: usermod(8) - Modify a user account


[SuSE][OpenSuSE] - GNU/Linux 中的 usermod(8) 說明文件
- GNU/Linux Manpage: chfn(1) - Change real user name and information


[SuSE][OpenSuSE] - GNU/Linux 中的 chfn(1) 說明文件
- FreeBSD Handbook - 13.6 Modifying Accounts] (The FreeBSD Documentation Project 2011)
- For People New to Both FreeBSD and UNIX® - 2 Adding A User with Root Privileges (Annelise Anderson 1997-08-15)
- BSD Manpage: adduser(8) - command for adding new users


- FreeBSD/NetBSD/OpenBSD 中的 adduser(8) 說明文件
- BSD Manpage: rmuser(8) - remove users from the system


- FreeBSD/NetBSD/OpenBSD 中的 rmuser(8) 說明文件
- BSD Manpage: chpass(8) - add or change user database information


- FreeBSD/NetBSD/OpenBSD 中的 chpass,chfn,chsh(8) 說明文件

