在本指南中,我们将学习如何在基于CentOS 7 / RHEL的派生产品上实施密码复杂性策略。我们之前的指南涵盖了在Ubuntu 18.04上实施密码复杂性。您可以通过以下链接进行检查;
在CentOS 7上实施密码复杂度策略
与我们之前的指南类似,我们将使用PAM pwquality模块在基于CentOS 7 / RHEL的派生产品上实施密码复杂性策略。
在基于Ubuntu或Debian的衍生产品中,我们修改了 /etc/pam.d/common-password配置文件。对于CentOS 7或类似的衍生产品,使用/etc/security/pwquality.conf或/etc/pam.d/system-auth配置文件。
通常,在进行更改之前先备份配置文件,以防万一。
cp /etc/security/pwquality.conf /etc/security/pwquality.conf.original
cp /etc/pam.d/system-auth /etc/pam.d/system-auth.original
打开配置文件进行编辑。
vim /etc/pam.d/system-auth
找到包含pam_pwquality.so模块的行;
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=
注释该行并替换为下面的行;
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=8 lcredit=-1 ucredit=-1 dcredit=-1 ocredit=-1 enforce_for_root
说明:
- minlen=8–将密码的最小长度设置为8个字符。
- lcredit=-1 -将密码应包含的小写字母的最小数目设置为至少一个
- ucredit=-1-将密码上的大写字母的最小数目设置为至少一个。
- dcredit=-1 –将密码中包含的最小位数至少设置为一个
- ocredit=-1–设置其他符号的最小数量,例如@,#、! $%等,至少要有一个密码
- enforce_for_root –确保即使是root用户设置密码,也应强制执行复杂性策略。
您还可以使用authconfig如下所示的命令行实用程序来实现相同的目的;
authconfig --enablereqlower --enablerequpper --enablereqdigit --enablereqother --passminlen=8 --passmaxrepeat=3 --update
上面的命令基本上可以确保密码至少具有(按相应的顺序);
- 一个小写字母
- 一个大写字母
- 一位数
- 字母数字字符。
- 长度为8个字符
- 与上一个密码类似,最多3个字符。
更改将在更新/etc/security/pwquality.conf。
tail /etc/security/pwquality.conf
# Path to the cracklib dictionaries. Default is to use the cracklib default.
# dictpath =
minlen = 8
minclass = 1
maxrepeat = 3
maxclassrepeat = 0
lcredit = -1
ucredit = -1
dcredit = -1
ocredit = -1
请注意,root用户或具有sudo权限的任何用户都可以始终设置任何密码,而与强制策略无关。但是,为确保密码复杂度策略同时适用于sudo的root用户和用户,您必须将该enforce_for_root选项附加到上的以下行/etc/pam.d/system-auth。
password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= enforce_for_root
测试密码执行策略
以root用户身份,尝试使用不符合设置的凭据的密码更改用户的密码。
[root@Cent7 ~]# passwd amos
Changing password for user amos.
New password: @moskifaru1
BAD PASSWORD: The password contains less than 1 uppercase letters
New password: @mosKifaru
BAD PASSWORD: The password contains less than 1 digits
New password: mosKifaru1
BAD PASSWORD: The password contains less than 1 non-alphanumeric characters
passwd: Have exhausted maximum number of retries for service
使用更复杂的密码进行测试;@ mosKifaru1
[root@Cent7 ~]# passwd amos
Changing password for user amos.
New password: @mosKifaru1
Retype new password: @mosKifaru1
passwd: all authentication tokens updated successfully.
这就是如何在CentOS 7上实施密码复杂性策略。