Sub topic 14
Logs and verifying a server after patching
A patch run that exits without errors does not prove the server is healthy. This chapter covers where apt and dnf record what they did, and how to confirm afterwards that the server still does its job. It assumes you can read systemd unit status, use journalctl, and have already applied updates with apt or dnf as in the earlier chapters.
Two questions to answer after every patch run
Every post-patch review answers two questions:
- What changed? Which packages moved from which version to which, and did any install step report an error?
- Is it still working? Are the services running, are the ports open, does the application answer, and is the machine running the new code?
The first question is answered from package manager logs. The second needs checks you run yourself, ideally compared against a baseline you took before patching.
Take a baseline first
Comparison is the cheapest way to spot damage. Before you patch, save the list of running services and listening ports.
mkdir -p ~/patch-check
systemctl list-units --type=service --state=running --no-legend --plain \
| awk '{print $1}' | sort > ~/patch-check/services.before
sudo ss -tlnpH | awk '{print $4, $6}' | sort > ~/patch-check/ports.before
uname -r > ~/patch-check/kernel.beforeAfter patching, repeat the same commands with .after file names and compare with diff. A service that was running before and is gone now is the most common sign of a bad patch.
Reading the Debian and Ubuntu logs
apt and dpkg keep separate logs, and each answers a different question.
| File | Contents | Use it to |
|---|---|---|
| `/var/log/apt/history.log` | One block per apt run: start time, command line, and the lists of upgraded, installed and removed packages | See what a run changed |
| `/var/log/apt/term.log` | The terminal output of each run, including maintainer scripts | Find errors and service restart messages |
| `/var/log/dpkg.log` | One line per package state change, with old and new versions | Build an exact timeline |
| `/var/log/unattended-upgrades/unattended-upgrades.log` | What the automatic job decided and did | Audit unattended runs |
Rotated logs are compressed, so older runs are in files such as history.log.1.gz. Use zgrep to search them alongside the current file.
grep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.log
zgrep -h ' upgrade ' /var/log/dpkg.log*
grep -iE 'error|warning|failed' /var/log/apt/term.logA line in dpkg.log looks like this:
2026-11-28 04:10:01 upgrade openssl:amd64 3.0.11-1~deb12u1 3.0.11-1~deb12u2The two version strings are the old and the new version. Keep that output with your change record, because it is the evidence of exactly what moved.
Reading the RHEL-family logs
dnf records every transaction in its history database and also writes a text log.
sudo dnf history list
sudo dnf history info last
rpm -qa --last | head -n 20
sudo grep -iE 'error|warning|scriptlet' /var/log/dnf.logdnf history info last shows the packages altered, the return code, and any scriptlet output. rpm -qa --last lists installed packages by install time, which works even when the dnf history is unavailable. On systems that use dnf5, such as recent Fedora, the text log is /var/log/dnf5.log.
For automatic updates, dnf-automatic runs as a systemd service, so its output is in the journal:
systemctl list-timers dnf-automatic.timer
sudo journalctl -u dnf-automatic.service --since "2 days ago" --no-pagerThe [emitters] section of /etc/dnf/automatic.conf controls whether the report also goes to standard output, email or the message of the day.
Checking systemd and the journal
A package upgrade runs scripts that restart services. If a restart fails, the failure shows up in systemd, not in the package manager's exit code.
systemctl --failed
sudo journalctl -p err -b --no-pager | tail -n 30
sudo journalctl -u nginx --since "30 minutes ago" --no-pagersystemctl --failed lists units in the failed state. journalctl -p err -b shows messages of priority error or worse since the last boot. The third command narrows to one service and a time window, which you set to cover the patch run.
If nothing is listed as failed but a service you expect is missing, compare against your baseline. A unit that was stopped by an upgrade and never restarted is not "failed", it is simply inactive.
Is the new code actually running?
Installing a package replaces files on disk. Processes that started earlier keep using the old library or binary until they restart. Verification therefore includes checking for stale processes and for an old kernel.
On Debian and Ubuntu:
if [ -f /var/run/reboot-required ]; then
echo "Reboot required"
cat /var/run/reboot-required.pkgs 2>/dev/null
fi
sudo apt install needrestart
sudo needrestart -r lneedrestart -r l only lists services that use outdated libraries. It does not restart anything.
On RHEL-family systems:
sudo dnf needs-restarting -r
echo "exit code: $?"
sudo dnf needs-restarting -sThe -r form exits with 1 when a reboot is recommended. The -s form lists services that should be restarted. On RHEL 8 the command comes from the dnf-utils package. On RHEL 9 it is part of the dnf plugins.
To compare the running kernel with the newest installed one:
uname -r
dpkg -l 'linux-image-[0-9]*' | awk '/^ii/ {print $2, $3}'
rpm -q kernel --last | head -n 1Use the dpkg line on Debian and Ubuntu, and the rpm line on RHEL-family systems. If uname -r shows an older version than the newest one installed, the kernel fix is not active yet.
Verifying package file integrity
Both families can check installed files against the package database. This is useful when a patch run was interrupted, or when you suspect a file was changed by hand.
sudo rpm -V openssh-server
sudo dpkg --verify openssh-serverA line of output means a file differs from the packaged version. Configuration files under /etc often differ on purpose, so read each line before reacting. No output means the package files match.
Checking the application, not just the system
A service can be active and still be broken. Add a check that asks the application a real question, such as a request to a health URL or a simple database query. The script below combines the system checks in this chapter with an optional HTTP check. Run it with sudo.
#!/usr/bin/env bash
# post-patch-check.sh: run as root after patching.
# Optional: HEALTH_URL=http://localhost:8080/health ./post-patch-check.sh
set -u
fail=0
echo "== Running kernel =="
uname -r
echo "== Failed units =="
failed=$(systemctl --failed --no-legend --plain)
if [ -n "$failed" ]; then
echo "$failed"
fail=1
else
echo "none"
fi
echo "== Reboot status =="
if [ -f /var/run/reboot-required ]; then
echo "Reboot required"
cat /var/run/reboot-required.pkgs 2>/dev/null
fail=1
elif command -v dnf >/dev/null 2>&1; then
if ! dnf needs-restarting -r >/dev/null 2>&1; then
echo "Reboot recommended (or needs-restarting is unavailable)"
fail=1
else
echo "no reboot needed"
fi
else
echo "no reboot needed"
fi
echo "== Errors in the journal since boot =="
journalctl -p err -b --no-pager | tail -n 10
if [ -n "${HEALTH_URL:-}" ]; then
echo "== Application health check =="
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$HEALTH_URL")
echo "HTTP $code from $HEALTH_URL"
[ "$code" = "200" ] || fail=1
fi
if [ "$fail" -eq 0 ]; then
echo "RESULT: all checks passed"
else
echo "RESULT: review the items above"
fi
exit "$fail"The script exits with 0 only when every check passed, so a patch tool or scheduler can act on the result.
Common errors
- Reading only the exit code. apt and dnf can finish successfully while a service restart in a maintainer script failed. Always check
systemctl --failedand the journal as well. - Checking too early. Some services take a minute to come back. Wait for them to settle before concluding they are down.
- Searching only the current log. After a log rotation, the run you want is in a compressed file. Use
zgrep. - Treating
rpm -Vordpkg --verifyoutput as damage. Edited configuration files show up on purpose. - No baseline. Without a before-state, you cannot tell whether a missing service was running yesterday.
What to do when a check fails
- Identify the package involved from the time window in the package log.
- Read the unit's journal for the actual error message.
- Decide whether to fix forward, for example by correcting a configuration file, or to revert.
- To revert on dnf, use
sudo dnf history undo last. On apt there is no undo command, so install the previous version explicitly, and place a hold on it so the next run does not upgrade it again.
Exercise
After an unattended upgrade at 03:00, your monitoring shows the web service is down, but /var/log/unattended-upgrades/unattended-upgrades.log reports a clean run. Name three checks, with commands, that would show what happened on a Debian or Ubuntu server.
Show answer
First, systemctl --failed and sudo journalctl -u nginx --since "03:00" --no-pager show whether the unit failed or was left stopped, and why. Second, grep -E 'Start-Date|Upgrade:' /var/log/apt/history.log (or zgrep -h ' upgrade ' /var/log/dpkg.log*) identifies which packages changed at that time. Third, grep -iE 'error|failed' /var/log/apt/term.log shows whether a maintainer script failed to restart the service. A clean unattended-upgrades log only means the upgrade step finished. It does not say the restart succeeded.
Exercise
A RHEL server shows uname -r as 5.14.0-362 while rpm -q kernel --last | head -n 1 shows a newer kernel. What does this mean, and which command confirms that a reboot is recommended?
Show answer
The newer kernel is installed but not running, so its fixes are not active. sudo dnf needs-restarting -r confirms it: it exits with 1 when a reboot is recommended, which you can display with echo "exit code: $?".