10 個 Linux 中的 passwd 命令樣本

來源:互聯網
上載者:User

10 個 Linux 中的 passwd 命令樣本

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

在這篇文章中,我們將用執行個體來介紹 passwd 命令。

文法 :
# passwd {options} {user_name}

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

 

例1:更改系統使用者的密碼

當你使用非 root 使用者登入時,比如我使用 ‘linuxtechi’ 登入的情況下,運行 passwd 命令它會重設當前登入使用者的密碼。

[linuxtechi@linuxworld ~]$ passwdChanging 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中國 榮譽推出

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.