未分类 · 2019年6月11日 0

初学者的Linux chage命令教程(6个示例)

Linux命令行提供了几种用于用户管理的工具。一种这样的实用程序是chage,它使您可以调整密码到期信息。在本教程中,我们将使用一些易于理解的示例来讨论此工具。但是,在我们深入之前,值得一提的是,这里的所有示例均已在Ubuntu 18.04 LTS计算机上进行了测试。

Linux chage命令

如上所述,chage命令可让您调整用户密码有效期信息。以下是其语法:

chage [options] LOGIN

这是手册页对此的说明:

chage命令更改密码更改之间的天数以及上次密码更改的日期。此信息由确定用户何时必须更改密码的系统。

以下是一些常见问题解答风格的示例,这些示例应该使您对chage命令的工作方式有一个很好的了解:

Q1。如何查看当前密码到期信息?

要查看用户的当前密码到期日期信息,请使用-l命令行选项。

chage -l [USERNAME]

这是一个例子:

chage -l himanshu

以下是它在我的系统上产生的输出:

Last password change                    : Jul 26, 2018
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

因此,当前密码设置为“永不过期”。

Q2。如何更改密码有效期?

您可以使用-M命令行选项来执行此操作,该选项要求您传递一个数字(这是指密码有效的最大天数)。

例如:

chage -M 1000 himanshu

请注意,此操作需要root特权。

如何更改密码有效期

因此,您可以在上面的屏幕截图中看到,密码有效期限现已设置为2021年4月21日。

请注意,您还可以使用-m命令行选项,该选项用于设置两次密码更改之间的最短天数。我们上面讨论的-M选项设置密码有效的最大天数。

Q3。如何更改上次密码更改日期?

您可以使用-d命令行选项来调整上次密码更改日期。作为输入,您可以将数字传递给此选项,也可以输入完整的日期。手册页的解释方式如下:

 -d, --lastday LAST_DAY
           Set the number of days since January 1st, 1970 when the password
           was last changed. The date may also be expressed in the format
           YYYY-MM-DD (or the format more commonly used in your area).

以下是一个示例:

如何更改上次密码更改日期

因此,您可以看到“上次密码更改”字段的值已成功更改。

Q4。如何在密码过期前警告用户?

chage命令还允许您设置需要更改密码之前的警告天数。可以使用-W命令行选项来完成。

-W, --warndays WARN_DAYS
           Set the number of days of warning before a password change is
           required. The WARN_DAYS option is the number of days prior to the
           password expiring that a user will be warned his/her password is
           about to expire.

例如:

chage -W 10 himanshu

此命令将确保用户在密码设置为过期10天之前看到密码过期警告。

Q5。如何锁定帐户?

使用-E命令行选项锁定帐户。它接受输入的方式类似于我们上面讨论的-W选项。供参考,以下是手册页的解释方式:

-E, --expiredate EXPIRE_DATE
           Set the date or number of days since January 1, 1970 on which the
           user's account will no longer be accessible. The date may also be
           expressed in the format YYYY-MM-DD (or the format more commonly
           used in your area). A user whose account is locked must contact the
           system administrator before being able to use the system again.

           Passing the number -1 as the EXPIRE_DATE will remove an account
           expiration date.

例如:

chage -E 2019-06-21 himanshu

从2019年6月21日开始,此命令将确保用户’himanshu’的帐户将不可访问。

Q6。如果不加选择地使用杂物会怎样?

在这种情况下会发生以下情况:

       If none of the options are selected, chage operates in an interactive
       fashion, prompting the user with the current values for all of the
       fields. Enter the new value to change the field, or leave the line
       blank to use the current value. The current value is displayed between
       a pair of [ ] marks.

如果在没有任何选择的情况下使用变位会发生什么

结论

如果您是Linux系统管理员,或者是负责Linux计算机上用户管理的人员,那么此命令值得保留。我们在这里介绍了几个chage命令行选项。要了解更多信息,请转至chage手册页