小版本升级
小版本升级(如 EL 9.3 到 9.4)是日常运维中最常见的维护操作。本文档涵盖完整的 dnf update 工作流,包括预检、执行、验证和回滚。
检查可用更新
Section titled “检查可用更新”在执行升级之前,先了解有哪些包可以更新。
列出所有可用更新
Section titled “列出所有可用更新”dnf check-update该命令返回码为 100 表示有可用更新,0 表示没有更新,1 表示出错。
查看安全更新
Section titled “查看安全更新”dnf updateinfo list security查看更新的详细信息
Section titled “查看更新的详细信息”dnf updateinfo info --advisories=ALSA-2025:1234查看更新摘要
Section titled “查看更新摘要”dnf updateinfo summary执行小版本升级
Section titled “执行小版本升级”标准全量更新
Section titled “标准全量更新”dnf update -y仅安装安全更新
Section titled “仅安装安全更新”dnf update --security -ydnf update -y --exclude=kernel* --exclude=php*模拟运行(不实际安装)
Section titled “模拟运行(不实际安装)”dnf update --assumeno内核更新注意事项
Section titled “内核更新注意事项”内核更新不会替换旧内核,而是并行安装新版本。
查看当前运行的内核
Section titled “查看当前运行的内核”uname -r查看已安装的内核列表
Section titled “查看已安装的内核列表”rpm -qa kernel-core | sort -V单独更新内核
Section titled “单独更新内核”dnf update kernel -y更新内核后必须重启系统才能使用新内核。
使用 needs-restarting 检查
Section titled “使用 needs-restarting 检查”needs-restarting 工具用于判断更新后哪些服务或系统需要重启。
安装 needs-restarting
Section titled “安装 needs-restarting”dnf install -y dnf-plugins-core检查是否需要重启系统
Section titled “检查是否需要重启系统”needs-restarting -r返回码 0 表示不需要重启,1 表示需要重启。
列出需要重启的服务
Section titled “列出需要重启的服务”needs-restarting -s批量重启受影响的服务
Section titled “批量重启受影响的服务”needs-restarting -s | xargs -I {} systemctl restart {}查看 dnf 历史
Section titled “查看 dnf 历史”dnf history list查看某次操作的详细信息
Section titled “查看某次操作的详细信息”dnf history info <事务ID>回滚到指定事务
Section titled “回滚到指定事务”dnf history undo <事务ID> -y回滚到某个时间点之前的状态
Section titled “回滚到某个时间点之前的状态”dnf history rollback <事务ID> -y如果新内核导致问题,可以在 GRUB 菜单中选择旧内核启动,无需回滚操作。
grubby --set-default /boot/vmlinuz-<旧版本号>维护窗口规划
Section titled “维护窗口规划”推荐的维护流程
Section titled “推荐的维护流程”- 提前通知 — 至少提前 48 小时通知相关团队
- 备份 — 创建系统快照或备份关键数据
- 预检 — 在测试环境验证更新
- 执行 — 在维护窗口内执行更新
- 验证 — 检查服务状态和功能
- 回滚准备 — 确认回滚方案可行
创建更新前快照(如使用 LVM)
Section titled “创建更新前快照(如使用 LVM)”lvcreate -s -n pre-update-snap -L 10G /dev/vg0/root编写自动化更新脚本
Section titled “编写自动化更新脚本”#!/bin/bashset -e
echo "=== 维护更新开始: $(date) ==="
# 1. 记录当前状态rpm -qa --qf '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' | sort > /root/pkg-before.txt
# 2. 执行更新dnf update -y
# 3. 记录更新后状态rpm -qa --qf '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' | sort > /root/pkg-after.txt
# 4. 输出变更差异echo "=== 包变更列表 ==="diff /root/pkg-before.txt /root/pkg-after.txt || true
# 5. 检查是否需要重启if ! needs-restarting -r &>/dev/null; then echo "*** 系统需要重启 ***"fi
echo "=== 维护更新完成: $(date) ==="设置自动安全更新(可选)
Section titled “设置自动安全更新(可选)”dnf install -y dnf-automatic
# 编辑配置:仅应用安全更新sed -i 's/^upgrade_type.*/upgrade_type = security/' /etc/dnf/automatic.confsed -i 's/^apply_updates.*/apply_updates = yes/' /etc/dnf/automatic.conf
systemctl enable --now dnf-automatic.timer验证自动更新定时器
Section titled “验证自动更新定时器”systemctl status dnf-automatic.timer