---
title: 确保口令复杂度设置正确
description: "若系统中口令设置过于简单，容易被猜测或暴力破解。比如，过短的密码、纯数字或纯字母组合容易成为攻击的目标。在系统设置口令时，应强制用户使用复杂口令规则。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengaiagent/bestpractice/aiagent-0031.html
sourcePath: /source/zh/kunpengaiagent/bestpractice/aiagent-0031.html
indexId: 87fc0172471ecde7e548b9a2c08f015e7da3bcfa6821b3147e5c77460363931656
---
# 确保口令复杂度设置正确

若系统中口令设置过于简单，容易被猜测或暴力破解。比如，过短的密码、纯数字或纯字母组合容易成为攻击的目标。在系统设置口令时，应强制用户使用复杂口令规则。

对于安全要求较高的业务场景，可参考业界最佳实践进行设置，例如：口令长度设置为14位及以上，且密码需满足包含大写字母、小写字母、数字以及特殊字符至少各一种，确保口令不会被轻易破解。

#### 口令复杂度要求

openEuler口令复杂度如下：

- 口令长度至少8个字符。
- 口令必须包含如下至少3种字符的组合：

  - 至少一个小写字母。
  - 至少一个大写字母。
  - 至少一个数字。
  - 至少一个特殊字符：`~!@#$%^&*()-_=+|[{}];:'",<.>/?和空格。

考虑到在不同场景下的易用性，openEuler默认不配置enforce_for_root和retry值，请根据实际场景按需配置。


#### 操作步骤

1. 检查口令复杂度的配置情况。

  - 方式一：“/etc/pam.d/system-auth”和“/etc/pam.d/password-auth”分别提供该功能项的配置，不同应用程序或者服务对应的配置项，需根据各自include的配置文件而定。

```
grep system-auth /etc/pam.d/ -r
```


    以下仅展示部分输出结果，可以看出，login和sudo的账号认证采用“/etc/pam.d/system-auth”文件中的配置，后续将以该文件为例进行说明。

```
/etc/pam.d/login:auth       substack     system-auth
/etc/pam.d/login:account    include      system-auth
/etc/pam.d/login:password   include      system-auth
/etc/pam.d/login:session    include      system-auth
/etc/pam.d/sudo:auth       include      system-auth
/etc/pam.d/sudo:account    include      system-auth
/etc/pam.d/sudo:password   include      system-auth
/etc/pam.d/sudo:session    include      system-auth-su
```


    在“/etc/pam.d/system-auth”文件中检查口令复杂度的配置情况。

```
grep pam_pwquality /etc/pam.d/system-auth
```


    如下为示例。

```
password    requisite     pam_pwquality.so minlen=8 minclass=3 enforce_for_root try_first_pass local_users_only retry=3 dcredit=0 ucredit=0 lcredit=0 ocredit=0
```


  - 方式二：在“/etc/security/pwquality.conf”文件中检查口令复杂度的配置情况。

```
cat /etc/security/pwquality.conf
```


    如下为示例，仅列举本规范关注的配置项。

```
minlen=8
minclass=3
retry=3
dcredit=0
ucredit=0
lcredit=0
ocredit=0
enforce_for_root
```


**表1 参数说明**

| 配置项 | 说明 |
| --- | --- |
| minlen=8 | 口令长度至少包含8个字符。 建议配置更长的口令最小长度。 |
| minclass=3 | 口令至少包含大写字母、小写字母、数字和特殊字符中的任意3种。 |
| ucredit=0 | 口令包含任意个大写字母。 |
| lcredit=0 | 口令包含任意个小写字母。 |
| dcredit=0 | 口令包含任意个数字。 |
| ocredit=0 | 口令包含任意个特殊字符。 |
| retry=3 | 每次修改最多可以尝试3次。 |
| enforce\_for\_root | 本设置对root账号同样有效。 |


2. 若需要修改口令复杂度要求，请按照如下命令操作。

  - 方式一：通过修改“/etc/pam.d/password-auth”和“/etc/pam.d/system-auth”文件实现。

    a. 以“/etc/pam.d/system-auth”文件为例，打开配置文件。

```
vim /etc/pam.d/system-auth
```


    b. 按“i”进入编辑模式，配置如下字段。

```
password    requisite     pam_pwquality.so minlen=8 minclass=3 enforce_for_root try_first_pass local_users_only retry=3 dcredit=0 ucredit=0 lcredit=0 ocredit=0
```


    c. 按“Esc”键退出编辑模式，输入:wq!，按“Enter”键保存并退出文件。
  - 方式二：通过修改“/etc/security/pwquality.conf”文件实现。

    a. 打开配置文件。

```
vim /etc/security/pwquality.conf
```


    b. 按“i”进入编辑模式，配置如下字段。

```
minlen=8
minclass=3
retry=3
dcredit=0
ucredit=0
lcredit=0
ocredit=0
enforce_for_root
```


    c. 按“Esc”键退出编辑模式，输入:wq!，按“Enter”键保存并退出文件。
