# chage

修改帐号和密码的有效期限

## 补充说明

**chage命令** 是用来修改帐号和密码的有效期限。

### 语法

```
chage [选项] 用户名
```

### 选项

```
-m：密码可更改的最小天数。为零时代表任何时候都可以更改密码。
-M：密码保持有效的最大天数。
-w：用户密码到期前，提前收到警告信息的天数。
-E：帐号到期的日期。过了这天，此帐号将不可用。
-d：上一次更改的日期。
-i：停滞时期。如果一个密码已过期这些天，那么此帐号将不可用。
-l：例出当前的设置。由非特权用户来确定他们的密码或帐号何时过期。
```

### 实例

可以编辑`/etc/login.defs`来设定几个参数，以后设置口令默认就按照参数设定为准：

```
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
```

当然在`/etc/default/useradd`可以找到如下2个参数进行设置：

```
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
```

通过修改配置文件，能对之后新建用户起作用，而目前系统已经存在的用户，则直接用chage来配置。

我的服务器root帐户密码策略信息如下：

```
[root@linuxde ~]# chage -l root
最近一次密码修改时间                    ： 3月 12, 2013
密码过期时间                            ：从不
密码失效时间                           ：从不
帐户过期时间                           ：从不
两次改变密码之间相距的最小天数          ：0
两次改变密码之间相距的最大天数          ：99999
在密码过期之前警告的天数                ：7
```

我可以通过如下命令修改我的密码过期时间：

```
[root@linuxde ~]# chage -M 60 root
[root@linuxde ~]# chage -l root
最近一次密码修改时间                          ： 3月 12, 2013
密码过期时间                                       ： 5月 11, 2013
密码失效时间                                       ：从不
帐户过期时间                                       ：从不
两次改变密码之间相距的最小天数          ：0
两次改变密码之间相距的最大天数          ：60
在密码过期之前警告的天数                    ：9
```

然后通过如下命令设置密码失效时间：

```
[root@linuxde ~]# chage -I 5 root
[root@linuxde ~]# chage -l root
最近一次密码修改时间                          ： 3月 12, 2013
密码过期时间                                  ： 5月 11, 2013
密码失效时间                                  ： 5月 16, 2013
帐户过期时间                                  ：从不
两次改变密码之间相距的最小天数          ：0
两次改变密码之间相距的最大天数          ：60
在密码过期之前警告的天数                 ：9
```

从上述命令可以看到，在密码过期后5天，密码自动失效，这个用户将无法登陆系统了。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hezhiqiang.gitbook.io/linux/ming-ling/chage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
