CentOS7/RHEL7 开始使用pam_pwquality模块进行密码复杂度策略的控制管理。pam_pwquality替换了原来Centos6/RHEL6中的pam_cracklib模块,并向后兼容。
使用pam_pwquality模块设置密码复杂度
可以通过两种方式实现:

  • 1、直接指定pam_pwquality模块参数,即在/etc/pam.d/system-auth中的“password requisite pam_pwquality.so”行尾添加具体参数,比如“minlen=16 ucredit=-1 lcredit=-1 ocredit=-1 dcredit=-1”,表示最小密码长度16位,数字,大小写字母,特殊字符均至少包含1位。

  • 2、通过修改/etc/security/pwquality.conf参数文件,定义密码复杂度规则。修改pwquality.conf参数文件有2种方法,可以参考“CentOS7 设置密码规则” by shaonbean。

       (1)直接vi或vim编辑器 ,或者甚至用sed命令,修改/etc/security/pwquality.conf
    

       (2)使用authconfig命令修改,修改后最终会体现在/etc/security/pwquality.conf文件中
    

    设置最小密码长度。
    用户不能设置小于此参数的密码长度。

 # set 8 for minimum password length

[root@linuxprobe~]# authconfig --passminlen=8 --update
# the parameter is set in a config below

[root@linuxprobe~]# grep "^minlen" /etc/security/pwquality.conf
minlen = 8 

# 在新密码中设置同一类的允许连续字符的最大数目
 # set 4 for maximum number of allowed consecutive characters of the same class

[root@linuxprobe~]# authconfig --passmaxclassrepeat=4 --update
# the parameter is set in a config below

[root@linuxprobe~]# grep "^maxclassrepeat" /etc/security/pwquality.conf
maxclassrepeat = 4 

# 在新密码中至少需要一个小写字符。
 [root@linuxprobe~]# authconfig --enablereqlower --update
# the parameter is set in a config below
# (if you'd like to edit the value, edit it with vi and others)

[root@linuxprobe~]# grep "^lcredit" /etc/security/pwquality.conf
lcredit = -1 

# 在新密码中至少需要一个大写字符
 [root@linuxprobe~]# authconfig --enablerequpper --update
# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^ucredit" /etc/security/pwquality.conf
ucredit = -1 

# 在新密码中至少需要一个数字
 [root@linuxprobe~]# authconfig --enablereqdigit --update
# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^dcredit" /etc/security/pwquality.conf
dcredit = -1 

# 密码包括至少一个特殊字符
 [root@linuxprobe~]# authconfig --enablereqother --update
# the parameter is set in a config below

# (if you'd like to edit the value, edit it with vi and others)
[root@linuxprobe~]# grep "^ocredit" /etc/security/pwquality.conf
ocredit = -1 

# 在新密码中设置单调字符序列的最大长度。 (ex⇒'12345','fedcb')
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
maxsequence = 3 

# 设置新密码中不能出现在旧密码中的字符数
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
difok = 5 

# 检查来自用户passwd条目的GECOS字段的长度超过3个字符的字是否包含在新密码中。
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
gecoscheck = 1 

# 设置不能包含在密码中的Ssace分隔的单词列表
 [root@linuxprobe~]# vi /etc/security/pwquality.conf
# add to the end
badwords = denywords1 denywords2 denywords3 

# 为新密码设置hash / crypt算法。 (默认为sha512)
# show current algorithm

[root@linuxprobe~]# authconfig --test | grep hashing

password hashing algorithm is md5
# chnage algorithm to sha512

[root@linuxprobe~]# authconfig --passalgo=sha512 --update
[root@linuxprobe~]# authconfig --test | grep hashing
password hashing algorithm is sha512

centos7如何继续使用pam_cracklib模块检验密码复杂度

由于Centos7默认取消了对pam_cracklib模块的使用,配置文件/etc/pam.d/system-auth中没有pam_cracklib.so相关的条目。

有同学直接在/etc/pam.d/system-auth文件末尾添加“password    requisite     pam_cracklib.so try_first_pass retry=3 type=  minlen=16 ucredit=-1 lcredit=-1 ocredit=-1 dcredit=-1” ,然后发现实际上密码复杂度策略并没有生效。

经测试是因为位置不对,把上面的语句添加在“pam_pwquality.so”所在行的前面,即可使策略生效。如下所示:


centos7如何继续使用pam_cracklib模块检验密码复杂度

1、设置密码过期的天数。 用户必须在几天内更改密码。 此设置仅在创建用户时才会产生影响,而不会影响到现有用户。 如果设置为现有用户,请运行命令“chage -M(days)(user)”

[root@shaonbean ~]# vi /etc/login.defs
# line 25: set 60 for Password Expiration
PASS_MAX_DAYS 60

2、设置可用密码的最短天数。 至少在改变它之后,用户必须至少使用他们的密码。 此设置仅在创建用户时才会产生影响,而不会影响到现有用户。 如果设置为现有用户,请运行命令“chage -m(days)(user)”

[root@shaonbean ~]# vi /etc/login.defs
# line 26: set 2 for Minimum number of days available
PASS_MIN_DAYS 2

3、在到期前设置警告的天数。 此设置仅在创建用户时才会产生影响,而不会影响到现有用户。 如果设置为存在用户,请运行命令“chage -W(days)(user)”

[root@shaonbean ~]# vi /etc/login.defs
# line 28: set 7 for number of days for warnings
PASS_WARN_AGE 7

4、设置密码最小长度 PASS_MIN_LEN
PASS_MIN_LEN 12