Linux常用命令-chage

命令

chage

描述

change user password expiry information
更改密码过期策略

用法

1
chage [options] USER

选项

1
2
3
4
5
6
7
8
Options:
-d 设置最后一次更改密码的日期,格式为YYYY-MM-DD,如果设置为0,则用户在下次登录时会提示必须更改密码
-E 设置账户有效期,设置为-1表示永久有效
-I 设置密码失效时间
-l 显示用户的密码策略
-m 更改密码的最短间隔天数
-M 更改密码的最长间隔天数,设置为99999表示密码永不过期
-W 密码过期前几天发出警告

注意

修改/etc/login.defs和/etc/default/useradd来设置默认账户策略,影响后续新增用户

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$ useradd usera
# 默认密码策略如下
$ chage -l usera
Last password change : Dec 24, 2020
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 -d 2020-10-20 usera
# 下次登录必须更改密码
$ chage -d 0 usera
# 设置账户有效时间
$ chage -E 2020-12-31 usera
$ chage -l usera
Last password change : Oct 20, 2020
Password expires : never
Password inactive : never
Account expires : Dec 31, 2020
# 设置账户有效期为90天,从今天开始
$ chage -E $(date -d +90days +%Y-%m-%d) usera
$ chage -l usera
Account expires : Mar 24, 2021
# 设置两次密码更改的间隔天数,最少3天,最多60天.即3天内不能重复更改密码,60后必须更改密码
$ chage -m 3 -M 60 usera
$ chage -l usera
Last password change : Oct 20, 2020
Password expires : Dec 19, 2020
Password inactive : never
Account expires : never
Minimum number of days between password change : 3
Maximum number of days between password change : 60
# 设置密码永不过期-never
chage -M 99999 admin

# 设置usera的密码策略,密码有效期为60天,提前14天提醒更改密码,密码过期7天后账户将失效
$ chage -m 0 -M 60 -W 14 -I 7 usera
$ chage -l usera
Last password change : Oct 20, 2020
Password expires : Dec 19, 2020
Password inactive : Dec 26, 2020
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 60
Number of days of warning before password expires : 14

# 账户密码未过期之前提示信息如下,截止日期为Password expires
Warning: your password will expire in 3 days

# 账户密码过期之后且在密码失效之前提示需要更改密码,截止日期为Password inactive
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user usera.
Changing password for usera.
(current) UNIX password:
New password:

# 密码失效之后将无法登录
Your account has expired; please contact your system administrator