Skip to content

13. 安全加固

本章节指导在 RHEL 8.10(主机名 ZSLinux)上使用 SecureCRT 进行系统安全加固,配合 SecureFX 传输配置文件。内容涵盖 SELinux 配置、安全更新、账户安全、合规扫描和审计工具配置,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 ZSLinux 环境中测试,确保实验一致性。

13.1 前提条件

  • RHEL 8.10 已安装(参考第 1 章),主机名设置为 ZSLinux.
  • 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 6 章).
  • 使用 SecureFX 传输文件(SFTP 协议).
  • 具有 root 或 sudo 权限.
  • 系统已订阅并启用 RHEL 仓库(参考第 4 章).
  • sshd 服务已配置(参考第 6 章).

13.2 安全加固

13.2.1 配置 SELinux

  • 查看当前 SELinux 模式:
    bash
    getenforce  # 显示当前 SELinux 模式(Enforcing、Permissive、Disabled)
  • 设置 SELinux 为强制模式:
    bash
    vim /etc/selinux/config  # 编辑 SELinux 配置文件
    # 修改为:
    SELINUX=enforcing
  • 配置 SSH 自定义端口(2222):
    bash
    semanage port -a -t ssh_port_t -p tcp 2222  # 为 SSH 添加端口 2222
    restorecon -R -v /etc/ssh  # 应用 SSH 配置文件上下文

13.2.2 配置安全更新

  • 安装并配置 dnf-automatic
    bash
    dnf install -y dnf-automatic  # 安装自动更新工具
    vim /etc/dnf/automatic.conf  # 编辑自动更新配置文件
    # 配置仅更新非内核包:
    [commands]
    apply_updates = yes
    upgrade_type = default
    exclude = kernel*
    systemctl enable --now dnf-automatic.timer  # 启用自动更新
  • 检查可用更新:
    bash
    dnf check-update  # 列出可用更新
  • 手动安装安全补丁:
    bash
    dnf update --security -y  # 仅安装安全更新

13.2.3 配置账户安全

  • 限制 root 登录:
    bash
    vim /etc/ssh/sshd_config  # 编辑 SSH 配置文件
    # 修改或添加:
    PermitRootLogin no
    systemctl restart sshd  # 重启 SSH 服务
  • 设置密码过期策略:
    bash
    chage -M 90 testuser  # 设置 testuser 密码 90 天后过期
  • 配置密码复杂性(PAM):
    bash
    vim /etc/security/pwquality.conf  # 编辑密码复杂性配置文件
    # 添加或修改:
    minlen = 12
    dcredit = -1
    ucredit = -1
    lcredit = -1
    ocredit = -1

13.3 审计与合规

13.3.1 配置审计日志

  • 安装并启用 auditd
    bash
    dnf install -y audit  # 安装 auditd 审计工具
    systemctl enable --now auditd  # 启用并启动 auditd 服务

13.3.2 文件完整性监控

  • 安装并初始化 aide
    bash
    dnf install -y aide  # 安装 AIDE 文件完整性监控工具
    aide --init  # 初始化 AIDE 数据库
    mv /var/lib/aide/aide.db.new.gz /var/lib/aide/aide.db.gz  # 激活数据库

13.3.3 防暴力破解

  • 安装并启用 fail2ban
    bash
    dnf install -y fail2ban  # 安装 fail2ban 防暴力破解工具
    vim /etc/fail2ban/jail.local  # 创建 fail2ban 配置文件
    # 添加:
    [sshd]
    enabled = true
    port = 2222
    maxretry = 5
    bantime = 3600
    systemctl enable --now fail2ban  # 启用并启动 fail2ban 服务

13.3.4 运行 OpenSCAP 扫描

  • 安装并运行 CIS 基准扫描:
    bash
    dnf install -y openscap-scanner scap-security-guide  # 安装 OpenSCAP 工具
    oscap xccdf eval --profile cis /usr/share/xml/scap/ssg/content/ssg-rhel8-ds.xml  # 运行 CIS 基准扫描

13.4 验证

  • 验证 SELinux 配置:
    bash
    getenforce  # 检查 SELinux 模式,应显示 Enforcing
    semanage port -l | grep ssh  # 检查 SSH 端口 2222
  • 验证安全更新:
    bash
    dnf list updates  # 检查可用更新
    systemctl status dnf-automatic.timer  # 检查自动更新状态
  • 验证账户安全:
    bash
    grep PermitRootLogin /etc/ssh/sshd_config  # 确认 root 登录已禁用
    chage -l testuser  # 检查 testuser 密码策略
  • 验证审计和监控:
    bash
    systemctl is-active auditd  # 检查 auditd 服务状态
    aide --check  # 检查文件完整性
    fail2ban-client status sshd  # 检查 fail2ban SSH 防护状态
  • 测试 SSH 连接:
    bash
    ssh -p 2222 testuser@ZSLinux  # 测试 SSH 连接(参考第 6 章)
  • 常见问题:
    • SELinux 阻止服务:检查日志(ausearch -m avc -ts recent)或临时设为 Permissive(setenforce 0)。
    • 自动更新失败:检查 dnf-automatic 配置(/etc/dnf/automatic.conf)。
    • root 仍可登录:确认 sshd_configsystemctl restart sshd

13.5 实践任务

  1. 使用 SecureCRT 登录 ZSLinux,将 SELinux 设置为 Enforcing 并为 SSH 配置端口 2222。
  2. 配置 dnf-automatic 自动更新(排除内核),验证更新状态。
  3. 禁用 root 登录并为 testuser 设置 90 天密码过期策略。
  4. 安装并启用 auditdfail2ban,验证 SSH 防护。

13.6 自测问题

  • 问题:如何为 SSH 添加非标准端口 2222?
    • 答案semanage port -a -t ssh_port_t -p tcp 2222
  • 问题:如何禁用 root 登录?
    • 答案:编辑 /etc/ssh/sshd_config,设置 PermitRootLogin no,然后 systemctl restart sshd.
  • 问题:如何启用 auditd 服务?
    • 答案systemctl enable --now auditd

Released under the MIT License.