Skip to main content

Linux and servers · Intermediate · 14 min

Know when Ubuntu needs a reboot or a service restart after updates

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · needrestart 3.11 · update-notifier-common 3.207 · systemd 259

You will read the reboot-required flag, list the services and processes still running old library code with needrestart, compare the running kernel with the newest installed one, and plan a reboot window with a small check script of your own.

On this page

Installing an update and running the updated code are two different things on Linux. apt upgrade replaces files on disk, but a program that was already running keeps the old copy of its libraries in memory until it restarts. The kernel is the extreme case: a new kernel package sits in /boot doing nothing until the next boot. So a server can report "0 updates pending" and still run code with a known hole in it.

Ubuntu gives you three signals to close that gap. A flag file says a package asked for a reboot. needrestart names the services that still use replaced libraries. And uname -r tells you which kernel is really running, which you can compare with the newest one installed. In this tutorial you read each signal by hand, then fold all three into a short script you can run after every patch session, and finish by planning the reboot itself.

If you pay for data by the gigabyte, for example on a prepaid mobile connection while you manage a server remotely, note that apt upgrade downloads every pending update at once. Run sudo apt upgrade without -y first to see the download size, and answer n if it is more than you want to spend now.

Step 1 · Install the tools that report pending restarts

Two packages do the reporting. update-notifier-common holds the script that package upgrades call to raise the reboot flag, and needrestart scans running processes for replaced files. Both are present on most Ubuntu Server installs; installing them again changes nothing if they are. lsof lets you look at the processes yourself.

Bash
sudo apt update
sudo apt install -y needrestart update-notifier-common lsof

needrestart also hooks itself into apt: /etc/apt/apt.conf.d/99needrestart runs it after every apt transaction. On Ubuntu, when it runs from that hook, its default is to restart affected services by itself, which is why an upgrade often ends with a few lines about restarting services. What it never does is reboot, and it skips a few services that are unsafe to restart in the middle of an upgrade, such as systemd-logind.

Check it worked

sudo needrestart -b | head -n 1 prints NEEDRESTART-VER: 3.11 (the version on Ubuntu 26.04), and ls /etc/apt/apt.conf.d/99needrestart prints the file name rather than No such file or directory.

Step 2 · Apply updates and read the reboot flag

Before you upgrade, note that every service already running keeps its current libraries in memory; the later steps find those processes.

List what will change, then install the pending updates:

Bash
apt list --upgradable 2>/dev/null
sudo apt upgrade -y

When a package that cannot take effect without a reboot is upgraded (a kernel, libc6, dbus, a CPU microcode package), its install script runs /usr/share/update-notifier/notify-reboot-required. That script writes two files: /var/run/reboot-required with a one-line message, and /var/run/reboot-required.pkgs with the name of the package that asked. Each package that asks adds its own line, so a single file can name several. Read both:

Bash
ls -l /var/run/reboot-required* 2>/dev/null || echo "No reboot flagged"
cat /var/run/reboot-required /var/run/reboot-required.pkgs 2>/dev/null || true

/var/run is a link to /run, which lives in memory, so both files vanish at the next boot. That is the point: the flag clears itself once the reboot it asked for has happened. The same file drives the *** System restart required *** line in the message you see when you log in over SSH.

Check it worked

If any update asked for a reboot, cat prints *** System restart required *** followed by one or more package names, such as libc6 or linux-image-7.0.0-15-generic (the version is an example). If it printed No reboot flagged, nothing you installed needs one. Treat the flag as a floor, not a full answer: it only knows about packages whose scripts call it.

Step 3 · List services running old code with needrestart batch mode

The reboot flag does not tell you about the dozens of services that use a library you have replaced. needrestart finds them. Its batch mode prints one fact per line, which suits scripts and monitoring:

Bash
sudo needrestart -b

The output looks something like this on a server that has upgraded libssl and a kernel (the version numbers are examples):

Text
NEEDRESTART-VER: 3.11
NEEDRESTART-KCUR: 7.0.0-14-generic
NEEDRESTART-KEXP: 7.0.0-15-generic
NEEDRESTART-KSTA: 3
NEEDRESTART-UCSTA: 1
NEEDRESTART-SVC: nginx.service
NEEDRESTART-SVC: ssh.service
  • KCUR is the running kernel and KEXP the one needrestart expects after a reboot.
  • KSTA is the kernel status: 0 unknown, 1 up to date, 2 an ABI-compatible upgrade is waiting, 3 a newer kernel version is waiting. Both 2 and 3 mean reboot.
  • UCSTA is the CPU microcode status: 0 unknown, 1 current, 2 a newer microcode is installed but not loaded, which also needs a reboot.
  • Each SVC line is a systemd service that still maps a deleted file. CONT and SESS lines, when present, name containers and login sessions that do the same.

For the same information in plain sentences, use list mode:

Bash
sudo needrestart -r l

List mode (-r l) only reports; it never restarts anything. On a virtual machine you may see Failed to retrieve available kernel versions. when the provider boots its own kernel rather than one from /boot. In that case use the kernel check in the next steps and your provider's console.

Check it worked

needrestart -b prints at least the NEEDRESTART-VER, NEEDRESTART-KCUR and NEEDRESTART-KSTA lines. If it prints no SVC lines, the apt hook already restarted everything that was safe to restart. If it prints some, those are the services still running old code, and sudo needrestart -r l names them in a sentence.

Step 4 · Find processes that still use deleted libraries

needrestart only reports processes it can tie to a service or a session. A program you started by hand in tmux, a cron job that runs for hours, or a process from a container runtime can slip past it. The kernel itself keeps the evidence: when dpkg replaces a library, the old file is deleted, and every process that still maps it shows the path with (deleted) in its memory map.

lsof marks those mappings DEL. List each process with the deleted libraries it holds:

Bash
sudo lsof -n 2>/dev/null | awk '$4 == "DEL" && /\.so/ {print $1, $2, $NF}' | sort -u

Each line is a command name, a PID and a library path, something like sleep 2877 /usr/lib/x86_64-linux-gnu/libc.so.6. Ask needrestart to show its own reasoning too; its verbose output names every process with a deleted file, including the ones it then decides are not services:

Bash
sudo needrestart -v -r l 2>&1 | grep 'uses deleted' || echo "No process uses a deleted file"

To fix a hand-started process, stop and start it again. For a service, sudo systemctl restart <service> (replace <service> with the name from SVC) loads the new libraries. Restart ssh.service from a session you already have open; existing SSH sessions survive the restart.

Check it worked

The lsof list includes the PIDs of long-running programs started before the upgrade, each beside a library such as libc.so.6. After you restart one of them, its new PID is no longer in the list. A list that is empty means every running process uses the libraries on disk.

Step 5 · Compare the running kernel with the newest installed one

The kernel is the one component that only a reboot replaces. Print the kernel that is running now, then the kernel images on disk sorted by version:

Bash
uname -r
ls -1 /boot/vmlinuz-* | sort -V
apt list --installed 'linux-image-*' 2>/dev/null

sort -V sorts by version number, so the last line is the newest kernel installed. If it does not end with the exact string uname -r printed, the server is running an older kernel and the fixes in the newer one are not active. Ubuntu keeps the running kernel and the previous one installed, so seeing two or three images is normal; what matters is which one is last.

Check it worked

You can name both versions. For example (the versions are examples from the 7.0 kernel series Ubuntu 26.04 ships), uname -r prints 7.0.0-14-generic and the last vmlinuz line is /boot/vmlinuz-7.0.0-15-generic: a reboot is due. When the last line matches uname -r, the kernel is current.

Step 6 · Put the checks in one script

Running four commands after every patch session is easy to forget. Put them in one script that prints a line per signal and sets an exit code a monitoring agent or a cron job can act on: 0 nothing to do, 1 restart services, 2 reboot.

Bash
mkdir -p ~/reboot-check
cat > ~/reboot-check/check-reboot.sh <<'EOF'
#!/bin/sh
# check-reboot.sh: does this server need a reboot or service restarts after updates?
# Run with sudo. Exit code: 0 nothing to do, 1 restart services, 2 reboot.
result=0

if [ -f /var/run/reboot-required ]; then
  pkgs=$(sort -u /var/run/reboot-required.pkgs 2>/dev/null | xargs)
  echo "REBOOT: flagged by ${pkgs:-an unnamed package}"
  result=2
else
  echo "REBOOT: not flagged"
fi

running=$(uname -r)
newest=$(ls /boot/vmlinuz-* 2>/dev/null | sed 's|^/boot/vmlinuz-||' | sort -V | tail -n 1)
if [ -z "$newest" ]; then
  echo "KERNEL: running $running, no kernel images found in /boot"
elif [ "$newest" != "$running" ]; then
  echo "KERNEL: running $running, newest installed is $newest"
  result=2
else
  echo "KERNEL: running the newest installed kernel ($running)"
fi

services=$(needrestart -b 2>/dev/null | sed -n 's/^NEEDRESTART-SVC: //p' | xargs)
if [ -n "$services" ]; then
  echo "SERVICES: $services"
  [ "$result" -eq 0 ] && result=1
else
  echo "SERVICES: none flagged by needrestart"
fi

pids=$(grep -lE '\.so[.0-9]* \(deleted\)$' /proc/[0-9]*/maps 2>/dev/null | cut -d/ -f3 | sort -n | xargs)
if [ -n "$pids" ]; then
  echo "DELETED LIBRARIES IN USE: PIDs $pids"
  [ "$result" -eq 0 ] && result=1
else
  echo "DELETED LIBRARIES IN USE: none"
fi

exit "$result"
EOF
chmod +x ~/reboot-check/check-reboot.sh

The script reads every process's memory map, which only root may do for other users' processes, so run it with sudo. A non-zero exit code is the answer, not an error, so print it rather than letting it stop your shell:

Bash
sudo ~/reboot-check/check-reboot.sh || echo "exit code $?"

Check it worked

Right after an upgrade that touched libc6 and a kernel, the output looks something like this, followed by exit code 2 (versions and PIDs are examples):

Text
REBOOT: flagged by libc6 linux-image-7.0.0-15-generic
KERNEL: running 7.0.0-14-generic, newest installed is 7.0.0-15-generic
SERVICES: none flagged by needrestart
DELETED LIBRARIES IN USE: PIDs 812 1940

On a server with nothing pending, every line says not flagged, newest or none, and no exit code is printed. To run it on a schedule, use the timer pattern in /tutorials/replace-cron-jobs-with-systemd-timers and alert on exit code 2.

Step 7 · Decide whether Livepatch can postpone the kernel reboot

Canonical Livepatch applies fixes for high and critical kernel vulnerabilities to the running kernel, so some kernel updates no longer need an immediate reboot. It comes with Ubuntu Pro, which is free for personal use on up to five machines; larger fleets need a paid subscription. Attach the server without putting a token on the command line: run pro attach with no arguments, and it prints a code to enter at https://ubuntu.com/pro/attach.

Bash
sudo pro attach
sudo pro enable livepatch
canonical-livepatch status --verbose
pro status
pro system reboot-required

Know what it does not cover before you rely on it:

  • It patches the kernel only. Every library and service that needrestart and the lsof list flag still needs a restart.
  • It only patches kernels Canonical builds and supports, for a limited window per kernel version. After that window the server needs a normal kernel upgrade and reboot to keep receiving livepatches.
  • Some kernel code cannot be patched safely while it runs, so some fixes still arrive only as a new kernel package and a reboot.

Livepatch also changes what the reboot flag means. After apt installs a kernel, the Livepatch client checks whether the patches already applied cover the fixes in that new kernel. If they do, and no other package asked for a reboot, it deletes /var/run/reboot-required, so the first line of your script says not flagged. The newer kernel still sits in /boot, so the kernel line keeps reporting it. pro system reboot-required gives the combined answer in one word: no, yes, or yes-kernel-livepatches-applied, which means only kernel packages want a reboot and Livepatch has already patched the running kernel. Treat that last answer as "reboot at the next planned window", not "never".

Check it worked

canonical-livepatch status --verbose shows the running kernel and whether patches are applied, pro status lists livepatch as enabled, and pro system reboot-required prints one of no, yes or yes-kernel-livepatches-applied. If you chose not to use Livepatch, you have decided that every kernel update means a reboot, and the next step is your plan for it.

Step 8 · Plan the reboot window

A reboot that has to happen should happen at a time you chose. shutdown -r schedules one and warns logged-in users. Pick a time by your users' clock, not the server's: a server in a European region set to UTC serving customers in Lagos (UTC+1) or Nairobi (UTC+3) has a quiet hour at a different UTC time from one serving London.

Schedule a reboot for 03:30 by the server's clock, with a message for anyone logged in:

Bash
sudo shutdown -r 03:30 "Rebooting for kernel and library updates"
shutdown --show

Five minutes before the time, new logins are refused. To call it off, run:

Bash
sudo shutdown -c

If restarts alone are enough (exit code 1 from the script), restart the services by name instead, one at a time, and check each one:

Bash
sudo systemctl restart <service>
systemctl status <service>

For servers that should patch and reboot without you, unattended-upgrades can reboot by itself at a fixed time when the reboot flag is set. /tutorials/automatic-security-updates-ubuntu shows the Automatic-Reboot settings, so they are not repeated here.

After the reboot, log in and run the script again:

Bash
sudo ~/reboot-check/check-reboot.sh || echo "exit code $?"

If you only wanted to try the script, remove it with rm -rf ~/reboot-check.

Check it worked

shutdown --show prints the pending reboot and its time; after sudo shutdown -c it reports that no shutdown is scheduled. After the reboot, the script prints REBOOT: not flagged and KERNEL: running the newest installed kernel, with no exit code.

Next steps

  • Let the server install security fixes and reboot at a quiet hour on its own with /tutorials/automatic-security-updates-ubuntu.
  • Stop an update you are not ready for from being installed with /tutorials/control-package-versions-with-apt-hold-and-pin.
  • Run the check script every day with /tutorials/replace-cron-jobs-with-systemd-timers.
  • Find out what a restarted service logged with /tutorials/search-the-systemd-journal-with-journalctl.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.