Sub topic 10
Reading history and undoing changes
This chapter assumes you can run apt and dnf with sudo, know the difference between installing and upgrading a package, and have already applied updates on a test machine. Patching sometimes breaks something. You then need to answer two questions: what changed, and can I put it back? The tools differ here. dnf records every transaction and can undo it. apt keeps a log but has no undo command.
What a transaction record tells you
A transaction is one run of the package manager that installs, upgrades or removes a set of packages. A useful record answers four questions:
- When did it run, and which command started it?
- Which packages changed, and from which version to which version?
- Who ran it (a person, or an automatic job)?
- Did it succeed?
You need these answers during an incident ("the web service broke at 03:10, what changed at 03:00?") and when you plan a rollback.
Reading history with dnf
dnf stores each transaction with a numeric ID. List them first:
sudo dnf history listThe output has columns for ID, command line, date and time, and the action (Install, Upgrade, Removed and so on). Pick the ID you care about and inspect it:
sudo dnf history info last
sudo dnf history info 42last means the most recent transaction. The detail view shows the command line, the user, the return code, and every package that changed with its old and new version. Compare the date against the time the problem started.
Undoing a change with dnf
dnf can reverse a transaction:
sudo dnf history undo lastdnf works out the opposite operation for each package. An upgrade becomes a downgrade, an install becomes a removal, and a removal becomes an install. You can also target a specific ID with sudo dnf history undo 42. Its sibling, sudo dnf history rollback 42, reverts everything done after transaction 42, which is a larger step. Read the plan dnf prints and answer the confirmation prompt carefully.
An undo only works if the old versions are still available. If the repository has dropped the previous build, dnf reports that it cannot find the package and refuses to continue. Mirrors of fast-moving repositories usually keep only the newest build. Do not rely on undo being possible months later.
Reading history with apt
apt has no history command and no undo. It writes plain-text logs instead. The main one is /var/log/apt/history.log, which records each apt run:
grep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.logEach entry has Start-Date, Commandline, and lists such as Install:, Upgrade: and Remove:. The Upgrade: line shows each package with the old and new version in parentheses. Older entries are rotated into compressed files such as history.log.1.gz. Read them with zgrep:
zgrep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.log.*.gzTwo other logs help:
/var/log/apt/term.logholds the terminal output of each run, including package script messages and errors./var/log/dpkg.logrecords each low-level step dpkg took, with a timestamp per package. Use it to find the exact minute a package was configured.
Unattended runs are logged separately in /var/log/unattended-upgrades/, so check there when a machine patched itself.
Undoing a change with apt
Because apt cannot undo, you downgrade by hand. Take the old version from history.log, then check which versions the repositories offer:
apt list -a nginx
apt-cache policy nginxInstall a specific older version by naming it:
sudo apt install nginx=1.24.0-2ubuntu7apt may refuse if the downgrade breaks dependencies, and it asks for confirmation when it has to go backwards. Hold the package afterwards so the next upgrade does not undo your fix:
sudo apt-mark hold nginxRelease the hold when a fixed version arrives. The previous chapter's hold and version-lock commands apply here.
If the old version is no longer in the repository, look in the local cache at /var/cache/apt/archives/. Cached .deb files stay there until you clean the cache, and you can install one with sudo apt install ./package.deb or sudo dpkg -i package.deb. Running apt clean removes them, which is a reason to keep the cache until a patch run has proved stable.
Comparing the two tools
| Question | dnf | apt |
|---|---|---|
| List past transactions | `dnf history list` | Read `/var/log/apt/history.log` |
| Details of one change | `dnf history info ID` | Read the matching entry in `history.log` |
| Undo the last change | `dnf history undo last` | Not available |
| Go back to an older state | `dnf history rollback ID` | Install each old version by hand |
| Needs old packages available | Yes | Yes |
Which tool fits which situation
Common errors and edge cases
- Undo fails with "no package available". The repository removed the old build. Check the cache (
/var/cache/dnfor/var/cache/apt/archives), an internal mirror, or restore from a backup. - The wrong transaction is undone.
lastmay refer to a transaction you did not expect, such as one from an automatic job. Rundnf history info lastfirst and check the date and the command line. - A downgrade is repaired by the next upgrade. Without a hold or versionlock, the package returns to the newest version on the next run, and the problem returns with it.
- Kernel changes need extra care. Undoing a kernel upgrade does not change the kernel that is running. You must reboot, and the older kernel must still be installed. Both tools keep several kernels by default, so you can usually pick the older one at the boot menu.
- Logs rotate away. apt history files are rotated and eventually deleted. If audit needs matter, copy them to central log storage.
- Partial transactions. If a run is interrupted, apt may leave packages half-configured. Run
sudo dpkg --configure -aand thensudo apt install -f. On dnf,dnf history infoshows whether the transaction finished, andsudo dnf checklooks for problems in the package database.
Building a safe habit
Before each patch run, note the current state and know your way back:
- Record the newest transaction ID with
sudo dnf history list, or the lastStart-Datein apt'shistory.log. - Take a virtual machine snapshot or a backup if the server holds data that matters.
- Apply the updates.
- If something fails, read the history first, then undo or downgrade only the packages involved.
- Hold the package if you must stay on the old version, and write down why.
Exercise
A server running Ubuntu was patched last night and an application now fails. You want to put back the previous version of one package, "libfoo". List the steps, and say why you should hold the package afterwards.
Show answer
Find the old version in /var/log/apt/history.log (the Upgrade: line shows old and new versions). Confirm the old version is still offered with apt list -a libfoo or apt-cache policy libfoo, or find its .deb in /var/cache/apt/archives/. Install it with sudo apt install libfoo=OLD_VERSION. Then run sudo apt-mark hold libfoo. Without the hold, the next apt upgrade installs the newest version again and the failure returns. Release the hold with apt-mark unhold when a fixed version is published.
Exercise
On a RHEL-family server, dnf history list shows transaction 57 as a kernel and openssl upgrade from yesterday, and transaction 58 as an unrelated package install made this morning. You want to reverse only transaction 57. Which command do you use, and why not dnf history undo last? What should you check first?
Show answer
Use sudo dnf history undo 57. The word last would refer to transaction 58, which is the wrong one. Run sudo dnf history info 57 first to confirm which packages changed, and make sure the old package versions are still available in the repositories or cache. After undoing a kernel change, reboot and confirm the running kernel with uname -r.