Blog articles

How to Rollback Changes with the DNF History Command

November 06, 2022

There’s many reasons to love Linux, such as its package management and the incredible amount of software at your fingertips. One such package manager is DNF, which is used by several Linux distributions like Red Hat, Fedora, and CentOS. DNF, often referred to as Dandified YUM, is an updated version of the YUM package manager, and it provides several improvements over its predecessor. These include reduced memory usage, more efficient dependency resolution, and increased performance. 

What is the DNF history command?

Among the many uses of the DNF command, like installing and upgrading software, is the ability to interact with past transactions using the dnf history command. The DNF history command will allow you to view all the transactions that have been made using DNF and interact with them. 

What is the DNF history rollback command?

This helpful feature includes the ability to rollback changes using the dnf history rollback command. This command will undo all transactions performed after the specified transaction. The transaction can be specified using a transaction specification, which is the ID number given to the transaction based on when it occurred. 

Another way to specify the transaction is with the package file specification, which is essentially the name of the package (more specifically the name, epoch, version, release, and architecture commonly known as NERVA). If more than one transaction is found for the given package file specification, it will use the latest transaction. Using the transaction ID ensures that the correct transaction is selected. 

What is the difference between DNF history rollback and undo commands? 

The difference between DNF history rollback and undo commands is that undo is used for a single transaction, whereas rollback is used to undo all the transactions that have occurred after the specified transaction. Both commands are great to have in your toolkit but have different use cases. 

How do you use the DNF history rollback command? 

Let’s walk through an example to illustrate how the DNF history rollback works:

sudo dnf history --reverse

sudo dnf history --reverse

The history command will show us a list of all the transactions that have occurred with DNF and the “--reverse” option will show the latest transactions at the bottom of the output. At transaction 34, we have the installation of the nginx service. Let’s say that we wanted to rollback all the transactions that occurred after it. For that, we’ll use the dnf history rollback command.

sudo dnf history rollback 34

sudo dnf history rollback 34

The list has been truncated, but, as you can see, it is going to remove all the packages and dependencies that occurred after the installation of nginx (ID 34). Now, if we run the dnf history command again.

sudo dnf history --reverse

sudo dnf history --reverse

 

We see that all the transactions remain, but there is a new transaction with the ID 40 letting us know that we performed a rollback to ID 34. 

This is a great way to test out a bunch of different software, while still being able to restore the host to a clean state. We hope you enjoyed this post about the dnf history rollback command. 

If you would like to learn more about the dnf history command as well as the many other features provided by DNF, we highly recommend you check out the course Package Management on Red Hat Linux with DNF and RPM.