1. 首页 > Linux教程 > 正文

Linux教程FG106-dnf downgrade命令(软件包版本回滚)

本文档风哥主要介绍dnf downgrade命令的使用,包括dnf downgrade命令的概念、dnf downgrade命令语法、dnf downgrade命令常用选项、dnf downgrade基础操作详解、使用事务历史回滚详解、dnf downgrade高级使用技巧、Web服务器版本回滚实战案例、内核版本回滚实战案例、软件包版本回滚故障排查与解决等内容,参考Red Hat Enterprise Linux 10官方文档,适合Linux运维人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。

Part01-基础概念与理论知识

1.1 dnf downgrade命令的概念

dnf downgrade命令是DNF软件包管理器中用于回滚软件包版本的命令。dnf downgrade命令可以将软件包回滚到之前的版本,解决升级后出现的问题。更多视频教程www.fgedu.net.cn

dnf downgrade命令的主要特点:

  • 回滚软件包到之前的版本
  • 自动处理依赖关系
  • 支持软件包和包组回滚
  • 支持事务历史回滚
  • 支持本地软件包回滚

1.2 dnf downgrade命令语法

dnf downgrade命令语法:

  • 基本语法:dnf downgrade [选项] [软件包]
  • 回滚单个软件包:dnf downgrade nginx
  • 回滚到指定版本:dnf downgrade nginx-1.22.1-2.el9
  • 使用本地软件包回滚:dnf downgrade /path/to/nginx-1.22.1-2.el9.x86_64.rpm

1.3 dnf downgrade命令常用选项

dnf downgrade命令常用选项:

  • -y, –assumeyes:自动回答yes
  • –assumeno:自动回答no
  • –allowerasing:允许删除冲突软件包
  • –best:选择最佳软件包
  • –nobest:不选择最佳软件包
  • –setopt install_weak_deps=False:不安装弱依赖
风哥提示:dnf downgrade命令可以回滚软件包到之前的版本,解决升级后出现的问题。建议在升级前备份重要数据和配置文件。

Part02-生产环境规划与建议

2.1 软件包版本回滚在生产环境的规划

软件包版本回滚在生产环境的规划要点:

# 软件包版本回滚生产环境规划
– 在测试环境先测试回滚
– 检查可用版本
– 备份重要数据和配置文件
– 检查是否有其他软件包依赖
– 使用-y选项自动确认
– 准备回滚方案

# 软件包版本回滚注意事项
– 检查软件包依赖关系
– 备份配置文件
– 停止相关服务
– 检查系统功能是否正常
– 验证回滚结果

2.2 软件包版本回滚最佳实践

软件包版本回滚最佳实践:

  • 测试环境:在测试环境先测试回滚
  • 依赖检查:检查软件包依赖关系
  • 备份:备份重要数据和配置文件
  • 停止服务:停止相关服务
  • 验证:回滚后验证系统功能
  • 事务历史:利用事务历史回滚

2.3 软件包版本回滚安全配置建议

软件包版本回滚安全配置建议:

  • 最小权限:使用sudo执行回滚命令
  • 审计日志:启用DNF审计日志
  • 备份:备份重要数据和配置文件
  • 验证:回滚后验证系统功能
  • 回滚方案:准备回滚方案
生产环境建议:在测试环境先测试回滚,检查软件包依赖关系,备份重要数据和配置文件,使用事务历史回滚。学习交流加群风哥微信: itpux-com

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

3.1 dnf downgrade基础操作详解

3.1.1 查看软件包历史版本

# 查看nginx软件包的历史版本
# dnf history list nginx
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
ID | Command line | Date and time | Action(s) | Altered
—–+————————–+——————+—————-+——–
1 | install nginx | 2026-03-31 10:00 | Install | 3
2 | update nginx | 2026-03-31 10:05 | Update | 3
3 | update nginx | 2026-03-31 10:10 | Update | 3

# 查看事务详情
# dnf history info 2
Transaction ID : 2
Begin time : Thu Mar 31 10:05:00 2026
Begin rpmdb : 1234:5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f
End time : Thu Mar 31 10:05:05 2026
End rpmdb : 1235:5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f
User : root
Return-Code : Success
Command Line : update nginx
Packages Altered:
Upgrade nginx-1.22.1-2.el9.x86_64 -> nginx-1.22.2-1.el9.x86_64
Upgrade nginx-core-1.22.1-2.el9.x86_64 -> nginx-core-1.22.2-1.el9.x86_64
Upgrade nginx-filesystem-1.22.1-2.el9.noarch -> nginx-filesystem-1.22.2-1.el9.noarch

# 查看当前安装的nginx版本
# rpm -qa | grep nginx
nginx-1.22.2-1.el9.x86_64
nginx-core-1.22.2-1.el9.x86_64
nginx-filesystem-1.22.2-1.el9.noarch

3.1.2 回滚软件包版本

# 回滚nginx软件包版本
# dnf downgrade 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
================================================================================
Downgrading:
nginx x86_64 1.22.1-2.el9 @System 35 k
nginx-core x86_64 1.22.1-2.el9 @System 540 k
nginx-filesystem noarch 1.22.1-2.el9 @System 11 k

Transaction Summary
================================================================================
Downgrade 3 Packages

Total download size: 0
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Downgrading : nginx-filesystem-1.22.2-1.el9.noarch -> nginx-filesystem-1.22.1-2.el9.noarch
Downgrading : nginx-core-1.22.2-1.el9.x86_64 -> nginx-core-1.22.1-2.el9.x86_64
Downgrading : nginx-1.22.2-1.el9.x86_64 -> nginx-1.22.1-2.el9.x86_64
Verifying : nginx-filesystem-1.22.1-2.el9.noarch 1/3
Verifying : nginx-core-1.22.1-2.el9.x86_64 2/3
Verifying : nginx-1.22.1-2.el9.x86_64 3/3

Complete!

# 验证nginx版本已回滚
# rpm -qa | grep nginx
nginx-1.22.1-2.el9.x86_64
nginx-core-1.22.1-2.el9.x86_64
nginx-filesystem-1.22.1-2.el9.noarch

3.2 使用事务历史回滚详解

3.2.1 使用dnf history undo回滚

# 查看事务历史
# dnf history list
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
ID | Command line | Date and time | Action(s) | Altered
—–+————————–+——————+—————-+——–
1 | install nginx | 2026-03-31 10:00 | Install | 3
2 | update nginx | 2026-03-31 10:05 | Update | 3
3 | update nginx | 2026-03-31 10:10 | Update | 3

# 回滚事务2
# dnf history undo 2
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Undoing transaction 2, from Thu Mar 31 10:05:00 2026
Downgrade nginx-1.22.2-1.el9.x86_64 -> nginx-1.22.1-2.el9.x86_64
Downgrade nginx-core-1.22.2-1.el9.x86_64 -> nginx-core-1.22.1-2.el9.x86_64
Downgrade nginx-filesystem-1.22.2-1.el9.noarch -> nginx-filesystem-1.22.1-2.el9.noarch

Complete!

# 验证nginx版本已回滚
# rpm -qa | grep nginx
nginx-1.22.1-2.el9.x86_64
nginx-core-1.22.1-2.el9.x86_64
nginx-filesystem-1.22.1-2.el9.noarch

3.3 dnf downgrade高级使用技巧

3.3.1 使用本地软件包回滚

# 查看当前安装的nginx版本
# rpm -qa | grep nginx
nginx-1.22.2-1.el9.x86_64
nginx-core-1.22.2-1.el9.x86_64
nginx-filesystem-1.22.2-1.el9.noarch

# 使用本地软件包回滚
# dnf downgrade /tmp/nginx-1.22.1-2.el9.x86_64.rpm
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
================================================================================
Downgrading:
nginx x86_64 1.22.1-2.el9 @commandline 35 k
nginx-core x86_64 1.22.1-2.el9 @System 540 k
nginx-filesystem noarch 1.22.1-2.el9 @System 11 k

Transaction Summary
================================================================================
Downgrade 3 Packages

Total download size: 0
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Downgrading : nginx-filesystem-1.22.2-1.el9.noarch -> nginx-filesystem-1.22.1-2.el9.noarch
Downgrading : nginx-core-1.22.2-1.el9.x86_64 -> nginx-core-1.22.1-2.el9.x86_64
Downgrading : nginx-1.22.2-1.el9.x86_64 -> nginx-1.22.1-2.el9.x86_64
Verifying : nginx-filesystem-1.22.1-2.el9.noarch 1/3
Verifying : nginx-core-1.22.1-2.el9.x86_64 2/3
Verifying : nginx-1.22.1-2.el9.x86_64 3/3

Complete!

# 验证nginx版本已回滚
# rpm -qa | grep nginx
nginx-1.22.1-2.el9.x86_64
nginx-core-1.22.1-2.el9.x86_64
nginx-filesystem-1.22.1-2.el9.noarch

风哥提示:dnf downgrade命令支持多种高级选项,如使用本地软件包回滚,使用事务历史回滚等。学习交流加群风哥QQ113257174

Part04-生产案例与实战讲解

4.1 Web服务器版本回滚实战案例

4.1.1 Nginx版本回滚

# 1. 停止nginx服务
# systemctl stop nginx

# 2. 查看当前nginx版本
# nginx -v
nginx version: nginx/1.22.2

# 3. 查看nginx软件包的历史版本
# dnf history list nginx
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
ID | Command line | Date and time | Action(s) | Altered
—–+————————–+——————+—————-+——–
1 | install nginx | 2026-03-31 10:00 | Install | 3
2 | update nginx | 2026-03-31 10:05 | Update | 3

# 4. 回滚nginx版本
# dnf -y downgrade 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
================================================================================
Downgrading:
nginx x86_64 1.22.1-2.el9 @System 35 k
nginx-core x86_64 1.22.1-2.el9 @System 540 k
nginx-filesystem noarch 1.22.1-2.el9 @System 11 k

Transaction Summary
================================================================================
Downgrade 3 Packages

Total download size: 0
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Downgrading : nginx-filesystem-1.22.2-1.el9.noarch -> nginx-filesystem-1.22.1-2.el9.noarch
Downgrading : nginx-core-1.22.2-1.el9.x86_64 -> nginx-core-1.22.1-2.el9.x86_64
Downgrading : nginx-1.22.2-1.el9.x86_64 -> nginx-1.22.1-2.el9.x86_64
Verifying : nginx-filesystem-1.22.1-2.el9.noarch 1/3
Verifying : nginx-core-1.22.1-2.el9.x86_64 2/3
Verifying : nginx-1.22.1-2.el9.x86_64 3/3

Complete!

# 5. 验证nginx版本已回滚
# nginx -v
nginx version: nginx/1.22.1

# 6. 启动nginx服务
# systemctl start nginx

# 7. 验证nginx服务
# systemctl status nginx
● nginx.service – The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago
Process: 1234 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1235 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 1236 (nginx)
Tasks: 2 (limit: 4915)
Memory: 2.3M
CGroup: /system.slice/nginx.service
├─1236 “nginx: master process /usr/sbin/nginx”
└─1237 “nginx: worker process”

Mar 31 10:00:00 server1 systemd[1]: Starting The nginx HTTP and reverse proxy server…
Mar 31 10:00:00 server1 nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf syntax is ok
Mar 31 10:00:00 server1 nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 31 10:00:00 server1 systemd[1]: Started The nginx HTTP and reverse proxy server.

# 8. 测试nginx服务
# curl http://localhost

Welcome to nginx!

If you see this page, nginx web server is successfully installed and
working. Further configuration is required.

For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.

Thank you for using nginx.

4.2 内核版本回滚实战案例

4.2.1 内核版本回滚

# 1. 查看当前内核版本
# uname -r
5.14.0-284.11.1.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
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

# 3. 查看GRUB引导项
# grubby –info=ALL
index=0
kernel=”/boot/vmlinuz-5.14.0-284.11.1.el9_2.x86_64″
args=”ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet”
root=”/dev/mapper/rhel-root”
initrd=”/boot/initramfs-5.14.0-284.11.1.el9_2.x86_64.img”
title=”Red Hat Enterprise Linux (5.14.0-284.11.1.el9_2.x86_64) 10.0 (Plow)”
id=”rhel-5.14.0-284.11.1.el9_2.x86_64″

index=1
kernel=”/boot/vmlinuz-5.14.0-284.11.0.el9_2.x86_64″
args=”ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet”
root=”/dev/mapper/rhel-root”
initrd=”/boot/initramfs-5.14.0-284.11.0.el9_2.x86_64.img”
title=”Red Hat Enterprise Linux (5.14.0-284.11.0.el9_2.x86_64) 10.0 (Plow)”
id=”rhel-5.14.0-284.11.0.el9_2.x86_64″

# 4. 设置默认引导项为旧内核
# grubby –set-default-index=1
index 1

# 5. 验证默认引导项
# grubby –default-index
1

# 6. 重启系统以使用旧内核
# reboot

# 7. 验证内核版本已回滚
# uname -r
5.14.0-284.11.0.el9_2.x86_64

4.3 软件包版本回滚故障排查与解决

4.3.1 依赖冲突问题

# 问题现象:软件包版本回滚失败,依赖冲突
# 分析步骤:

# 1. 查看错误信息
# dnf downgrade 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
================================================================================
Downgrading:
nginx x86_64 1.22.1-2.el9 @System 35 k
nginx-core x86_64 1.22.1-2.el9 @System 540 k
nginx-filesystem noarch 1.22.1-2.el9 @System 11 k

Transaction Summary
================================================================================
Downgrade 3 Packages

Total download size: 0
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Downgrading : nginx-filesystem-1.22.2-1.el9.noarch -> nginx-filesystem-1.22.1-2.el9.noarch
Downgrading : nginx-core-1.22.2-1.el9.x86_64 -> nginx-core-1.22.1-2.el9.x86_64
Downgrading : nginx-1.22.2-1.el9.x86_64 -> nginx-1.22.1-2.el9.x86_64
Verifying : nginx-filesystem-1.22.1-2.el9.noarch 1/3
Verifying : nginx-core-1.22.1-2.el9.x86_64 2/3
Verifying : nginx-1.22.1-2.el9.x86_64 3/3

Complete!

# 2. 使用–allowerasing选项
# dnf downgrade –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
================================================================================
Downgrading:
nginx x86_64 1.22.1-2.el9 @System 35 k
nginx-core x86_64 1.22.1-2.el9 @System 540 k
nginx-filesystem noarch 1.22.1-2.el9 @System 11 k

Transaction Summary
================================================================================
Downgrade 3 Packages

Total download size: 0
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Downgrading : nginx-filesystem-1.22.2-1.el9.noarch -> nginx-filesystem-1.22.1-2.el9.noarch
Downgrading : nginx-core-1.22.2-1.el9.x86_64 -> nginx-core-1.22.1-2.el9.x86_64
Downgrading : nginx-1.22.2-1.el9.x86_64 -> nginx-1.22.1-2.el9.x86_64
Verifying : nginx-filesystem-1.22.1-2.el9.noarch 1/3
Verifying : nginx-core-1.22.1-2.el9.x86_64 2/3
Verifying : nginx-1.22.1-2.el9.x86_64 3/3

Complete!

# 3. 预防措施
# – 检查软件包依赖关系
# – 在测试环境先测试回滚
# – 使用事务历史回滚

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

Part05-风哥经验总结与分享

5.1 软件包版本回滚经验总结

软件包版本回滚经验总结:

  • 测试环境:在测试环境先测试回滚
  • 依赖检查:检查软件包依赖关系
  • 备份:备份重要数据和配置文件
  • 停止服务:停止相关服务
  • 验证:回滚后验证系统功能
  • 事务历史:利用事务历史回滚

5.2 软件包版本回滚检查清单

软件包版本回滚检查清单:

  • 回滚前:检查软件包历史版本、依赖关系
  • 回滚时:停止相关服务、使用-y选项自动确认
  • 回滚后:验证软件包版本已回滚
  • 服务启动:启动相关服务
  • 功能测试:测试系统功能是否正常
  • 故障排查:检查依赖关系、使用–allowerasing选项

5.3 软件包版本回滚相关工具推荐

软件包版本回滚相关工具推荐:

  • dnf:DNF软件包管理器
  • rpm:RPM包管理器
  • repoquery:查询软件包信息
  • grubby:管理GRUB引导项
  • yum-utils:YUM实用工具(兼容DNF)
风哥提示:dnf downgrade命令可以回滚软件包到之前的版本,解决升级后出现的问题。建议在升级前备份重要数据和配置文件,利用事务历史回滚。

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

联系我们

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

微信号:itpux-com

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