ubuntu22.04 设置密码复杂度

密码复杂度策略

安装密码质量检查库

# apt -y install libpam-pwquality

设置密码过期的天数

用户必须在几天内更改密码。
此设置仅在创建用户时产生影响,不会影响现有用户。
如果设置为现有用户,请运行命令 [chage -M (days) (user)]。

查看用户密码有效期,请运行命令 chage -l user

# vi /etc/login.defs
# 设置密码到期天数(下面的示例表示 60 天)
PASS_MAX_DAYS 90

# 可用的最少天数(以下示例表示 1 天)
PASS_MIN_DAYS 1

# 设置到期警告天数(下面的示例表示 7 天)
PASS_WARN_AGE 7

限制使用过去使用过的密码

# vi /etc/pam.d/common-password
# add [remember=*] (example below means 5 gen)
password        [success=1 default=ignore]      pam_unix.so obscure use_authtok try_first_pass sha512 remember=5

设置最小密码长度

# vim /etc/security/pwquality.conf
# 设置最小长度(下面的示例表示 8 个字符)
minlen = 8

# 设置新密码所需的最少字符类别数 (大写/小写/数字/其他)
minclass = 2

# 设置新密码中允许的最大连续相同字符数
maxrepeat = 2

# 设置新密码中允许的同类最大连续字符数
maxclassrepeat = 4

# 要求新密码中至少有一个小写字符
lcredit =-1

# 要求新密码中至少有一个大写字符
ucredit = -1

# 新密码至少需要一位数字
dcredit = -1

# 新密码中至少需要一个其他字符
ocredit = -1

# 设置新密码中单调字符序列的最大长度
maxsequence = 2

# 设置新密码中不能出现在旧密码中的字符数
difok = 5

# 检查新密码中是否包含用户密码条目的GECOS字段中超过3个字符的单词
gecoscheck = 1

# 设置密码中不得包含的以空格分隔的单词列表
badwords = denywords1 denywords2 denywords3

设置登录超时时间

系统登录超时

# vim /etc/profile
export TMOUT=3600    # 表示超时时间为1小时

远程连接超时

# vim /etc/ssh/sshd_config
ClientAliveInterval 600
ClientAliveCountMax 0

禁用root远程登录

# vim /etc/ssh/sshd_config
PermitRootLogin prohibit-password

登录失败锁定

创建faillock目录

# mkdir /var/run/faillock

faillock.conf

# egrep -v "^$|^#" /etc/security/faillock.conf
dir = /var/run/faillock
audit
deny = 3
fail_interval = 900
unlock_time = 600
even_deny_root
root_unlock_time = 900

PAM 设置

common-auth

# egrep -v "^$|^#" /etc/pam.d/common-auth
auth	[success=1 default=ignore]	pam_unix.so nullok
auth     [default=die]      pam_faillock.so authfail
auth     sufficient         pam_faillock.so authsucc
auth	requisite			pam_deny.so
auth	required			pam_permit.so
auth	optional			pam_cap.so

common-account

# egrep -v "^$|^#" /etc/pam.d/common-account
account	[success=1 new_authtok_reqd=done default=ignore]	pam_unix.so 
account	requisite			pam_deny.so
account	required			pam_permit.so
account required            pam_faillock.so

锁定用户

测试用户

# ssh wgs@172.16.18.31
ubuntu@172.16.18.31's password: 
Permission denied, please try again.
ubuntu@172.16.18.31's password: 
Permission denied, please try again.
ubuntu@172.16.18.31's password: 
Permission denied (publickey,password).

测试结果

# faillock
wgs-01:
When                Type  Source                                           Valid     # v表示有效,i表示无效
2023-03-30 15:08:11 RHOST 192.168.0.189                                        V
2023-03-30 15:08:18 RHOST 192.168.0.189                                        V
2023-03-30 15:08:22 RHOST 192.168.0.189                                        V
2023-03-30 15:08:28 RHOST 192.168.0.189                                        V

 用户锁定期间输入正确的密码也无法登录

解锁用户

# faillock --user wgs-01 --reset
# faillock 
wgs-01:
When                Type  Source                                           Valid

参考文档

https://www.server-world.info/en/note?os=Ubuntu_22.04&p=pam&f=1

faillock文档: https://manpages.ubuntu.com/manpages/impish/man8/pam_faillock.8.html#examples

posted @ 2023-03-27 17:03  小吉猫  阅读(3400)  评论(0编辑  收藏  举报