chage 指令:確認密碼到期日與 -M、-E、-d 選項用法
chage 指令是確認與變更 Linux 帳號密碼使用期限與帳號到期日的指令。在 AlmaLinux 9 與 Debian 12 伺服器上對照 chage -l 的輸出與預設值檔案後,密碼到期日是以最後變更日加上最長使用天數計算,兩台伺服器的預設最長使用天數都是 99999 天,因此密碼設定成不會到期。
以下輸出是在 AlmaLinux 9.8 伺服器的 root 帳號與 Debian 12 伺服器的一般帳號上執行的結果。帳號名稱與日期已換成範例值。
1️⃣ chage 指令用法
基本格式是 [chage 選項 帳號名稱]。密碼使用期限存放在只有 root 能讀取的 /etc/shadow,所以變更值的選項要用 root 帳號或 sudo 執行。依官方手冊,一般帳號對自己的帳號只能使用 -l 選項。
chageUsage: chage [options] LOGIN
Options:
-d, --lastday LAST_DAY set date of last password change to LAST_DAY
-E, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-h, --help display this help message and exit
-i, --iso8601 use YYYY-MM-DD when printing dates
-I, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-l, --list show account aging information
-m, --mindays MIN_DAYS set minimum number of days before password
change to MIN_DAYS
-M, --maxdays MAX_DAYS set maximum number of days before password
change to MAX_DAYS
-R, --root CHROOT_DIR directory to chroot into
-W, --warndays WARN_DAYS set expiration warning days to WARN_DAYS不加帳號名稱執行時只出現用法,並以結束代碼 2 結束。依官方手冊的結束代碼表,chage 成功時以 0 結束,沒有權限時以 1 結束,選項錯誤時以 2 結束,找不到 /etc/shadow 檔案時以 15 結束。
變更值的選項成功時不會輸出任何內容就結束,所以變更後的值要用 chage -l 再查詢一次。多個選項可以寫在同一行。
2️⃣ 用 chage -l 確認密碼到期資訊
-l 選項以七行輸出帳號的密碼使用期限與到期日。在 AlmaLinux 9.8 伺服器上查詢了一個系統帳號。帳號名稱與日期是範例值。
chage -l user1Last password change : Oct 01, 2026
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7第一行是最後一次變更密碼的日期。第二行的 Password expires 是該日期加上最長使用天數得出的密碼到期日,最長使用天數是預設值 99999 時會顯示 never。
第三行的 Password inactive 是密碼到期後帳號被鎖定的日期,第四行的 Account expires 是與密碼分開設定的帳號到期日。下面三行是兩次變更密碼之間的最短天數與最長天數,以及到期前的警告天數。
加上 -i 後只有日期換成 YYYY-MM-DD 格式,其他行維持不變。
chage -l -i user1Last password change : 2026-10-01
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7在 Debian 12 伺服器上用一般帳號查詢自己的帳號時,出現了與 root 相同的七行。
chage -l webuserLast password change : Jan 20, 2026
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7用同一個一般帳號查詢其他帳號時以 [chage: Permission denied.] 結束,在 AlmaLinux 9.8 伺服器上由 root 以 nobody 帳號權限查詢其他帳號時也是同樣的訊息。
chage -l daemonchage: Permission denied.一般帳號能查詢自己的帳號,是因為 chage 程式附帶的權限。AlmaLinux 9.8 伺服器的 /usr/bin/chage 在擁有者欄位有 s(setuid),因此即使由一般帳號執行,也會以 root 權限讀取 /etc/shadow。Debian 12 伺服器則是在群組欄位有 s(setgid)與 shadow 群組。
stat -c "%A %U %G %n" /etc/shadow /usr/bin/chage---------- root root /etc/shadow
-rwsr-xr-x root root /usr/bin/chage查詢不存在的帳號時,會以 /etc/passwd 中沒有該名稱的錯誤結束。
chage -l user9chage: user 'user9' does not exist in /etc/passwd3️⃣ chage 選項
選項清單用 chage --help 輸出。依 AlmaLinux 9.8 伺服器的 shadow-utils 4.9,-l 是查詢選項,-i、-h、-R 是輔助選項,-d、-E、-I、-m、-M、-W 這六個是變更值的選項。
| 選項 | 複製 | 說明 |
|---|---|---|
| -l, --list | 查詢密碼使用期限與到期日 | |
| -i, --iso8601 | 以 YYYY-MM-DD 格式輸出日期,與 -l 搭配 | |
| -d, --lastday | 指定最後變更密碼日期,0 表示下次登入時變更 | |
| -E, --expiredate | 指定帳號到期日(YYYY-MM-DD),-1 表示解除 | |
| -M, --maxdays | 密碼最長使用天數,-1 表示解除檢查 | |
| -W, --warndays | 到期前的警告天數 |
| 選項 | 複製 | 說明 |
|---|---|---|
| -I, --inactive | 密碼到期後到帳號鎖定為止的天數,-1 表示解除 | |
| -m, --mindays | 兩次變更密碼之間的最短天數,0 表示不限制 | |
| -R, --root | 套用到其他根目錄的設定檔 | |
| -h, --help | 輸出說明 |
日期值以 YYYY-MM-DD 格式或從 1970 年 1 月 1 日起經過的天數來寫。
不加選項只寫帳號名稱時,會切換成互動模式逐一詢問各個值,方括號中顯示目前的值。輸入新值,要保留原樣的欄位只按 Enter。
chage user1Changing the aging information for user1
Enter the new value, or press ENTER for the default
Minimum Password Age [0]:
Maximum Password Age [99999]: 90
Last Password Change (YYYY-MM-DD) [2026-10-01]:
Password Expiration Warning [7]: 14
Password Inactive [-1]:
Account Expiration Date (YYYY-MM-DD) [-1]: 上面的範例是只變更最長使用天數與警告天數、其餘以 Enter 略過的輸入。
🔲 chage 指令模擬器
在下方模擬器中,可以用 chage -l 查詢值,並用 -M、-E、-d 選項與不加選項的互動輸入試著變更密碼使用期限。
4️⃣ chage 指令實務範例
🔲 到期警告與首次登入時變更密碼
替用 useradd 建立的 devuser 帳號設定最長使用天數 90 天與警告天數 14 天,再從一般帳號 alice 用 su 登入 devuser,確認每種設定出現的訊息。
把最後變更日設為 80 天前,製造出密碼再十天就到期的狀態。輸入密碼後,剩餘天數以警告顯示,登入照常進行。
~]# chage -d $(date -d '-80 days' +%F) devuser
~]# chage -l devuser | head -2
Last password change : Aug 26, 2026
Password expires : Nov 24, 2026
~]# su - alice -c 'su - devuser -c id'
Password:
Warning: your password will expire in 10 days.
uid=1001(devuser) gid=1001(devuser) groups=1001(devuser),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023指定 -d 0 後,出現管理員要求變更的訊息,並依序詢問目前密碼與新密碼,新密碼輸入兩次後指令才執行。
~]# chage -d 0 devuser
~]# su - alice -c 'su - devuser -c id'
Password:
You are required to change your password immediately (administrator enforced).
Current password:
New password:
Retype new password:
uid=1001(devuser) gid=1001(devuser) groups=1001(devuser),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023變更密碼後查詢,Last password change 回到當天日期,Password expires 也重新計算為當天起 90 天後。
~]# chage -l devuser | head -3
Last password change : Nov 14, 2026
Password expires : Feb 12, 2027
Password inactive : never用 -E 把已過的日期設為帳號到期日後,即使密碼輸入正確,也會出現帳號已到期的訊息並擋下登入。用 -E -1 解除後,Account expires 回到 never。
~]# chage -E 2026-11-01 devuser
~]# su - alice -c 'su - devuser -c id'
Password:
Your account has expired; please contact your system administrator.
su: User account has expired
~]# chage -E -1 devuser
~]# chage -l devuser | grep 'Account expires'
Account expires : never🔲 一開始常犯的錯誤
chage -E 即使日期格式錯誤,也沒有錯誤並以結束代碼 0 結束。月與日寫反的 2026-31-12 被存成 2028 年 7 月 12 日,不存在的日期 2026-12-32 被存成隔天的 2027 年 1 月 1 日。沒有連字號的 20261231 被當成從 1970 年 1 月 1 日起算的天數讀取,存成 57443 年。
~]# chage -E '2026-31-12' devuser
~]# chage -l devuser | grep 'Account expires'
Account expires : Jul 12, 2028
~]# chage -E '20261231' devuser
~]# chage -l devuser | grep 'Account expires'
Account expires : May 15, 57443
~]# chage -E '2026-12-32' devuser
~]# chage -l devuser | grep 'Account expires'
Account expires : Jan 01, 2027所以執行 -E 後,要立刻用 chage -l 確認 Account expires 那一行。像 2026-12-31 這樣依年、月、日順序寫的值,會照原樣存成 Dec 31, 2026。
從 root 用 su 確認設定是否生效,也是常犯的錯誤。即使用 -E 設定已過日期讓帳號到期,root 的 su 也不詢問任何內容就輸出 id 結果,到期訊息只在從一般帳號用 su 進入時出現。
~]# su - devuser -c id
uid=1001(devuser) gid=1001(devuser) groups=1001(devuser),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023🔲 解讀 chage -l 結果的方法
chage -l 的日期在 /etc/shadow 中以從 1970 年 1 月 1 日起算的天數存放。設定 -M 90 -W 14 與 -E 2026-12-31 後把兩個輸出並排比對,20771 是 Last password change 的 Nov 14, 2026,20818 是 Account expires 的 Dec 31, 2026。其間的 0、90、14 是 chage -l 下面三行的最短天數、最長天數與警告天數。
~]# chage -l devuser
Last password change : Nov 14, 2026
Password expires : Feb 12, 2027
Password inactive : never
Account expires : Dec 31, 2026
Minimum number of days between password change : 0
Maximum number of days between password change : 90
Number of days of warning before password expires : 14
~]# grep '^devuser:' /etc/shadow | cut -d: -f1,3-9
devuser:20771:0:90:14::20818:Password expires 在 /etc/shadow 中沒有另外存放的欄位,是以最後變更日加上最長使用天數 90 天計算出的 Feb 12, 2027。
-M、-E 與 -d 選項可以在 chage 指令模擬器中直接輸入試試。
不再使用的帳號,要用 userdel 指令連同家目錄一起刪除。
5️⃣ 用 chage -M、-m、-W 設定密碼使用期限
-M 是密碼可使用的最長天數。依官方手冊,最後變更日加上這個天數的日期過後,必須在登入時變更密碼才能使用帳號。
chage -M 90 user1-W 決定到期前幾天開始在登入時發出警告。
chage -W 14 user1-m 決定變更過一次的密碼在幾天內不能再變更。
chage -m 1 user1設定三個值後查詢,Password expires 那一行會變成最後變更日加上 90 天的日期。
Last password change : Oct 01, 2026
Password expires : Dec 30, 2026
Password inactive : never
Account expires : never
Minimum number of days between password change : 1
Maximum number of days between password change : 90
Number of days of warning before password expires : 14要解除最長使用天數檢查,就對 -M 指定 -1。
chage -M -1 user1新帳號一開始取得的值由 /etc/login.defs 的 PASS_ 那幾行決定。AlmaLinux 9.8 伺服器與 Debian 12 伺服器的值都相同。
grep ^PASS_ /etc/login.defsPASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_WARN_AGE 7依官方手冊,這三個值只在建立帳號時套用,修改檔案也不會影響既有帳號。既有帳號要用 chage 逐一變更。
6️⃣ 用 chage -E、-I 設定帳號到期與鎖定
-E 是帳號本身的到期日。依官方手冊,過了這個日期,即使密碼有效也無法登入,必須由管理員變更到期日才能再使用。
chage -E 2026-12-31 user1-I 是密碼到期後仍可登入並變更密碼的寬限天數。過了這段期間,帳號就會被鎖定。
chage -I 7 user1在設定最長使用天數 90 天的帳號上設定兩個值後查詢,Password inactive 那一行是密碼到期日加上 7 天的日期,Account expires 那一行是指定的帳號到期日。
Last password change : Oct 01, 2026
Password expires : Dec 30, 2026
Password inactive : Jan 06, 2027
Account expires : Dec 31, 2026
Minimum number of days between password change : 1
Maximum number of days between password change : 90
Number of days of warning before password expires : 14要取消帳號到期日,就對 -E 指定 -1。
chage -E -1 user1解除後查詢,Account expires 那一行會再回到 never。不同之處在於,密碼到期只擋下用密碼的登入,帳號到期則擋下登入本身。
7️⃣ 用 chage -d 0 在下次登入時變更密碼
-d 是變更最後變更密碼日期的選項。依官方手冊,指定 0 時,下次登入必須變更密碼。
chage -d 0 user1在這個狀態下查詢,從 Last password change 到 Password inactive 的三行會變成 [password must be changed]。
Last password change : password must be changed
Password expires : password must be changed
Password inactive : password must be changed
Account expires : never
Minimum number of days between password change : 1
Maximum number of days between password change : 90
Number of days of warning before password expires : 14也可以直接寫日期。
chage -d 2026-10-29 user1這樣指定的日期會被記錄成當天變更了密碼,密碼到期日與開始警告的日期會以該日期為準重新計算。
🔢 FAQ & 推薦內容
建立帳號
useradd 指令:選項與 -m、-s 用法,用 passwd 設定密碼
說明用 useradd 建立帳號、用 passwd 設定密碼的步驟,以及 /etc/skel 與 /etc/passwd 結構的文章。
sudo 權限
[Linux] 給 Linux 一般使用者 root 權限的方法
比較修改 /etc/sudoers、/etc/passwd 與 /etc/group 來給一般使用者 root 權限之三種方法的文章。
ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。