歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 10 個 Linux 中的 passwd 命令示例

10 個 Linux 中的 passwd 命令示例

日期:2017/2/28 13:56:49   编辑:Linux教程

正如 passwd 命令的名稱所示,其用於改變系統用戶的密碼。如果 passwd 命令由非 root 用戶執行,那麼它會詢問當前用戶的密碼,然後設置調用該命令的用戶的新密碼。當此命令由超級用戶 root 執行的話,就可以重新設置任何用戶的密碼,包括不知道當前密碼的用戶。

在這篇文章中,我們將用實例來介紹 passwd 命令。

語法 :

# passwd {options} {user_name}

可以在 passwd 命令使用不同的選項,列表如下:

例1:更改系統用戶的密碼

當你使用非 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 命令後指定了用戶名,它會重置該用戶的密碼。

  1. [root@linuxworld ~]#passwd
  2. [root@linuxworld ~]#passwd linuxtechi

注意 : 系統用戶的密碼以加密的形式保存在 /etc/shadow 文件中。

例2:顯示密碼狀態信息

要顯示用戶密碼的狀態信息,請在 passwd 命令後使用 -S 選項。

  1. [root@linuxworld ~]#passwd-S linuxtechi
  2. linuxtechi PS 2015-09-200999997-1(Passwordset, SHA512 crypt.)
  3. [root@linuxworld ~]#

在上面的輸出中,第一個字段顯示的用戶名,第二個字段顯示密碼狀態(PS = 密碼設置,LK = 密碼鎖定,NP = 無密碼),第三個字段顯示了上次修改密碼的時間,後面四個字段分別顯示了密碼能更改的最小期限和最大期限,警告期限和沒有使用該口令的時長。

例3:顯示所有賬號的密碼狀態信息

為了顯示所有用戶密碼的狀態信息需要使用 “-aS”選項在passwd 命令中,示例如下所示:

  1. root@localhost:~#passwd-Sa

(LCTT譯注:不同發行版/passwd 的行為不同。CentOS6.6 沒有測試成功,但 Ubuntu 可以。)

例4:使用 -d 選項刪除用戶的密碼

用我做例子,刪除 ‘linuxtechi‘ 用戶的密碼。

  1. [root@linuxworld ~]#passwd-d linuxtechi
  2. Removing password for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#
  5. [root@linuxworld ~]#passwd-S linuxtechi
  6. linuxtechi NP 2015-09-200999997-1(Empty password.)
  7. [root@linuxworld ~]#

-d” 選項將清空用戶密碼,並禁用用戶登錄。

例5:設置密碼立即過期

在 passwd 命令中使用 '-e' 選項會立即使用戶的密碼過期,這將強制用戶在下次登錄時更改密碼。

  1. [root@linuxworld ~]#passwd-e linuxtechi
  2. Expiring password for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#passwd-S linuxtechi
  5. linuxtechi PS 1970-01-010999997-1(Passwordset, SHA512 crypt.)
  6. [root@linuxworld ~]#

現在嘗試用 linuxtechi 用戶 SSH 連接到主機。

例6:鎖定系統用戶的密碼

在 passwd 命令中使用 ‘-l‘ 選項能鎖定用戶的密碼,它會在密碼的起始位置加上“!”。當他/她的密碼被鎖定時,用戶將不能更改它的密碼。

  1. [root@linuxworld ~]#passwd-l linuxtechi
  2. Locking password for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#passwd-S linuxtechi
  5. linuxtechi LK 2015-09-200999997-1(Password locked.)
  6. [root@linuxworld ~]#

例7:使用 -u 選項解鎖用戶密碼

  1. [root@linuxworld ~]#passwd-u linuxtechi
  2. Unlocking password for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#

例8:使用 -i 選項設置非活動時間

在 passwd 命令中使用 -i 選項用於設系統用戶的非活動時間。當用戶(我使用的是linuxtechi用戶)密碼過期後,用戶再經過 ‘n‘ 天後(在我的情況下是10天)沒有更改其密碼,用戶將不能登錄。

  1. [root@linuxworld ~]#passwd-i 10 linuxtechi
  2. Adjusting aging data for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#
  5. [root@linuxworld ~]#passwd-S linuxtechi
  6. linuxtechi PS 2015-09-20099999710(Passwordset, SHA512 crypt.)
  7. [root@linuxworld ~]#

例9:使用 -n 選項設置密碼更改的最短時間

在下面的例子中,linuxtechi用戶必須在90天內更改密碼。0表示用戶可以在任何時候更改它的密碼。

  1. [root@linuxworld ~]#passwd-n 90 linuxtechi
  2. Adjusting aging data for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#passwd-S linuxtechi
  5. linuxtechi PS 2015-09-209099999710(Passwordset, SHA512 crypt.)
  6. [root@linuxworld ~]#

例10:使用 -w 選項設置密碼過期前的警告期限

-w’ 選項在 passwd 命令中用於設置用戶的警告期限。這意味著,n天之後,他/她的密碼將過期。

  1. [root@linuxworld ~]#passwd-w12 linuxtechi
  2. Adjusting aging data for user linuxtechi.
  3. passwd:Success
  4. [root@linuxworld ~]#passwd-S linuxtechi
  5. linuxtechi PS 2015-09-2090999991210(Passwordset, SHA512 crypt.)
  6. [root@linuxworld ~]#

via: http://www.linuxtechi.com/10-passwd-command-examples-in-linux/

作者:Pradeep Kumar 譯者:strugglingyouth 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

Copyright © Linux教程網 All Rights Reserved