1. 首页 > Linux教程 > 正文

Linux教程FG105-dnf update/upgrade命令(软件包升级)

本文档风哥主要介绍dnf update/upgrade命令的使用,包括dnf update/upgrade命令的概念、dnf update与dnf upgrade的区别、dnf update/upgrade命令常用选项、dnf update基础操作详解、dnf update安全补丁详解、dnf update高级使用技巧、系统全面升级实战案例、内核升级实战案例、软件包升级故障排查与解决等内容,参考Red Hat Enterprise Linux 10官方文档,适合Linux运维人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。

Part01-基础概念与理论知识

1.1 dnf update/upgrade命令的概念

dnf update/upgrade命令是DNF软件包管理器中用于升级软件包的命令。dnf update和dnf upgrade命令功能相同,都是用于升级软件包到最新版本。更多视频教程www.fgedu.net.cn

dnf update/upgrade命令的主要特点:

  • 自动解析依赖关系
  • 支持软件包和包组升级
  • 支持安全补丁更新
  • 支持事务回滚
  • 支持最小化升级
  • 支持下载后升级

1.2 dnf update与dnf upgrade的区别

dnf update与dnf upgrade的区别:

  • 功能相同:dnf update和dnf upgrade命令功能完全相同
  • 兼容性:dnf update向后兼容YUM
  • 推荐使用:推荐使用dnf upgrade
  • 别名:dnf update是dnf upgrade的别名

1.3 dnf update/upgrade命令常用选项

dnf update/upgrade命令常用选项:

  • -y, –assumeyes:自动回答yes
  • –assumeno:自动回答no
  • –downloadonly:仅下载不升级
  • –downloaddir DIR:指定下载目录
  • –security:仅升级安全补丁
  • –bugfix:仅升级bug修复
  • –advisory ADVISORY:升级指定公告
  • –minimal:最小化升级
风哥提示:dnf update和dnf upgrade命令功能相同,推荐使用dnf upgrade。升级前建议先使用dnf check-update查看可用更新。

Part02-生产环境规划与建议

2.1 软件包升级在生产环境的规划

软件包升级在生产环境的规划要点:

# 软件包升级生产环境规划
– 在测试环境先测试升级
– 检查可用更新
– 检查更新详情
– 备份重要数据
– 使用-y选项自动确认
– 使用–security仅升级安全补丁

# 软件包升级注意事项
– 检查更新是否影响系统功能
– 备份重要数据和配置文件
– 检查磁盘空间是否充足
– 准备回滚方案
– 选择合适的升级时间

2.2 软件包升级最佳实践

软件包升级最佳实践:

  • 测试环境:在测试环境先测试升级
  • 检查更新:检查可用更新和更新详情
  • 备份:备份重要数据和配置文件
  • 安全补丁:优先升级安全补丁
  • 磁盘空间:检查磁盘空间是否充足
  • 回滚方案:准备回滚方案

2.3 软件包升级安全配置建议

软件包升级安全配置建议:

  • 安全补丁:优先升级安全补丁
  • 官方存储库:使用官方或可信的存储库
  • 最小权限:使用sudo执行升级命令
  • 审计日志:启用DNF审计日志
  • 定期更新:定期升级软件包
生产环境建议:在测试环境先测试升级,检查可用更新和更新详情,备份重要数据和配置文件,优先升级安全补丁。学习交流加群风哥微信: itpux-com

Part03-生产环境项目实施方案

3.1 dnf update基础操作详解

3.1.1 检查可用更新

# 检查可用更新
# dnf check-update
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
kernel.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-core.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-modules.x86_64 5.14.0-284.11.1.el9_2 baseos
nginx.x86_64 1.22.2-1.el9 appstream

# 查看更新详情
# dnf updateinfo list
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Updates Information Summary: available
1 security notice(s)
1 bugfix notice(s)

# 查看安全更新详情
# dnf updateinfo info –security
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
RHSA-2026:1234 Important/Sec. kernel security update
Update ID: RHSA-2026:1234
Type: security
Severity: Important
Release: RHEL 10
Status: stable
Issued: 2026-03-31 00:00:00
Bugs: 1234567
CVEs: CVE-2026-1234
Description: The kernel packages contain the Linux kernel, the core of any
Linux operating system. Security Fix(es): * kernel: A flaw
was found in the Linux kernel’s implementation of the
eBPF subsystem. This issue could allow a local user to
escalate their privileges on the system. (CVE-2026-1234,
Important)

3.1.2 升级所有软件包

# 升级所有软件包
# dnf -y update
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
kernel x86_64 5.14.0-284.11.1.el9_2 baseos 2.3 M
kernel-core x86_64 5.14.0-284.11.1.el9_2 baseos 9.8 M
kernel-modules x86_64 5.14.0-284.11.1.el9_2 baseos 28 M
nginx x86_64 1.22.2-1.el9 appstream 35 k

Transaction Summary
================================================================================
Upgrade 4 Packages

Total download size: 40.1 M
Downloading Packages:
(1/4): kernel-5.14.0-284.11.1.el9_2.x86_64.rpm 2.3 MB/s | 2.3 MB 00:00
(2/4): kernel-core-5.14.0-284.11.1.el9_2.x86_64.rpm 9.8 MB/s | 9.8 MB 00:00
(3/4): kernel-modules-5.14.0-284.11.1.el9_2.x86_64.rpm 28 MB/s | 28 MB 00:00
(4/4): nginx-1.22.2-1.el9.x86_64.rpm 35 kB/s | 35 kB 00:00
——————————————————————————–
Total 40.1 MB/s | 40.1 MB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Upgrading : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/4
Upgrading : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/4
Upgrading : kernel-5.14.0-284.11.1.el9_2.x86_64 3/4
Upgrading : nginx-1.22.2-1.el9.x86_64 4/4
Cleanup : kernel-core-5.14.0-284.11.0.el9_2.x86_64 5/4
Cleanup : kernel-modules-5.14.0-284.11.0.el9_2.x86_64 6/4
Cleanup : kernel-5.14.0-284.11.0.el9_2.x86_64 7/4
Cleanup : nginx-1.22.1-2.el9.x86_64 8/4
Verifying : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/4
Verifying : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/4
Verifying : kernel-5.14.0-284.11.1.el9_2.x86_64 3/4
Verifying : nginx-1.22.2-1.el9.x86_64 4/4

Complete!

# 验证软件包已升级
# rpm -qa | grep -E “kernel|nginx”
kernel-5.14.0-284.11.0.el9_2.x86_64
kernel-core-5.14.0-284.11.0.el9_2.x86_64
kernel-modules-5.14.0-284.11.0.el9_2.x86_64
kernel-5.14.0-284.11.1.el9_2.x86_64
kernel-core-5.14.0-284.11.1.el9_2.x86_64
kernel-modules-5.14.0-284.11.1.el9_2.x86_64
nginx-1.22.2-1.el9.x86_64

3.2 dnf update安全补丁详解

3.2.1 仅升级安全补丁

# 检查安全更新
# dnf check-update –security
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
kernel.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-core.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-modules.x86_64 5.14.0-284.11.1.el9_2 baseos

# 仅升级安全补丁
# dnf -y update –security
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
kernel x86_64 5.14.0-284.11.1.el9_2 baseos 2.3 M
kernel-core x86_64 5.14.0-284.11.1.el9_2 baseos 9.8 M
kernel-modules x86_64 5.14.0-284.11.1.el9_2 baseos 28 M

Transaction Summary
================================================================================
Upgrade 3 Packages

Total download size: 40.1 M
Downloading Packages:
(1/3): kernel-5.14.0-284.11.1.el9_2.x86_64.rpm 2.3 MB/s | 2.3 MB 00:00
(2/3): kernel-core-5.14.0-284.11.1.el9_2.x86_64.rpm 9.8 MB/s | 9.8 MB 00:00
(3/3): kernel-modules-5.14.0-284.11.1.el9_2.x86_64.rpm 28 MB/s | 28 MB 00:00
——————————————————————————–
Total 40.1 MB/s | 40.1 MB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Upgrading : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/3
Upgrading : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/3
Upgrading : kernel-5.14.0-284.11.1.el9_2.x86_64 3/3
Cleanup : kernel-core-5.14.0-284.11.0.el9_2.x86_64 4/3
Cleanup : kernel-modules-5.14.0-284.11.0.el9_2.x86_64 5/3
Cleanup : kernel-5.14.0-284.11.0.el9_2.x86_64 6/3
Verifying : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/3
Verifying : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/3
Verifying : kernel-5.14.0-284.11.1.el9_2.x86_64 3/3

Complete!

# 验证内核已升级
# uname -r
5.14.0-284.11.1.el9_2.x86_64

3.3 dnf update高级使用技巧

3.3.1 仅下载不升级

# 仅下载更新不升级
# dnf update –downloadonly –downloaddir=/tmp
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
kernel x86_64 5.14.0-284.11.1.el9_2 baseos 2.3 M
kernel-core x86_64 5.14.0-284.11.1.el9_2 baseos 9.8 M
kernel-modules x86_64 5.14.0-284.11.1.el9_2 baseos 28 M
nginx x86_64 1.22.2-1.el9 appstream 35 k

Transaction Summary
================================================================================
Upgrade 4 Packages

Total download size: 40.1 M
Downloading Packages:
(1/4): kernel-5.14.0-284.11.1.el9_2.x86_64.rpm 2.3 MB/s | 2.3 MB 00:00
(2/4): kernel-core-5.14.0-284.11.1.el9_2.x86_64.rpm 9.8 MB/s | 9.8 MB 00:00
(3/4): kernel-modules-5.14.0-284.11.1.el9_2.x86_64.rpm 28 MB/s | 28 MB 00:00
(4/4): nginx-1.22.2-1.el9.x86_64.rpm 35 kB/s | 35 kB 00:00
——————————————————————————–
Total 40.1 MB/s | 40.1 MB 00:00
Complete!

# 查看下载的软件包
# ls -lh /tmp/*.rpm
-rw-r–r–. 1 root root 2.3M Mar 31 10:00 kernel-5.14.0-284.11.1.el9_2.x86_64.rpm
-rw-r–r–. 1 root root 9.8M Mar 31 10:00 kernel-core-5.14.0-284.11.1.el9_2.x86_64.rpm
-rw-r–r–. 1 root root 28M Mar 31 10:00 kernel-modules-5.14.0-284.11.1.el9_2.x86_64.rpm
-rw-r–r–. 1 root root 35K Mar 31 10:00 nginx-1.22.2-1.el9.x86_64.rpm

风哥提示:dnf update命令支持多种高级选项,如–security仅升级安全补丁,–downloadonly仅下载不升级等。学习交流加群风哥QQ113257174

Part04-生产案例与实战讲解

4.1 系统全面升级实战案例

4.1.1 系统全面升级

# 1. 检查可用更新
# dnf check-update
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
kernel.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-core.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-modules.x86_64 5.14.0-284.11.1.el9_2 baseos
nginx.x86_64 1.22.2-1.el9 appstream
systemd.x86_64 252-14.el9_2.3 baseos

# 2. 查看更新详情
# dnf updateinfo list
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Updates Information Summary: available
2 security notice(s)
1 bugfix notice(s)

# 3. 备份重要数据
# tar -czf /backup/system-backup-$(date +%Y%m%d).tar.gz /etc /var /home

# 4. 升级所有软件包
# dnf -y update
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
kernel x86_64 5.14.0-284.11.1.el9_2 baseos 2.3 M
kernel-core x86_64 5.14.0-284.11.1.el9_2 baseos 9.8 M
kernel-modules x86_64 5.14.0-284.11.1.el9_2 baseos 28 M
nginx x86_64 1.22.2-1.el9 appstream 35 k
systemd x86_64 252-14.el9_2.3 baseos 4.5 M

Transaction Summary
================================================================================
Upgrade 5 Packages

Total download size: 44.6 M
Downloading Packages:
(1/5): kernel-5.14.0-284.11.1.el9_2.x86_64.rpm 2.3 MB/s | 2.3 MB 00:00
(2/5): kernel-core-5.14.0-284.11.1.el9_2.x86_64.rpm 9.8 MB/s | 9.8 MB 00:00
(3/5): kernel-modules-5.14.0-284.11.1.el9_2.x86_64.rpm 28 MB/s | 28 MB 00:00
(4/5): nginx-1.22.2-1.el9.x86_64.rpm 35 kB/s | 35 kB 00:00
(5/5): systemd-252-14.el9_2.3.x86_64.rpm 4.5 MB/s | 4.5 MB 00:00
——————————————————————————–
Total 44.6 MB/s | 44.6 MB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Upgrading : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/5
Upgrading : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/5
Upgrading : kernel-5.14.0-284.11.1.el9_2.x86_64 3/5
Upgrading : nginx-1.22.2-1.el9.x86_64 4/5
Upgrading : systemd-252-14.el9_2.3.x86_64 5/5
Cleanup : kernel-core-5.14.0-284.11.0.el9_2.x86_64 6/5
Cleanup : kernel-modules-5.14.0-284.11.0.el9_2.x86_64 7/5
Cleanup : kernel-5.14.0-284.11.0.el9_2.x86_64 8/5
Cleanup : nginx-1.22.1-2.el9.x86_64 9/5
Cleanup : systemd-252-14.el9_2.2.x86_64 10/5
Verifying : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/5
Verifying : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/5
Verifying : kernel-5.14.0-284.11.1.el9_2.x86_64 3/5
Verifying : nginx-1.22.2-1.el9.x86_64 4/5
Verifying : systemd-252-14.el9_2.3.x86_64 5/5

Complete!

# 5. 重启系统以使用新内核
# reboot

# 6. 验证系统升级
# uname -r
5.14.0-284.11.1.el9_2.x86_64

# rpm -qa | grep nginx
nginx-1.22.2-1.el9.x86_64

# rpm -qa | grep systemd
systemd-252-14.el9_2.3.x86_64

4.2 内核升级实战案例

4.2.1 内核升级

# 1. 查看当前内核版本
# uname -r
5.14.0-284.11.0.el9_2.x86_64

# 2. 查看已安装的内核
# rpm -qa | grep kernel
kernel-5.14.0-284.11.0.el9_2.x86_64
kernel-core-5.14.0-284.11.0.el9_2.x86_64
kernel-modules-5.14.0-284.11.0.el9_2.x86_64

# 3. 检查内核更新
# dnf check-update kernel
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
kernel.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-core.x86_64 5.14.0-284.11.1.el9_2 baseos
kernel-modules.x86_64 5.14.0-284.11.1.el9_2 baseos

# 4. 升级内核
# dnf -y update kernel
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
kernel x86_64 5.14.0-284.11.1.el9_2 baseos 2.3 M
kernel-core x86_64 5.14.0-284.11.1.el9_2 baseos 9.8 M
kernel-modules x86_64 5.14.0-284.11.1.el9_2 baseos 28 M

Transaction Summary
================================================================================
Upgrade 3 Packages

Total download size: 40.1 M
Downloading Packages:
(1/3): kernel-5.14.0-284.11.1.el9_2.x86_64.rpm 2.3 MB/s | 2.3 MB 00:00
(2/3): kernel-core-5.14.0-284.11.1.el9_2.x86_64.rpm 9.8 MB/s | 9.8 MB 00:00
(3/3): kernel-modules-5.14.0-284.11.1.el9_2.x86_64.rpm 28 MB/s | 28 MB 00:00
——————————————————————————–
Total 40.1 MB/s | 40.1 MB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Upgrading : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/3
Upgrading : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/3
Upgrading : kernel-5.14.0-284.11.1.el9_2.x86_64 3/3
Cleanup : kernel-core-5.14.0-284.11.0.el9_2.x86_64 4/3
Cleanup : kernel-modules-5.14.0-284.11.0.el9_2.x86_64 5/3
Cleanup : kernel-5.14.0-284.11.0.el9_2.x86_64 6/3
Verifying : kernel-core-5.14.0-284.11.1.el9_2.x86_64 1/3
Verifying : kernel-modules-5.14.0-284.11.1.el9_2.x86_64 2/3
Verifying : kernel-5.14.0-284.11.1.el9_2.x86_64 3/3

Complete!

# 5. 查看已安装的内核
# rpm -qa | grep kernel
kernel-5.14.0-284.11.0.el9_2.x86_64
kernel-core-5.14.0-284.11.0.el9_2.x86_64
kernel-modules-5.14.0-284.11.0.el9_2.x86_64
kernel-5.14.0-284.11.1.el9_2.x86_64
kernel-core-5.14.0-284.11.1.el9_2.x86_64
kernel-modules-5.14.0-284.11.1.el9_2.x86_64

# 6. 设置默认内核
# grubby –set-default-index=0
index 0

# 7. 重启系统以使用新内核
# reboot

# 8. 验证内核版本
# uname -r
5.14.0-284.11.1.el9_2.x86_64

4.3 软件包升级故障排查与解决

4.3.1 依赖冲突问题

# 问题现象:软件包升级失败,依赖冲突
# 分析步骤:

# 1. 查看错误信息
# dnf update nginx
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
nginx x86_64 1.22.2-1.el9 appstream 35 k
Removing dependencies:
httpd x86_64 2.4.53-7.el9 @System 1.4 M

Transaction Summary
================================================================================
Upgrade 1 Package
Remove 1 Package

Total download size: 35 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): nginx-1.22.2-1.el9.x86_64.rpm 35 kB/s | 35 kB 00:00
——————————————————————————–
Total 35 kB/s | 35 kB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Upgrading : nginx-1.22.2-1.el9.x86_64 1/2
Removing : httpd-2.4.53-7.el9.x86_64 2/2
Verifying : nginx-1.22.2-1.el9.x86_64 1/2
Verifying : httpd-2.4.53-7.el9.x86_64 2/2

Complete!

# 2. 使用–allowerasing选项
# dnf update –allowerasing nginx
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Upgrading:
nginx x86_64 1.22.2-1.el9 appstream 35 k
Removing dependencies:
httpd x86_64 2.4.53-7.el9 @System 1.4 M

Transaction Summary
================================================================================
Upgrade 1 Package
Remove 1 Package

Total download size: 35 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): nginx-1.22.2-1.el9.x86_64.rpm 35 kB/s | 35 kB 00:00
——————————————————————————–
Total 35 kB/s | 35 kB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Upgrading : nginx-1.22.2-1.el9.x86_64 1/2
Removing : httpd-2.4.53-7.el9.x86_64 2/2
Verifying : nginx-1.22.2-1.el9.x86_64 1/2
Verifying : httpd-2.4.53-7.el9.x86_64 2/2

Complete!

# 3. 预防措施
# – 检查软件包依赖关系
# – 在测试环境先测试升级
# – 使用–security仅升级安全补丁

生产环境建议:软件包升级故障排查需要耐心和细致。依赖冲突可以使用–allowerasing选项解决。更多学习教程公众号风哥教程itpux_com

Part05-风哥经验总结与分享

5.1 软件包升级经验总结

软件包升级经验总结:

  • 测试环境:在测试环境先测试升级
  • 检查更新:检查可用更新和更新详情
  • 备份:备份重要数据和配置文件
  • 安全补丁:优先升级安全补丁
  • 磁盘空间:检查磁盘空间是否充足
  • 回滚方案:准备回滚方案

5.2 软件包升级检查清单

软件包升级检查清单:

  • 升级前:检查可用更新、更新详情、磁盘空间
  • 升级时:备份重要数据、使用-y选项自动确认
  • 升级后:验证软件包已升级
  • 内核升级:重启系统以使用新内核
  • 功能测试:测试系统功能是否正常
  • 故障排查:检查依赖关系、使用–allowerasing选项

5.3 软件包升级相关工具推荐

软件包升级相关工具推荐:

  • dnf:DNF软件包管理器
  • rpm:RPM包管理器
  • repoquery:查询软件包信息
  • package-cleanup:清理软件包
  • yum-utils:YUM实用工具(兼容DNF)
风哥提示:dnf update和dnf upgrade命令功能相同,推荐使用dnf upgrade。升级前建议先使用dnf check-update查看可用更新,备份重要数据和配置文件。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息