Skip to content

Eternal Center

  • Single Node (单节点)
    • System (系统)
    • Service (服务)
    • Database (数据库)
    • Container (容器)
    • Virtualization (虚拟化)
  • Multi Node (多节点)
    • Cluster (集群)
    • Big Data (大数据)
    • Cloud Computing (云计算)
    • Batch Processing (批量处理)
  • Other (其它)
    • Ideas (思路)
    • Language (语言)
    • Project (项目)
  • Eternity (永恒)
    • Creations (创作)
    • Classics (经典)
    • Chronicle (编年史)
    • News (消息)
Posted on January 1, 2022September 7, 2022 by Mingyu Zhu

[步骤] Linux 密码的安全 (本地和 SSH 输错密码次数的限制) (pam_faillock 版) (自定义配置文件版) (Rocky Linux 8 & RHEL 8 版)

  • 正文:
    • 步骤一:了解背景
    • 步骤二:让 sshd 使用可插入身份验证模块
      • 2.1 修改 sshd 配置文件
      • 2.2 让修改 sshd 配置文件生效
    • 步骤三:让本地登录和 sshd 登录使用密码认证
      • 3.1 确认 /etc/pam.d/login 配置文件
      • 3.2 确认 /etc/pam.d/sshd 配置文件
    • 步骤四:通过自定义配置文件使用 pam_faillock 模块
      • 4.1 检查是否选择了自定义配置文件
        • 4.1.1 检查是否选择了自定义配置文件
        • 4.1.2 检查选择的自定义配置文件是否生效
      • 4.2 如果自定义配置文件存在
        • 4.2.1 在 /etc/authselect/custom/password-policy/system-auth 配置文件中添加 pam_faillock.so 模块和相关参数 (不建议)
        • 4.2.2 在 /etc/authselect/custom/password-policy/system-auth 配置文件中设置登录失败几次后提示登陆失败
        • 4.2.3 在 /etc/authselect/custom/password-policy/password-auth 配置文件中添加 pam_faillock.so 模块和相关参数 (不建议)
        • 4.2.4 在 /etc/authselect/custom/password-policy/password-auth 配置文件中设置登录失败几次后提示登陆失败
      • 4.3 如果自定义配置文件不存在
        • 4.3.1 生成新的自定义配置文件
        • 4.3.1.1 备份当前的自定义配置文件
        • 4.3.1.2 创建新的自定义配置文件
        • 4.3.1.3 选择新的自定义配置文件
        • 4.3.1.3.1 选择新的自定义配置文件
        • 4.3.1.3.2 满足选择新的自定义配置文件时 with-mkhomedir 参数的要求
        • 4.3.1.4 显示当前选择的自定义配置文件
        • 4.3.2 修改自定义配置文件
        • 4.3.2.1 在 /etc/authselect/custom/password-policy/system-auth 配置文件中添加 pam_faillock.so 模块和相关参数  (不建议)
        • 4.3.2.2 在 /etc/authselect/custom/password-policy/system-auth 配置文件中设置登录失败几次后提示登陆失败
        • 4.3.2.3 在 /etc/authselect/custom/password-policy/password-auth 配置文件中添加 pam_faillock.so 模块和相关参数 (不建议)
        • 4.3.2.4 在 /etc/authselect/custom/password-policy/password-auth 配置文件中设置登录失败几次后提示登陆失败
      • 4.4 让自定义配置文件生效
    • 步骤五:修改 /etc/security/faillock.conf 配置文件
    • 步骤六:用户登录失败的管理
      • 6.1 显示某个用户近期输错了几次密码
      • 6.2 重制远程登录密码输错次数
        • 6.2.1 重制某用户远程登录密码输错次数
        • 6.2.2 重制所有用户远程登录密码输错次数
    • 步骤七:管理自定义配置文件
      • 7.1 在自定义配置文件中禁用 pam_faillock.so 模块
      • 7.2 在自定义配置文件中启用 pam_faillock.so 模块
  • 参考文献:

正文:

步骤一:了解背景

从 Rocky Linux 8 & RHEL 8 开始,系统的身份验证模块从 CentOS Linux 7 & RHEL 7 的 pam_tally2 换成了 pam_faillock

步骤二:让 sshd 使用可插入身份验证模块

2.1 修改 sshd 配置文件

# vim /etc/ssh/sshd_config

将以下内容:

......
#UsePAM no
......

修改为:

......
UsePAM yes
......

2.2 让修改 sshd 配置文件生效

# systemctl restart sshd

步骤三:让本地登录和 sshd 登录使用密码认证

3.1 确认 /etc/pam.d/login 配置文件

# cat /etc/pam.d/login | grep system-auth

确保包含以下内容:

auth       substack     system-auth
account    include      system-auth
password   include      system-auth
session    include      system-auth

3.2 确认 /etc/pam.d/sshd 配置文件

# cat /etc/pam.d/sshd | grep password-auth

确保包含以下内容:

auth       substack     password-auth
account    include      password-auth
password   include      password-auth
session    include      password-auth

步骤四:通过自定义配置文件使用 pam_faillock 模块

4.1 检查是否选择了自定义配置文件

4.1.1 检查是否选择了自定义配置文件
# authselect current | awk 'NR == 1 {print $3}' | grep custom/
custom/password-policy

(补充:从这里显示的结果可以看出这里选择的自定义配置文件是 custom/password-policy ,如果没有输出则代表没有选择自定义配置文件)

4.1.2 检查选择的自定义配置文件是否生效
# authselect check
Current configuration is valid.

(补充:从这里显示的结果可以看出自定义配置文件是生效的)

4.2 如果自定义配置文件存在

4.2.1 在 /etc/authselect/custom/password-policy/system-auth 配置文件中添加 pam_faillock.so 模块和相关参数 (不建议)
# 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 秒为例)

(注意:不建议执行此步骤,执行后正常的 SFTP 登陆会被视为登陆失败,不过正常的 SSH 登录不受影响)

4.2.2 在 /etc/authselect/custom/password-policy/system-auth 配置文件中设置登录失败几次后提示登陆失败
# vim /etc/authselect/custom/password-policy/system-auth

将以下内容:

......
password    requisite                                    pam_pwquality.so ......
......

修改为:

......
password    requisite                                    pam_pwquality.so try_first_pass local_users_only enforce-for-root retry=3 ......
......

(补充:这里以登录失败 3 次后提示登录失败为例)

4.2.3 在 /etc/authselect/custom/password-policy/password-auth 配置文件中添加 pam_faillock.so 模块和相关参数 (不建议)
# 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 秒为例)

(注意:不建议执行此步骤,执行后正常的 SFTP 登陆会被视为登陆失败,不过正常的 SSH 登录不受影响)

4.2.4 在 /etc/authselect/custom/password-policy/password-auth 配置文件中设置登录失败几次后提示登陆失败
# vim /etc/authselect/custom/password-policy/password-auth

将以下内容:

......
password    requisite                                    pam_pwquality.so ......
......

修改为:

......
password    requisite                                    pam_pwquality.so try_first_pass local_users_only enforce-for-root retry=3 ......
......

(补充:这里以登录失败 3 次后提示登录失败为例)

4.3 如果自定义配置文件不存在

4.3.1 生成新的自定义配置文件
4.3.1.1 备份当前的自定义配置文件
# authselect apply-changes -b --backup=sssd.backup

(补充:这里以创建 sssd.backup 备份文件为例)

4.3.1.2 创建新的自定义配置文件
# authselect create-profile password-policy -b sssd --symlink-meta --symlink-pam

(补充:这里以生成名为 password-policy 的自定义配置文件为例)

4.3.1.3 选择新的自定义配置文件
4.3.1.3.1 选择新的自定义配置文件
# authselect select custom/password-policy with-sudo with-faillock without-nullok with-mkhomedir --force

(
补充:
1) 这里以选择名为 password-policy 的自定义配置文件为例
2) 这里设置了 with-sudo、with-faillock、without-nullok 和 with-mkhomedir 参数
)

(注意:使用了 with-mkhomedir 参数后,会提示需要开启 oddjobd)

4.3.1.3.2 满足选择新的自定义配置文件时 with-mkhomedir 参数的要求
# dnf install oddjob ; systemctl enable --now oddjobd.service
4.3.1.4 显示当前选择的自定义配置文件
# authselect current

(补充:这里以生成并选择名为 password-policy 的自定义配置文件为例)

4.3.2 修改自定义配置文件
4.3.2.1 在 /etc/authselect/custom/password-policy/system-auth 配置文件中添加 pam_faillock.so 模块和相关参数  (不建议)
# 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 秒为例)

(注意:不建议执行此步骤,执行后正常的 SFTP 登陆会被视为登陆失败,不过正常的 SSH 登录不受影响)

4.3.2.2 在 /etc/authselect/custom/password-policy/system-auth 配置文件中设置登录失败几次后提示登陆失败
# vim /etc/authselect/custom/password-policy/system-auth

将以下内容:

......
password    requisite                                    pam_pwquality.so ......
......

修改为:

......
password    requisite                                    pam_pwquality.so try_first_pass local_users_only enforce-for-root retry=3 ......
......

(补充:这里以登录失败 3 次后提示登录失败为例)

4.3.2.3 在 /etc/authselect/custom/password-policy/password-auth 配置文件中添加 pam_faillock.so 模块和相关参数 (不建议)
# 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"}
......

(
补充:
1) 这里以包括 root 用户每使用密码 SSH 远程登录失败 6 次则被锁定 180 秒
2) 登录失败 3 次后提示登录失败
为例
)

(注意:不建议执行此步骤,执行后正常的 SFTP 登陆会被视为登陆失败,不过正常的 SSH 登录不受影响)

4.3.2.4 在 /etc/authselect/custom/password-policy/password-auth 配置文件中设置登录失败几次后提示登陆失败
# vim /etc/authselect/custom/password-policy/password-auth

将以下内容:

......
password    requisite                                    pam_pwquality.so ......
......

修改为:

......
password    requisite                                    pam_pwquality.so try_first_pass local_users_only enforce-for-root retry=3 ......
......

(补充:这里以登录失败 3 次后提示登录失败为例)

4.4 让自定义配置文件生效

# authselect apply-changes

(注意:此步骤会刷新 /etc/authselect/system-auth 配置文件和 /etc/authselect/password-auth 配置文件)

步骤五:修改 /etc/security/faillock.conf 配置文件

# vim /etc/security/faillock.conf

将以下内容:

......
# deny =
......
# unlock_time =
......

修改为:

......
deny = 6
......
unlock_time = 180
......

(补充:这里以包括 root 用户每使用密码 SSH 远程登录失败 6 次则被锁定 180 秒为例)

(注意:只在 Rocky Linux 8 & RHEL 8 才进行此操作)

步骤六:用户登录失败的管理

6.1 显示某个用户近期输错了几次密码

# faillock --user root

(补充:这里以显示 root 用户近期输错了几次密码为例)

6.2 重制远程登录密码输错次数

6.2.1 重制某用户远程登录密码输错次数
# faillock --user root --reset
6.2.2 重制所有用户远程登录密码输错次数
# faillock --reset

步骤七:管理自定义配置文件

7.1 在自定义配置文件中禁用 pam_faillock.so 模块

# authselect disable-feature with-faillock

7.2 在自定义配置文件中启用 pam_faillock.so 模块

# authselect enable-feature with-faillock

参考文献:

https://access.redhat.com/solutions/62949

CategoriesChinese (中文), System (系统), System Login Security (系统登录安全), System Network & System Security & System Log (系统网络 & 系统安全 & 系统日志), System Operation & System Setting & System Software (系统操作 & 系统设置 & 系统软件), System Security (系统安全), System Setting (系统设置)

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