Skip to content

Eternal Center

  • System (系统)
  • Services (服务)
  • Databases (数据库)
  • Clusters (集群)
  • Big Data (大数据)
  • Cloud Computing (云计算)
  • Languages (语言)
  • Project (项目)
  • Writings (写作)
  • Eternity (永恒)
  • News (消息)
  • Chronicle (编年史)
  • Words (言)
Posted on January 1, 2022April 29, 2022 by Mingyu Zhu

[步骤] Linux 本地和 SSH 输错密码次数的限制 (pam_faillock 版) (authselect 版) (CentOS Linux 8 & RHEL 8 版)

步骤目录:

步骤一:了解背景

步骤二:设置 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
CategoriesChinese (中文), System (系统), System Login Security (系统登录安全), System Network & System Security & System Log (系统网络 & 系统安全 & 系统日志), System Operation & System Setting & System Software (系统操作 & 系统设置 & 系统软件), System Security (系统安全), System Setting (系统设置), System User (系统用户), System User & System Privilege (系统用户 & 系统权限)

Post navigation

Previous PostPrevious [步骤] GitHub 代码的更新 (令牌版)
Next PostNext [消息] Rocky Linux 和 AlmaLinux 可以用于代替 CentOS Linux

Aspiration (愿景):

Everyone can achieve self achievement and self happiness fairly

每个人都能公平地实现自我成就和自我幸福

Position (位置):

Running on Evolution Host and DigitalOcean

正在 Evolution Host 和 DigitalOcean 上运行

Logo (徽标):

Additional Information (其他信息):

About Manual Clone Contact Disclaimer Donation Friendly Links 关于 说明书 克隆 联系 免责申明 捐赠 友情链接

Standby IP Address (备用 IP 地址):

152.69.204.95  150.230.63.10  Please configure before use / 请先配置再使用

Search Outside Website (站外搜索):

Google Wikipedia Bing
Proudly powered by LNMP Proudly powered by WordPress