linux极简小知识:25、chage修改用户密码的有效期等

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

密码过期或即将过期的提示等问题,除了重新指定和修改密码之外,还有一个简单的解决办法,就是,调整用户密码的有效期,使账户可以继续使用。

chage命令

chage 即 change age ,可以查看、修改用户密码的有效期信息,还可以查看最新一次修改密码日期、设置密码修改的时间、在指定时间后锁定帐号等。

可以依据需要,让用户定期修改密码、指定密码过期用户的新有效期重新使用

-h查看chage的帮助信息:

$ chage -h
Usage: 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, --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 maximim 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
复制代码

chage 修改账户密码信息需要 root 权限。

-l 查看账户的密码信息

chage -l user_name 查看密码信息:

$ chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : Sep 17, 2021
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 3
Number of days of warning before password expires       : 7
复制代码

可以看到:密码的最后的修改时间、密码过期时间、密码失效(Password inactive)、账号过期、最小最大密码修改间隔、以及过期警告的天数。

-M 设置密码修改的最长天数

-M 设置密码有效期的天数。密码实际有效期是最近一次修改密码的日期加上此处设置的最长天数。当最长有效期设置大于等于10000时表示永久有效

如下,设置密码修改的最长天数为5,密码过期时间,也相应的变化:

# chage -M 5 root_test2
# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : Sep 19, 2021
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 5
Number of days of warning before password expires       : 7
复制代码

设置密码有效期为 10000 天,则密码将会永不失效:

# chage -M 10000 root_test2
# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 10000
Number of days of warning before password expires       : 7
复制代码

chage其他参数

chage -E

  • chage -E date_str 设置账户过期,指定账户具体的过期日期。
# chage -E 2021-09-20 root_test2
# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Sep 20, 2021
Minimum number of days between password change          : 0
Maximum number of days between password change          : 10000
Number of days of warning before password expires       : 7
复制代码

chage -E days 指定天数,表示的是从 1970 年 1 月 1 日 开始的天数。

如下,指定 11111 天,此时账户已经失效:

[root@VM_0_15_centos ~]# chage -E 11111 root_test2
[root@VM_0_15_centos ~]# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Jun 03, 2000
Minimum number of days between password change          : 0
Maximum number of days between password change          : 10000
Number of days of warning before password expires       : 7
[root@VM_0_15_centos ~]# su - root_test
Last login: Tue Sep 14 09:07:25 CST 2021 on pts/0
[root_test@VM_0_15_centos ~]$ su - root_test2
Password:
Your account has expired; please contact your system administrator
su: User account has expired
[root_test@VM_0_15_centos ~]$ 
复制代码

chage -E 修改过期日期后,无法设置会 never 的状态...

chage -I 设置密码过期后多长天数密码失效

  • chage -I 用于设置密码过期后,多少天数之后密码无效。

设置账户密码一天后过期:

# chage -M 1 root_test2
# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : Sep 15, 2021
Password inactive                                       : never
Account expires                                         : Oct 16, 2243
Minimum number of days between password change          : 0
Maximum number of days between password change          : 1
Number of days of warning before password expires       : 7
复制代码

设置账户密码过期后2天后密码失效:

# chage -I 2 root_test2
# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : Sep 15, 2021
Password inactive                                       : Sep 17, 2021
Account expires                                         : Oct 16, 2243
Minimum number of days between password change          : 0
Maximum number of days between password change          : 1
Number of days of warning before password expires       : 7
复制代码

chage -d 0 强制用户登陆时修改口令

chage -d 0 可实现,强制用户登陆时修改口令。

chage -W 密码过期之前的警告天数

# chage -W 8 root_test2
# chage -l root_test2
Last password change                                    : Sep 14, 2021
Password expires                                        : Sep 15, 2021
Password inactive                                       : Sep 17, 2021
Account expires                                         : Oct 16, 2243
Minimum number of days between password change          : 0
Maximum number of days between password change          : 1
Number of days of warning before password expires       : 8
复制代码

新用户创建是默认的密码有效策略

在创建新用户时,其默认的密码有效期等信息,采用的是 /etc/login.defs 配置文件中的设置。

如下Password aging controls

# Password aging controls:
#
#       PASS_MAX_DAYS   Maximum number of days a password may be used.
#       PASS_MIN_DAYS   Minimum number of days allowed between password changes.
#       PASS_MIN_LEN    Minimum acceptable password length.
#       PASS_WARN_AGE   Number of days warning given before a password expires.
#
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    8
PASS_WARN_AGE   7
复制代码

可以通过修改 PASS_MAX_DAYS 等参数,实现新建用户时,默认的密码过期、警告等策略。

比如指定一个合理的密码有效期,可以强制用户定期修改密码,保证服务器的安全。

参考

部分参考自Linux 下如何修改密码有效期?、及其他未记录的资料。