Skip to content
Eternal Center

Eternal Center

  • Single-Node (单节点)
    • System (系统)
    • Service (服务)
    • Database (数据库)
    • Container (容器)
    • Virtualization (虚拟化)
  • Multi-Node (多节点)
    • Cluster (集群)
    • Big Data (大数据)
    • Cloud Computing (云计算)
    • Batch Processing (批量处理)
  • Approach (方式方法)
    • Languages (语言)
    • Ideas (思路)
    • Programing (编程)
    • Project (项目)
  • Eternity (永恒)
    • News (消息)
    • Creations (创作)
    • Classics (经典)
    • Legends (传说)
    • Chronicle (编年史)
    • FNIOS (宇宙公民开源学院)
Posted on January 1, 2022November 22, 2024 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 登录不受影响。若要确保正常 SFTP 登录不被视为失败登录,则需要确保部分内容如下:

# cat /etc/authselect/custom/password-policy/system-auth
......
auth        required                                     pam_faillock.so preauth silent
......
auth        required                                     pam_faillock.so authfail
......
account     required                                     pam_faillock.so
......

)

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 登录不受影响。若要确保正常 SFTP 登录不被视为失败登录,则需要确保部分内容如下:

# cat /etc/authselect/custom/password-policy/password-auth
......
auth        required                                     pam_faillock.so preauth silent
......
auth        required                                     pam_faillock.so authfail
......
account     required                                     pam_faillock.so
......

)

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 登录不受影响。若要确保正常 SFTP 登录不被视为失败登录,则需要确保部分内容如下:

# cat /etc/authselect/custom/password-policy/system-auth
......
auth        required                                     pam_faillock.so preauth silent
......
auth        required                                     pam_faillock.so authfail
......
account     required                                     pam_faillock.so
......

)

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 登录不受影响。若要确保正常 SFTP 登录不被视为失败登录,则需要确保部分内容如下:

# cat /etc/authselect/custom/password-policy/password-auth
......
auth        required                                     pam_faillock.so preauth silent
......
auth        required                                     pam_faillock.so authfail
......
account     required                                     pam_faillock.so
......

)

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

(
注意:此步骤
1) 会将 /etc/authselect/custom/password-policy/system-auth 配置文件里的内容刷新到 /etc/authselect/system-auth 配置文件
2) 会将/etc/authselect/custom/password-policy/password-auth 配置文件里的内容刷新到 /etc/authselect/password-auth 配置文件
3) 若要确保正常 SFTP 登录不被视为失败登录,则需要确保部分内容如下:

# cat /etc/authselect/system-auth
......
auth        required                                     pam_faillock.so preauth silent
......
auth        required                                     pam_faillock.so authfail
......
account     required                                     pam_faillock.so
......
# cat /etc/authselect/password-auth
......
auth        required                                     pam_faillock.so preauth silent
......
auth        required                                     pam_faillock.so authfail
......
account     required                                     pam_faillock.so
......

)

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

# vim /etc/security/faillock.conf

将以下内容:

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

修改为:

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

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

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

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

# faillock --user root

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

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

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

(补充:这里以重置 root 用户远程登录密码输错次数为例)

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 Post (中文帖子), System (系统), System Login Security (系统登录安全), System Network & System Security & System Log (系统网络 & 系统安全 & 系统日志), System Operation & System Setting & System Software (系统操作 & 系统设置 & 系统软件), System Security (系统安全), System Setting (系统设置), System Setting Other Steps (系统设置其它步骤), System Setting Others (系统设置其它)

Post navigation

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

Aspiration (愿景):

Everyone can achieve self-achievement and self-happiness fairly

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

Logo (徽标):

Additional Information (其他信息):

About     Manual     Clone     Contact
Disclaimer     Friendly Links     Donation

关于     手册     克隆     联系
免责声明     友情链接     捐赠

Search Inside Website (站内搜索)

Search Outside Website (站外搜索):

Google         Wikipedia         Bing

Eternal URL (永恒网址):

https://eternity.eternalcenter.com Will be last access method / 将是最后的访问方式

Proudly powered by LNMP Proudly powered by WordPress