使用chage来管理linux密码过期时间

今天打开线上联通的虚拟机,使用SecureCRT去连线上联通的虚拟机时,弹出下面的提示:密码过期。

所以,决定写一篇介绍和解决密码过期的文章,而我们涉及到的命令就是 chage。
 


debian系统可以通过如下命令安装chage:(chage is for change age)

apt-get install chage

CentOS7 默认是自带这个命令的


 

一 列出用户密码相关信息:
列出root用户密码相关信息:
1:对于密码永不会过期的类型举例:

[root@zabbix_nginx ~]# chage --list root  或   chage -l root
Last password change                                    : never
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
[root@zabbix_nginx ~]# 

2: 对于密码会过期的类型举例:

[root@liantong-jtfwq ~]# chage --list root   或   chage -l root
Last password change                                    : Feb 25, 2019    // 上次密码修改的时间
Password expires                                        : May 26, 2019    //  密码有效截至日期
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 90              //  密码有效期90天(3个月)
Number of days of warning before password expires       : 7               //  密码过期之前7天会提示密码过期
[root@liantong-jtfwq ~]# 

二  更改root用户密码

[root@liantong-jtfwq ~]# passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
You have new mail in /var/spool/mail/root

注意:如果是非root用户,比如普通用户tom,更改tom用户密码的命令为: passwd tom

三  通过参数 -M 设置账户密码的到期时间

语法: chage  -M  number-of-days  username
(扩展:
-M  指定的是密码有效的最大天数
-m  指定的是密码有效的最小天数。不建议用-m, 使用 默认 最小天数0 即可
)

[root@liantong-jtfwq ~]# chage -M 10 root
[root@liantong-jtfwq ~]# chage --list root  或   chage -l root
Last password change                                    : Feb 25, 2019
Password expires                                        : Mar 07, 2019
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 10
Number of days of warning before password expires       : 7

密码到期时间变成了十天后。

四 密码过期前几天(具体几天视自己设置而定)会有消息提醒

密码过期前的消息提醒信息类似如下:
Warning: your password will expire in 6 days

五 密码过期时,会强制用户修改密码。

这个见我开篇时的第一张截图。

You are required to change your password immediately (password aged)
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for dhinesh
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:

六 前面讲的是密码过期,现在讲一下怎么设置账号的过期时间。----建议不要对root用户使用

可以使用 -E 选项设置账号的过期时间,时间格式为 "YYYY-MM-DD"

以普通账户 tom 进行举例:
chage -E "2019-05-21" tom

[root@liantong-ybjt ~]# chage --list tom
Last password change                                    : Jan 24, 2019
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
[root@liantong-ybjt ~]# 
[root@liantong-ybjt ~]# chage -E "2019-05-21" tom    // 设置账户tom过期截至日期
[root@liantong-ybjt ~]# chage --list tom
Last password change                                    : Jan 24, 2019
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : May 21, 2019   
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
[root@liantong-ybjt ~]# 

七 设置用户账密永不过期

chage -m 0 -M 99999 -E -1 tom

参数解释:
-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
-E -1 (number minus one)  # will set “Account expires” to never.
[root@liantong-ybjt ~]# chage --list tom
Last password change                                    : Jan 24, 2019
Password expires                                        : Feb 03, 2019
Password inactive                                       : Feb 23, 2019
Account expires                                         : Jan 02, 1970
Minimum number of days between password change          : 0
Maximum number of days between password change          : 10
Number of days of warning before password expires       : 7
[root@liantong-ybjt ~]# chage -m 0 -M 99999 -E -1 tom
[root@liantong-ybjt ~]# chage --list tom
Last password change                                    : Jan 24, 2019
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

 

 

 

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值