作者:【吴业亮】云计算开发工程师
博客:http://blog.csdn.net/wylfengyujiancheng
一、设置密码规则
1、密码长度、有效期
/etc/login.defs文件是当创建用户时的一些规划,比如创建用户时,是否需要家目录,UID和GID的范围;用户的期限等等,这个文件是可以通过root来定义的
PASS_MAX_DAYS 90 —-两次改变密码之间相距的最大天数,密码有效最大天数
PASS_MIN_DAYS 6 —-两次改变密码之间相距的最小天数,为零时代表任何时候都可以更改密码
PASS_MIN_LEN 6 —-密码最小长度
PASS_WARN_AGE 30 —-在密码过期之前警告的天数
注意:以上只对之后新增的用户有效,如果要修改已存在的用户密码规则,需要使用chage命令
2、查看用户的密码规则
chage -l test
最近一次密码修改时间 : 6月 08, 2017
密码过期时间 : 9月 06, 2017
密码失效时间 :从不
帐户过期时间 :从不
两次改变密码之间相距的最小天数 :6
两次改变密码之间相距的最大天数 :90
在密码过期之前警告的天数 :30
3、修改方法:
# chage -M 90 -m 6 -W 30 test
注意:不要用该命令给root用户加上有效期,如果密码过期,再加上后文说的/etc/shadow文件加锁禁止修改,会导致root提示修改密码,无法成功修改密码,从而无法登陆。
如果要修改密码过期时间为“从不”
# chage -M 99999 test
4、设置密码最大使用时间
[root@server ~]# vi /etc/login.defs
PASS_MAX_DAYS 60
5、两次改变密码之间相距的最小天数
[root@server ~]# vi /etc/login.defs
PASS_MIN_DAYS 2
6、在密码过期之前警告的天数
[root@server ~]# vi /etc/login.defs
PASS_WARN_AGE 7
7、五次更改密码不能有重复,并且每次修改密码都会将历史密码记录在/etc/security/opasswd文件中
[root@server ~]# vi /etc/pam.d/system-auth
password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=5
8、设置最小的密码长度为8
[root@server ~]# authconfig --passminlen=8 --update
查看配置结果
[root@server ~]# grep "^minlen" /etc/security/pwquality.conf
minlen = 8
9、设置密码必须满足同时有两种特殊字符(大写、小写、数字、特殊字符)
[root@server ~]# authconfig --passminclass=2 --update
查看配置结果
[root@server ~]# grep "^minclass" /etc/security/pwquality.conf
minclass = 2
10、设置密码中只允许两个连续的字符相同
[root@server ~]# authconfig --passmaxrepeat=2 --update
查看配置结果
[root@server ~]# grep "^maxrepeat" /etc/security/pwquality.conf
maxrepeat = 2
11、在新密码中设置同一类的允许连续字符的最大数目
[root@server ~]# authconfig --passmaxclassrepeat=4 --update
查看配置结果
[root@server ~]# grep "^maxclassrepeat" /etc/security/pwquality.conf
maxclassrepeat = 4
12、在新密码中至少需要一个小写字符。
[root@server ~]# authconfig --enablereqlower --update
查看配置结果
[root@server ~]# grep "^lcredit" /etc/security/pwquality.conf
lcredit = -1
13、在新密码中至少需要一个大写字符
[root@server ~]# authconfig --enablerequpper --update
查看配置结果
[root@server ~]# grep "^ucredit" /etc/security/pwquality.conf
ucredit = -1
14、在新密码中至少需要一个数字
[root@server ~]# authconfig --enablereqdigit --update
查看配置结果
[root@server ~]# grep "^dcredit" /etc/security/pwquality.conf
dcredit = -1
15、密码包括至少一个特殊字符
查看配置结果
[root@server ~]# grep "^ocredit" /etc/security/pwquality.conf
ocredit = -1
16、在新密码中设置单调字符序列的最大长度
[root@server ~]# vi /etc/security/pwquality.conf
#文件末尾新增
maxsequence = 3
17、设置新密码中不能出现在旧密码中的字符数
[root@server ~]# vi /etc/security/pwquality.conf
#文件末尾新增
difok = 5
18、检查来自用户passwd条目的GECOS字段的长度超过3个字符的字是否包含在新密码中。
[root@server ~]# vi /etc/security/pwquality.conf
#文件末尾新增
gecoscheck = 1
19、设置不能包含在密码中的Ssace分隔的单词列表
[root@server ~]# vi /etc/security/pwquality.conf
#文件末尾新增
badwords = denywords1 denywords2 denywords3
20、为新密码设置hash / crypt算法。 (默认为sha512)
[root@server ~]# authconfig --test | grep hashing
password hashing algorithm is md5
#修改为sha512
[root@server ~]# authconfig --passalgo=sha512 --update
[root@server ~]# authconfig --test | grep hashing
password hashing algorithm is sha512
二、账户锁定策略实现
策略要求如下:
设定锁定的阈值为5次
锁定时间为5分钟即300秒
必须所有用户都受限,包括root
1、修改配置文件/etc/pam.d/system-auth-ac,写入策略
root@server ~]# vi /etc/pam.d/system-auth-ac
auth required pam_env.so
auth required pam_tally2.so even_deny_root deny=5 unlock_time=60
auth sufficient pam_unix.so nullok try_first_pass
auth requisite pam_succeed_if.so uid >= 1000 quiet_success
auth required pam_deny.so
account required pam_unix.so
account required pam_tally2.so
account sufficient pam_localuser.so
account sufficient pam_succeed_if.so uid < 1000 quiet
account required pam_permit.so
2、修改配置文件/etc/pam.d/password-auth-ac
[root@server ~]# vi /etc/pam.d/password-auth-ac
auth required pam_env.so
auth required pam_tally2.so deny=5 unlock_time=60
auth sufficient pam_unix.so nullok try_first_pass
auth requisite pam_succeed_if.so uid >= 1000 quiet_success
auth required pam_deny.so
account required pam_unix.so
account required pam_tally2.so
account sufficient pam_localuser.so
account sufficient pam_succeed_if.so uid < 1000 quiet
account required pam_permit.so
3、查看用户锁定状态
[root@ server pam.d]# pam_tally2 -u wyl
Login Failures Latest failure From
wyl 7 12/20/16 14:02:55 192.168.10.86
4、解锁状态
[root@ server ~]# pam_tally2 -r -u wyl
Login Failures Latest failure From
wyl 0
参考:
https://www.server-world.info/en/note?os=CentOS_7&p=password