Sub topic 10
Reading History and Undoing Changes
Prerequisites and the rollback model
This chapter assumes you can use a Linux shell, run commands with sudo, and identify whether a server uses Debian/Ubuntu or a RHEL-family distribution. You should also know that a package manager changes both packages and their dependencies.
Package history tells you what a package manager attempted. It does not create a complete system snapshot. A transaction can be reversed only when the required package versions and dependencies are still available. Configuration files, database changes, and files created by applications may remain changed.
Inspecting DNF history
DNF records package transactions with an identifier. Start by listing recent transactions:
sudo dnf history list
sudo dnf history info lastThe list shows actions such as install, upgrade, erase, and downgrade. The detailed view shows the packages affected, the command used, and whether the transaction completed.
To inspect a specific transaction, use its ID:
sudo dnf history info 42Replace 42 with the ID from dnf history list.
If a recent transaction caused a problem, ask DNF to reverse that transaction:
sudo dnf history undo 42Review the proposed changes before confirming. The undo operation might downgrade packages, remove packages that were installed by the transaction, or install packages that were removed. It might fail if the old package versions no longer exist in enabled repositories.
A failed transaction is different from a completed transaction. Failed transactions can leave partial changes, so inspect the detailed history and package state before trying another repair.
flowchart TD
A[Identify the problem] --> B[Read dnf history]
B --> C[Inspect transaction details]
C --> D{Old packages available?}
D -- Yes --> E[Review dnf history undo]
E --> F[Confirm and verify]
D -- No --> G[Use backup or package-specific repair]Reading APT and DPKG records
APT does not provide an equivalent history undo command. Read its transaction log instead:
grep -E 'Start-Date|Commandline|Upgrade:|Install:|Remove:' \
/var/log/apt/history.logAPT may rotate older logs into files such as /var/log/apt/history.log.1.gz. The lower-level DPKG log records package status changes:
grep -E ' upgrade | install | remove ' /var/log/dpkg.logFor a package-specific investigation, check versions available from configured repositories:
apt-cache policy nginxIf the required older version is available, you can request it explicitly:
sudo apt install nginx=1.24.0-2ubuntu7The exact version must appear in the apt-cache policy output. Downgrading one package may create dependency conflicts, so test the proposed transaction and verify the service afterwards.
Common errors and safe recovery
Do not treat dnf history undo as a full rollback. It cannot restore arbitrary application data or undo changes made outside the package transaction. Do not delete package logs while troubleshooting. They are often the only record of what happened.
For high-risk changes, capture the transaction ID, save relevant configuration files, and use a virtual machine snapshot or filesystem backup when available. After an undo or downgrade, check package state, service status, application logs, and a real application request.
Exercise
Does APT have a command that reverses its last package transaction, and what should you use instead?
Show answer
No. Read /var/log/apt/history.log and /var/log/dpkg.log, determine the needed package versions, and use a tested package-specific install or a backup when appropriate.