步骤目录:
步骤一:了解背景
步骤二:设置 SSH 的登录限制
2.1 在 sshd 中开启 UsePAM
2.1.1 修改 sshd 的配置文件
2.1.2 让修改的 sshd 配置文件生效
2.2 确保 /etc/pam.d/sshd 包含 password-auth
步骤三:检查是否选择了 authselect 自定义认证
步骤四:配置认证
4.1 如果 authselect 自定义认证存在
4.1.1 修改 system-auth 文件
4.1.2 修改 password-auth 文件
4.2 如果 authselect 自定义认证不存在
4.2.1 生成新的自定义认证
4.2.1.1 备份当前的自定义认证
4.2.1.2 创建新的自定义认证
4.2.1.3 选择自定义认证
4.2.1.4 显示当前选择的自定义认证
4.2 修改自定义认证
4.2.1 修改 system-auth 文件
4.2.2 修改 password-auth 文件
步骤五:让配置的认证生效
步骤六:管理远程登录密码输错次数的用户
6.1 显示某个用户近期输错了几次密码
6.2 重制所有远程登录密码输错次数
具体的操作步骤:
步骤一:了解背景
从 CentOS Linux 8 & RHEL 8 开始,系统的身份验证模块从 CentOS Linux 7 & RHEL 7 的 pam_tally2 换成了 pam_faillock
步骤二:设置 SSHd 的登录限制
2.1 在 sshd 中开启 UsePAM
2.1.1 修改 sshd 的配置文件
# vim /etc/ssh/sshd_config
将以下内容:
......
#UsePAM no
......
修改为:
......
UsePAM yes
......
2.1.2 让修改 sshd 配置文件生效
# systemctl restart sshd
2.2 确保 /etc/pam.d/sshd 包含 password-auth
# cat /etc/pam.d/sshd | grep password-auth
auth substack password-auth
account include password-auth
password include password-auth
session include password-auth
(注意:如果输出结果中不包含这 4 条内容则需要手动添加)
步骤三:检查是否选择了 authselect 自定义认证
# authselect current | awk 'NR == 1 {print $3}' | grep custom/
custom/password-policy
(
补充:
(1)如果这条命令里没有输出则代表没有选择自定义认证
(2)从这里的输出结果可以看出这里选择的自定义认证是 custom/password-policy
)
步骤四:配置认证
4.1 如果 authselect 自定义认证存在
4.1.1 修改 system-auth 文件
# vim /etc/authselect/custom/password-policy/system-auth
将以下内容:
......
auth required pam_faillock.so preauth silent {include if "with-faillock"}
......
auth required pam_faillock.so authfail {include if "with-faillock"}
......
修改为:
......
auth required pam_faillock.so preauth silent audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
auth required pam_faillock.so authfail audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
(补充:这里以包括 root 用户每使用密码 ssh 远程登录失败 6 次则被锁定 180 秒为例)
4.1.2 修改 password-auth 文件
# vim /etc/authselect/custom/password-policy/password-auth
将以下内容:
......
auth required pam_faillock.so preauth silent {include if "with-faillock"}
......
auth required pam_faillock.so authfail {include if "with-faillock"}
......
修改为:
......
auth required pam_faillock.so preauth silent audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
auth required pam_faillock.so authfail audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
(补充:这里以包括 root 用户每使用密码 ssh 远程登录失败 6 次则被锁定 180 秒为例)
4.2 如果 authselect 自定义认证不存在
4.2.1 生成新的自定义认证
4.2.1.1 备份当前的自定义认证
# authselect apply-changes -b --backup=sssd.backup
(补充:这里以创建 sssd.backup 备份文件为例)
4.2.1.2 创建新的自定义认证
# authselect create-profile password-policy -b sssd --symlink-meta --symlink-pam
(补充:这里以生成名为 password-policy 的自定义认证为例)
4.2.1.3 选择自定义认证
# authselect select custom/password-policy with-sudo with-faillock without-nullok with-mkhomedir
(
补充:
1) 这里以选择名为 password-policy 的自定义认证为例
2) 这里设置了 with-sudo、with-faillock、without-nullok 和 with-mkhomedir 参数
)
4.2.1.4 显示当前选择的自定义认证
# authselect current
(补充:这里以生成并选择名为 password-policy 的自定义认证为例)
4.2 修改自定义认证
4.2.1 修改 system-auth 文件
# vim /etc/authselect/custom/password-policy/password-auth
将以下内容:
......
auth required pam_faillock.so preauth silent {include if "with-faillock"}
......
auth required pam_faillock.so authfail {include if "with-faillock"}
......
修改为:
......
auth required pam_faillock.so preauth silent audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
auth required pam_faillock.so authfail audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
(补充:这里以包括 root 用户每使用密码 ssh 远程登录失败 6 次则被锁定 180 秒为例)
4.2.2 修改 password-auth 文件
# vim /etc/authselect/custom/password-policy/password-auth
将以下内容:
......
auth required pam_faillock.so preauth silent {include if "with-faillock"}
......
auth required pam_faillock.so authfail {include if "with-faillock"}
......
修改为:
......
auth required pam_faillock.so preauth silent audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
auth required pam_faillock.so authfail audit even_deny_root deny=6 unlock_time=180 {include if "with-faillock"}
......
(补充:这里以包括 root 用户每使用密码 ssh 远程登录失败 6 次则被锁定 180 秒为例)
步骤五:让配置的认证生效
# authselect apply-changes
(注意:此步骤会刷新 /etc/authselect/system-auth 文件和 /etc/authselect/password-auth 文件)
步骤六:管理远程登录密码输错次数的用户
6.1 显示某个用户近期输错了几次密码
# faillock --user root
(补充:这里以显示 root 用户近期输错了几次密码为例)
6.2 重制所有远程登录密码输错次数
# faillock --reset