正如 passwd 命令的名稱所示,其用於改變系統用戶的密碼。如果 passwd 命令由非 root 用戶執行,那麼它會詢問當前用戶的密碼,然後設置調用該命令的用戶的新密碼。當此命令由超級用戶 root 執行的話,就可以重新設置任何用戶的密碼,包括不知道當前密碼的用戶。
在這篇文章中,我們將用實例來介紹 passwd 命令。
# passwd {options} {user_name}
可以在 passwd 命令使用不同的選項,列表如下:
當你使用非 root 用戶登錄時,比如我使用 ‘linuxtechi’ 登錄的情況下,運行 passwd 命令它會重置當前登錄用戶的密碼。
[linuxtechi@linuxworld ~]$ passwd
Changing password for user linuxtechi.
Changing password for linuxtechi.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[linuxtechi@linuxworld ~]$
當你作為 root 用戶登錄後並運行 passwd 命令時,它默認情況下會重新設置 root 的密碼,如果你在 passwd 命令後指定了用戶名,它會重置該用戶的密碼。
[root@linuxworld ~]#passwd
[root@linuxworld ~]#passwd linuxtechi
注意 : 系統用戶的密碼以加密的形式保存在 /etc/shadow 文件中。
要顯示用戶密碼的狀態信息,請在 passwd 命令後使用 -S 選項。
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-200999997-1(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
在上面的輸出中,第一個字段顯示的用戶名,第二個字段顯示密碼狀態(PS = 密碼設置,LK = 密碼鎖定,NP = 無密碼),第三個字段顯示了上次修改密碼的時間,後面四個字段分別顯示了密碼能更改的最小期限和最大期限,警告期限和沒有使用該口令的時長。
為了顯示所有用戶密碼的狀態信息需要使用 “-aS”選項在passwd 命令中,示例如下所示:
root@localhost:~#passwd-Sa
(LCTT譯注:不同發行版/passwd 的行為不同。CentOS6.6 沒有測試成功,但 Ubuntu 可以。)
用我做例子,刪除 ‘linuxtechi‘ 用戶的密碼。
[root@linuxworld ~]#passwd-d linuxtechi
Removing password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi NP 2015-09-200999997-1(Empty password.)
[root@linuxworld ~]#
“-d” 選項將清空用戶密碼,並禁用用戶登錄。
在 passwd 命令中使用 '-e' 選項會立即使用戶的密碼過期,這將強制用戶在下次登錄時更改密碼。
[root@linuxworld ~]#passwd-e linuxtechi
Expiring password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 1970-01-010999997-1(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
現在嘗試用 linuxtechi 用戶 SSH 連接到主機。
在 passwd 命令中使用 ‘-l‘ 選項能鎖定用戶的密碼,它會在密碼的起始位置加上“!”。當他/她的密碼被鎖定時,用戶將不能更改它的密碼。
[root@linuxworld ~]#passwd-l linuxtechi
Locking password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi LK 2015-09-200999997-1(Password locked.)
[root@linuxworld ~]#
[root@linuxworld ~]#passwd-u linuxtechi
Unlocking password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#
在 passwd 命令中使用 -i 選項用於設系統用戶的非活動時間。當用戶(我使用的是linuxtechi用戶)密碼過期後,用戶再經過 ‘n‘ 天後(在我的情況下是10天)沒有更改其密碼,用戶將不能登錄。
[root@linuxworld ~]#passwd-i 10 linuxtechi
Adjusting aging data for user linuxtechi.
passwd:Success
[root@linuxworld ~]#
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-20099999710(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
在下面的例子中,linuxtechi用戶必須在90天內更改密碼。0表示用戶可以在任何時候更改它的密碼。
[root@linuxworld ~]#passwd-n 90 linuxtechi
Adjusting aging data for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-209099999710(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
‘-w’ 選項在 passwd 命令中用於設置用戶的警告期限。這意味著,n天之後,他/她的密碼將過期。
[root@linuxworld ~]#passwd-w12 linuxtechi
Adjusting aging data for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-2090999991210(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
via: http://www.linuxtechi.com/10-passwd-command-examples-in-linux/
作者:Pradeep Kumar 譯者:strugglingyouth 校對:wxy
本文由 LCTT 原創編譯,Linux中國 榮譽推出