跳转至

31. 附录与维护建议

本章节指导在 RHEL 9(主机名 LinuxDC)上使用 SecureCRT 进行系统配置记录、备份恢复与长期维护操作,配合 SecureFX 传输备份文件。内容涵盖关键配置记录(服务、防火墙、SELinux)、备份与恢复流程、日常维护建议,以确保系统可恢复性与长期稳定性为例,全面实用,适合初学者快速掌握和运维人员日常参考。所有操作在 LinuxDC 环境中测试,确保实验一致性。


31.1 前提条件

  • RHEL 9 已安装(参考第 1 章),主机名设置为 LinuxDC
  • 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 9 章)。
  • 使用 SecureFX 传输文件(SFTP 协议)。
  • 具有 root 或 sudo 权限。
  • 系统已订阅并启用 RHEL 和 EPEL 仓库(参考第 2 和第 5 章)。
  • 防火墙和 SELinux 启用(参考第 12 章)。
  • 备份目录 /backup 已创建并具有足够空间。

💡 提示:建议将本章内容作为运维手册的“常备章节”,定期执行记录与备份检查。在 RHEL 10 中,系统诊断工具(如 sosreport)支持更多自动化采集与云上传功能,提升问题诊断效率。


31.2 系统配置记录

31.2.1 记录启用服务列表

mkdir -p /root/config-records
systemctl list-units --type=service --state=enabled > /root/config-records/services-enabled.txt
systemctl list-unit-files --type=service --state=enabled >> /root/config-records/services-enabled.txt

31.2.2 记录防火墙规则

firewall-cmd --list-all-zones > /root/config-records/firewall-zones.txt
firewall-cmd --get-active-zones >> /root/config-records/firewall-zones.txt
firewall-cmd --list-all >> /root/config-records/firewall-active.txt

31.2.3 记录 SELinux 配置与布尔值

sestatus > /root/config-records/selinux-status.txt
semanage port -l > /root/config-records/selinux-ports.txt
semanage fcontext -l > /root/config-records/selinux-fcontext.txt
getsebool -a > /root/config-records/selinux-booleans.txt

31.2.4 记录已安装软件包(完整清单)

dnf list installed > /root/config-records/packages-installed.txt
rpm -qa --last > /root/config-records/packages-by-install-time.txt

31.2.5 记录网络配置

nmcli con show > /root/config-records/network-connections.txt
nmcli device status >> /root/config-records/network-connections.txt
ip addr show > /root/config-records/ip-addresses.txt

🧠 知识点:定期将 /root/config-records 目录备份到外部存储或云端,便于快速对比与恢复。


31.3 备份与恢复流程

31.3.1 定期全系统关键目录备份

  • 示例 cron 任务(每周日凌晨 2:00):

    crontab -e
    # 添加:
    0 2 * * 0 tar -czf /backup/system-$(date +%F).tar.gz /etc /var /home /root/config-records
    

  • 轮转删除旧备份(保留最近 4 周):

    find /backup -name "system-*.tar.gz" -mtime +28 -delete
    

31.3.2 恢复关键文件(示例:/etc/fstab)

mkdir /tmp/restore
tar -xzf /backup/system-$(date +%F).tar.gz -C /tmp/restore etc/fstab
mv /tmp/restore/etc/fstab /etc/fstab
restorecon -v /etc/fstab  # 恢复 SELinux 上下文

⚠️ 注意:恢复前建议先备份当前文件(如 cp /etc/fstab /etc/fstab.bak)。

31.3.3 生成诊断报告(sosreport)

dnf install -y sos
sos report --tmp-dir /tmp --batch  # 生成诊断报告
# 使用 SecureFX 下载 /tmp/sosreport-*.tar.xz

🧠 知识点:sosreport 是 Red Hat 官方推荐的诊断工具,常用于提交支持案例。


31.4 日常维护建议

  1. 定期更新系统

    dnf update --security -y  # 每周执行安全更新
    dnf update -y             # 每月执行完整更新
    reboot                    # 必要时重启
    

  2. 监控磁盘空间

    df -h | grep -v tmpfs | sort -k5 -nr  # 查看占用空间前几项
    

  3. 检查日志异常

    journalctl -p err -b  # 当前引导的错误日志
    journalctl --since "7 days ago" -u sshd  # 最近 7 天 sshd 日志
    

  4. 验证 SELinux 拒绝

    ausearch -m avc -ts recent | audit2allow -M mypolicy  # 生成修复策略
    

  5. 备份验证
    每月随机抽取一次备份文件进行恢复测试,确保可读、可恢复。

  6. 文档与快照管理

  7. 每次重大变更前创建 VMware 快照,命名格式:YYYYMMDD-变更描述
  8. 保留关键配置文件版本历史(如 /etc/httpd/conf.d/ 使用 git 管理)

31.5 自测问题

  • 问题 1:如何记录当前所有启用服务列表?
    答案systemctl list-units --type=service --state=enabled > /root/config-records/services-enabled.txt

  • 问题 2:如何恢复 /etc/fstab 文件?
    答案tar -xzf /backup/system-$(date +%F).tar.gz -C /tmp etc/fstab; mv /tmp/etc/fstab /etc/fstab; restorecon -v /etc/fstab

  • 问题 3:如何设置每周日凌晨 2:00 自动备份关键目录?
    答案:编辑 crontab -e,添加 0 2 * * 0 tar -czf /backup/system-$(date +%F).tar.gz /etc /var /home


🧾 总结
本章作为全套教程的附录,提供了系统配置记录、备份恢复与日常维护的实用方法。这些习惯与工具组合使用,可显著提升系统的可维护性、可恢复性与长期稳定性,是 Linux 运维人员的核心素养。