Sub topic 8
Deciding when a reboot or service restart is needed
This chapter assumes you can run apt or dnf upgrades with sudo, read systemctl status output, and have a Debian/Ubuntu or RHEL-family test machine. Installing an update replaces files on disk. It does not change the programs already running in memory. This chapter shows how to find out which running programs are still using the old code, and how to bring them up to date.
Why installed does not mean active
When a package manager replaces a library such as libssl, it writes a new file and unlinks the old one. A running process still holds the old file open or mapped. It keeps using the old, possibly vulnerable code until it is restarted. The kernel works the same way. The new kernel image is on disk, but the machine keeps running the old one until it boots.
A patch run therefore has two steps. First you install the updates. Then you make the running system use them. A server can show "0 updates pending" and still be exposed to a fixed vulnerability.
There are three levels of action, from least to most disruptive:
| Level | What it fixes | Typical trigger | Impact |
|---|---|---|---|
| Restart one service | Daemon using an old binary or library | Updated `nginx`, `openssl`, `libc` users | Brief outage for that service |
| Re-execute systemd | The init process (PID 1) itself | Updated `systemd` package | None visible |
| Reboot | Kernel, firmware, or core libraries used by almost everything | New kernel, `glibc`, `systemd` | Whole server down for a while |
Checking whether a reboot is required
Debian and Ubuntu
Packages that need a reboot, mainly kernels and some core libraries, run a hook that creates the flag file /var/run/reboot-required. The package names go into /var/run/reboot-required.pkgs.
if [ -f /var/run/reboot-required ]; then
echo "Reboot required"
cat /var/run/reboot-required.pkgs 2>/dev/null
fiOn Ubuntu the hook comes with update-notifier-common, which is normally installed. On a minimal Debian install the file may never appear, because the hook is missing. Absence of the file then proves nothing, so compare the running kernel yourself (see below).
RHEL-family systems
dnf needs-restarting -r compares the install time of core packages with the boot time. These packages include the kernel, glibc, systemd, and firmware. The command is part of dnf-plugins-core, and dnf5 includes it.
sudo dnf needs-restarting -r
echo "exit code: $?"The exit code is the useful part. An exit code of 1 means a reboot is recommended, and 0 means it is not. That makes the command easy to use in scripts and monitoring checks.
Checking the kernel yourself
Neither tool is a complete oracle, so know the manual check. Compare the running kernel with the newest installed one:
uname -r
rpm -q kernel --last | head -n 1 # RHEL family
ls -1 /boot/vmlinuz-* | sort -V | tail -n 1 # Debian/UbuntuIf uname -r differs from the newest installed version, the server is running an older kernel than the one on disk.
Finding services that need a restart
A reboot is not always needed. If only a library changed, restarting the services that use it is enough. The signal is a process with a mapped file that no longer exists on disk. The kernel marks such mappings with (deleted).
You can see this directly:
sudo grep -l ' (deleted)' /proc/[0-9]*/maps 2>/dev/nullThis prints the maps file of every affected process, so the PID is in the path. It is noisy, because temporary files and memory-backed files also show as deleted. Use the distribution tools below for real work.
RHEL-family: needs-restarting
Without options, dnf needs-restarting lists processes started before an update replaced files they use:
sudo dnf needs-restarting
sudo dnf needs-restarting -sThe first command shows PIDs and command lines. The -s form lists systemd service names, which is easier to act on. The -s option needs a reasonably recent dnf-plugins-core, so check dnf needs-restarting --help on older releases.
Debian and Ubuntu: needrestart
needrestart is a separate package. It is installed by default on recent Ubuntu Server releases and available on Debian. It inspects running processes and maps them to services, and it also checks for kernel and microcode changes.
sudo apt install needrestart
sudo needrestart -r lThe -r l option lists what needs a restart and changes nothing. After apt upgrade, needrestart can run through a package hook and offer an interactive menu. In scripts and automation, set the mode explicitly so a prompt cannot hang the run: -r a restarts automatically, and -r l only lists.
Restarting services safely
Once you have the list, restart services one at a time. Check each one before you move on:
sudo systemctl restart nginx
systemctl is-active nginx
sudo systemctl status nginx --no-pagerOrder and impact matter:
- Restart only what is on the list. Do not restart everything.
- Prefer
systemctl reloadwhen the service supports it, but note that a reload re-reads configuration. It does not load a new binary or library. After a package update you usually need a real restart. - Be careful with
sshd. Restarting it keeps existing sessions alive, but a broken config would lock you out of new ones. Test withsudo sshd -tfirst and keep your current session open. - After a
systemdpackage update, runsudo systemctl daemon-reexecso PID 1 reloads itself. This is cheap and does not stop services. - Services with long-lived connections, such as databases, may need a maintenance window even for a "quick" restart.
Packaging policy differs between the families. Debian and Ubuntu packages usually restart their own daemon in the post-install script, so a patched nginx often restarts during the upgrade. RHEL packages restart some services in package scriptlets and leave many alone. Do not assume either behavior. Check with the tools above after every run.
Deciding on a reboot
Reboot when:
- The kernel changed and the fix matters to you, for example a privilege-escalation or network-stack fix.
glibc,systemd, or firmware changed, and the tool flags a reboot.- Too many services need a restart. Rebooting is then simpler and more reliable than hand-restarting twenty daemons.
Defer a reboot to a planned window when the server cannot be spared right now. This is a risk decision. Record the date, the packages involved, and the exposure you are accepting.
Live kernel patching, such as Canonical Livepatch or RHEL kpatch, applies some kernel security fixes without a reboot. It covers selected fixes only. It does not replace regular reboots, because it does not update userspace libraries or firmware.
Before you reboot:
- Confirm the new kernel is installed and the bootloader entry exists.
- Check that
/etc/fstabhas no broken entries. A bad mount can drop the machine to emergency mode. - Make sure you have out-of-band access (console or cloud serial console) in case SSH does not come back.
- For clustered or load-balanced servers, remove the node from rotation first, then reboot one node at a time.
Reboot with sudo systemctl reboot, and afterwards confirm the result:
uname -r
systemctl --failed
sudo dnf needs-restarting -r; echo "exit code: $?" # RHEL family
test -f /var/run/reboot-required && echo "still required" # Debian/UbuntuCommon errors
- Trusting the package count. "No updates pending" says nothing about running processes. Always run the reboot and restart checks.
- Reloading instead of restarting. A reload does not pick up a replaced binary or library.
- Ignoring the flag file.
/var/run/reboot-requiredstays until the next boot. Monitoring that never checks it lets servers run old kernels for months. - Unattended restarts hanging. Interactive needrestart prompts can stall a script. Set the mode explicitly.
- Rebooting a whole fleet at once. If the new kernel fails to boot, every node is down at the same time.
Both automatic update tools can reboot on their own. unattended-upgrades has Unattended-Upgrade::Automatic-Reboot and Automatic-Reboot-Time. dnf-automatic has a reboot setting in the [commands] section. Leave both off until you trust your checks, and see the automation chapter for the settings.
Exercise
After sudo dnf upgrade, dnf check-update reports nothing pending, but sudo dnf needs-restarting -r exits with code 1 and dnf needs-restarting -s lists nginx.service. What do you do, and in what order?
Show answer
The exit code 1 means a reboot is recommended, because a core package such as the kernel or glibc changed. The nginx entry means that service is still running old files. Nothing pending only means the packages are installed. Plan the reboot in a maintenance window, since it also restarts nginx. If the reboot must wait, restart nginx now with sudo systemctl restart nginx and check systemctl is-active nginx. Record that the reboot is still outstanding. After the reboot, check uname -r, systemctl --failed, and that dnf needs-restarting -r exits with 0.
Exercise
On an Ubuntu server, /var/run/reboot-required.pkgs lists linux-image-6.8.0-45-generic, but uname -r shows 6.8.0-40-generic. Is a reboot needed, and how can you confirm without rebooting?
Show answer
Yes. The running kernel (6.8.0-40) is older than the installed one (6.8.0-45), so the fixes in the new kernel are not active. Confirm by comparing uname -r with ls -1 /boot/vmlinuz-* | sort -V | tail -n 1. Both the flag file and the version mismatch point to the same conclusion.