12. 安全加固
本章节指导在 RHEL 9(主机名 LinuxDC)上使用 SecureCRT 进行系统安全加固,配合 SecureFX 传输配置文件。内容涵盖 SELinux 配置、安全更新、账户安全、合规扫描和审计工具配置,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 LinuxDC 环境中测试,确保实验一致性。
12.1 前提条件
- RHEL 9 已安装(参考第 1 章),主机名设置为
LinuxDC。 - 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 9 章)。
- 使用 SecureFX 传输文件(SFTP 协议)。
- 具有 root 或 sudo 权限。
- 系统已订阅并启用 RHEL 仓库(参考第 2 章)。
sshd服务已配置(参考第 9 章)。
💡 提示:安全加固应在基线环境后立即进行,以最小化暴露风险。在 RHEL 10 中,安全工具如 SELinux 支持更多自动化策略和集成 AI 辅助的威胁检测。
12.2 安全加固
12.2.1 配置 SELinux
-
查看当前 SELinux 模式:
getenforce # 显示当前 SELinux 模式(Enforcing、Permissive、Disabled) -
设置 SELinux 为强制模式:
vim /etc/selinux/config # 修改为: SELINUX=enforcing setenforce 1 # 立即启用 Enforcing 模式 reboot # 重启应用(推荐) -
为 SSH 添加自定义端口(2222):
semanage port -a -t ssh_port_t -p tcp 2222 # 添加端口上下文 restorecon -Rv /etc/ssh/sshd_config # 恢复上下文 systemctl restart sshd
🧠 知识点:SELinux 基于标签强制访问控制(MAC)。Enforcing 模式拒绝违规操作,Permissive 仅记录。
✅ 检查点:
sestatus显示 Current mode: enforcing。
12.2.2 配置安全更新
-
安装自动更新工具:
dnf install -y dnf-automatic -
配置自动更新(排除内核):
vim /etc/dnf/automatic.conf # 修改: apply_updates = yes upgrade_type = security exclude = kernel* systemctl enable --now dnf-automatic.timer # 启用定时器 -
手动应用安全更新:
dnf update --security -y
💡 提示:定期检查
dnf check-update --security。在 RHEL 10 中,dnf-automatic 支持更多细粒度排除和通知集成。
12.2.3 账户安全配置
-
禁用 root 登录:
vim /etc/ssh/sshd_config # 修改为: PermitRootLogin no systemctl restart sshd -
设置密码策略(示例:90 天过期):
vim /etc/login.defs # 修改: PASS_MAX_DAYS 90 chage -M 90 testuser # 为 testuser 设置 -
锁定空闲账户:
useradd -D -f 30 # 默认锁定 30 天无活动账户
✅ 检查点:
chage -l testuser显示 Max days: 90。
12.2.4 合规扫描与审计
-
安装 OpenSCAP:
dnf install -y openscap-scanner scap-security-guide -
运行扫描:
oscap xccdf eval --profile stig --report /tmp/scap-report.html /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml -
启用 auditd:
dnf install -y audit systemctl enable --now auditd auditctl -a always,exit -F arch=b64 -S execve -k exec_log # 添加审计规则 -
安装 fail2ban(SSH 防护):
dnf install -y fail2ban vim /etc/fail2ban/jail.d/sshd.local # 添加: [sshd] enabled = true banaction = firewallcmd-ipset maxretry = 5 systemctl enable --now fail2ban
🧠 知识点:OpenSCAP 用于 CIS/STIG 合规检查,auditd 记录系统调用,fail2ban 防暴力破解。
✅ 检查点:
ausearch -k exec_log显示审计日志,fail2ban-client status sshd显示 jails。
12.3 常见问题与排查
🔍 故障排查:
-
问题 1:SELinux 拒绝访问
解决:ausearch -m avc -ts recent检查拒绝日志,restorecon或semanage修正上下文。 -
问题 2:更新失败
解决:检查订阅(subscription-manager status)、网络,或日志(journalctl -u dnf-automatic)。 -
问题 3:root 仍可登录
解决:确认sshd_config和systemctl restart sshd。 -
问题 4:审计日志缺失
解决:检查 auditd 状态(systemctl status auditd),规则(auditctl -l)。
12.4 实践任务
- 使用 SecureCRT 通过 SSH(端口 2222)登录
LinuxDC,将 SELinux 设置为 Enforcing 并为 SSH 配置端口 2222。 - 配置
dnf-automatic自动更新(排除内核),验证更新状态。 - 禁用 root 登录并为
testuser设置 90 天密码过期策略。 - 安装并启用
auditd和fail2ban,验证 SSH 防护。
✅ 检查点:SELinux enforcing,更新应用,root 禁用,审计和 fail2ban 运行正常。
12.5 自测问题
-
问题 1:如何为 SSH 添加非标准端口 2222?
答案:semanage port -a -t ssh_port_t -p tcp 2222 -
问题 2:如何禁用 root 登录?
答案:编辑/etc/ssh/sshd_config,设置PermitRootLogin no,然后systemctl restart sshd。 -
问题 3:如何启用
auditd服务?
答案:systemctl enable --now auditd
🧾 总结:
本章完整介绍了 RHEL 9 系统安全加固的流程,包括 SELinux、更新、账户和审计配置。正确实施这些措施可显著提升系统安全性,为后续故障排查与性能优化提供坚实防护基础。