Linux 中使用 chage 进行密码管理
在我们的上一篇文章中,我们通过使用 passwd 命令了解了密码管理,在这篇文章中,我们通过使用 chage 了解了密码管理。
在阅读本文之前,请先阅读使用 passwd 进行密码管理
chage 使您能够修改密码周围的参数(复杂性、期限、到期时间)。我们可以使用 chage 命令编辑和管理密码过期详细信息。但是,root 用户可以对任何用户帐户执行 chage 命令,但不能对其他用户执行 chage 命令。
Syntax: chage [options] USERNAME
选项 :
-d LAST_DAY Indicates the day the password was last changed
-E EXPIRE_DATE Sets the account expiration date
-I INACTIVE Changes the password in an inactive state after the account expires
-l Shows account aging information
-m MIN_DAYS Sets the minimum number of days between password changes
-M MAX_DAYS Sets the maximum number of days a password is valid
-W WARN_DAYS Sets the number of days to warn before the password expires
例如我们可以使用chage命令查找特定的用户信息,如下所示。
[root@localhost ~]# chage -l root
Last password change : Mar 12, 2016
Password expires : Jul 25, 2017
Password inactive : never
Account expires : Dec 31, 2025
Minimum number of days between password change : 365
Maximum number of days between password change : 500
Number of days of warning before password expires : 7
如何强制用户更改密码,这对于系统管理员来说是一个大问题,为了安全起见,强制用户定期更改密码。
为此,我们使用 chage 选项 -M 为用户设置密码到期日期。根用户可以为任何用户设置密码到期日期。
请注意,选项 -M 将更新“密码过期”和“密码更改之间的最大天数”条目,如下所示。
Syntax: # chage -M number-of-days username
[root@localhost ~]# chage -M 60 root
上述命令将密码有效期设置为 60 天。
[root@localhost ~]# chage -l root
Last password change : Mar 12, 2016
Password expires :Jul 25, 2017
Password inactive : never
Account expires : Dec 31, 2025
Minimum number of days between password change : 0
Maximum number of days between password change : 60
Number of days of warning before password expires : 7
使用 -E 选项设置用户的帐户到期日期
我们还可以使用 chage 命令使用选项 -E 设置帐户到期日期,如下所示。下面给出的日期采用“YYYY-MM-DD”格式。这将更新“帐户过期”值,如下所示。
[root@localhost ~]# chage -E "2017-10-03" root
[root@localhost ~]# chage -l root
Last password change : Mar 12, 2016
Password expires : Jul 25, 2017
Password inactive : never
Account expires : Oct 03, 2017
Minimum number of days between password change : 0
Maximum number of days between password change : 60
Number of days of warning before password expires : 7
强制用户帐户在 n 天不活动后被锁定
通常,如果密码过期,用户将被迫在下次登录时更改密码。您还可以设置一个附加条件,在密码过期后,如果用户在 6 天内从未尝试登录,您可以使用选项 -I 自动锁定其帐户,如下所示。在此示例中,“密码无效”日期设置为距“密码过期”值 10 天。帐户被锁定后,只有系统管理员才能解锁。
# chage -I 6 root
# chage -l root
Last password change : Mar 12, 2016
Password expires : Jul 25, 2017
Password inactive : Aug 31,2017
Account expires : Oct 03, 2017
Minimum number of days between password change : 0
Maximum number of days between password change : 60
Number of days of warning before password expires : 7
如何设置更改密码的最短天数
我们可以使用选项 -m 和 chage 命令来设置密码更改之间的最短天数,如下所示。
chage -m 10 USERNAME
[root@localhost ~]# chage -m 10 root
[root@localhost ~]# chage -l root
Last password change : Mar 12, 2016
Password expires : Jul 25, 2017
Password inactive : Aug 31,2017
Account expires : Oct 03, 2017
Minimum number of days between password change : 10
Maximum number of days between password change : 60
Number of days of warning before password expires : 7
如何设置密码过期前的警告天数
我们可以使用选项 -W 和 chage 命令来设置密码过期前警告的天数
[root@localhost ~]# chage -W 10 root
[root@localhost ~]# chage -l root
Last password change : Mar 12, 2016
Password expires : Jul 25, 2017
Password inactive : Aug 31,2017
Account expires : Oct 03, 2017
Minimum number of days between password change : 10
Maximum number of days between password change : 60
Number of days of warning before password expires : 10
如何禁用用户帐户的密码时效
要禁用特定帐户的帐户密码老化,我们必须在该帐户上进行以下设置。
-m 0 will set the minimum number of days between password change to 0
-M 99999 will set the maximum number of days between password change to 99999
-I -1 (number minus one) will set the “Password inactive” to never
-E -1 (number minus one) will set “Account expires” to never.
当我们使用 chage 命令设置帐户的详细信息时,不要重置
使用 passwd 命令输入密码,因为这样做会删除对帐户的任何更改
即将到期。
祝您使用 Linux 愉快!