๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

Linux Updates Without Reboots: needrestart and dnf needs-restarting

Linux Updates Without Reboots: needrestart and dnf needs-restarting

You run apt upgrade on a web server. It installs a new libssl3 and a new libc6, prints no errors, and exits 0. Meanwhile nginx, sshd and cron are still running with the old libraries mapped in memory, so the CVE you just patched is still exploitable until each of those processes starts again. The package manager's job ended when the files landed on disk, not when the fix became active.

That gap is what needrestart on Debian/Ubuntu and dnf needs-restarting on the RHEL family exist to close. Comparing needrestart vs dnf needs-restarting is mostly a question of how each tool finds stale processes, what it tells you to do, and how safely you can automate it. First, though, you need the mechanism, because it explains both tools and tells you when a service restart is not enough and you need a reboot.

Why a finished upgrade can still leave you vulnerable

What happens to a library file when a package replaces it

dpkg and rpm do not overwrite a library in place. They write the new file under a temporary name, rename it over the old path, and the old inode loses its last directory entry. Any process that already mapped the old file keeps a valid reference to it, so the kernel keeps the data alive until the last mapping goes away. The process never sees the new code.

You can see this directly in the process's memory map:

$ grep libssl /proc/$(pidof -s nginx)/maps
7f3c2a1e4000-7f3c2a25c000 r--p 00000000 fd:01 1311042  /usr/lib/x86_64-linux-gnu/libssl.so.3 (deleted)

The (deleted) suffix is the whole signal. The path on disk now points to a different inode, and this process is still running the one that was unlinked.

Which updates need a service restart and which need a reboot

Updates fall into three classes, and the fix differs for each.

  • Ordinary libraries such as libssl or libc: restart every process that maps them. For libssl that is nginx, sshd, postfix and the rest. For glibc it is effectively everything.
  • Long-lived system daemons such as systemd and dbus: systemctl daemon-reexec replaces systemd's own process image. dbus-daemon cannot be restarted safely on a running system, so a reboot is the practical answer.
  • Kernel, CPU microcode and firmware: nothing restarts these. You reboot, or you apply a kernel live patch for the specific fixes it covers.

Finding stale processes by hand with /proc

Spotting deleted mappings in /proc/PID/maps

The quickest check lists every process that has any deleted mapping. Run it as root, otherwise you only see your own processes:

grep -l '(deleted)' /proc/[0-9]*/maps 2>/dev/null

This is noisy. The format is documented in proc(5), and the path is the sixth field followed by the (deleted) marker. Temporary files, /dev/shm segments and memfd: mappings all match too, so filter by path.

A one-liner that lists affected services

This loop keeps only deleted files under system directories and prints PID, command name, owning unit and the first few paths:

for pid in $(grep -l '(deleted)' /proc/[0-9]*/maps 2>/dev/null | cut -d/ -f3); do
  files=$(awk '$7=="(deleted)" && $6 ~ /^\/(usr|lib|bin|sbin|opt)\// {print $6}' /proc/$pid/maps | sort -u | head -3 | paste -sd,)
  [ -n "$files" ] && printf '%s\t%s\t%s\t%s\n' "$pid" "$(cat /proc/$pid/comm)" "$(ps -o unit= -p $pid)" "$files"
done

ps -o unit= reads the cgroup, so it returns nginx.service or session-3.scope. systemctl status PID gives the same answer with more context. An lsof alternative is lsof +c 15 | grep -E 'DEL|deleted', which is slower on busy hosts.

Expect false positives even with the filter. Databases hold deleted data or WAL files, and package scripts sometimes unlink files a process legitimately still uses. Telling those apart from a stale library, and mapping the result to the right restart action, is what the dedicated tools do for you.

needrestart on Debian and Ubuntu

Install it with apt install needrestart. The package adds an apt hook, /etc/apt/apt.conf.d/99needrestart (check the exact name on your release), that runs it after every dpkg transaction. Recent Ubuntu LTS server images ship it already, which is why you see a "Daemons using outdated libraries" dialog after apt upgrade.

Interactive, list and automatic modes

The -r option selects the restart behaviour:

  • -r i: interactive, a checklist of services to restart.
  • -r l: list only, restart nothing.
  • -r a: restart everything affected automatically.

Other useful flags are -k (kernel check only), -l (libraries only), -b (batch output), -p (Nagios plugin output) and -v (verbose). Defaults live in /etc/needrestart/needrestart.conf. Put overrides in /etc/needrestart/conf.d/*.conf, which is Perl syntax:

$nrconf{restart} = 'l';

The same file has $nrconf{kernelhints}, which controls the kernel hint messages. The comments in the shipped file describe the accepted values for your version.

Batch mode output for scripts and monitoring

$ sudo needrestart -b
NEEDRESTART-VER: 3.6
NEEDRESTART-KCUR: 5.15.0-91-generic
NEEDRESTART-KEXP: 5.15.0-94-generic
NEEDRESTART-KSTA: 3
NEEDRESTART-SVC: cron.service
NEEDRESTART-SVC: nginx.service
NEEDRESTART-SVC: ssh.service

KCUR is the running kernel and KEXP the newest installed one. KSTA summarises the gap: 1 means up to date, 2 an ABI-compatible upgrade is pending, 3 a version upgrade is pending, and 0 means it could not tell. Each SVC line is a unit to restart. Newer versions also report microcode status. Parse these lines instead of scraping the interactive text. The -p mode emits one Nagios-style line and the matching exit code.

The reboot-required flag file and how it differs

On Ubuntu, /var/run/reboot-required and /var/run/reboot-required.pkgs come from update-notifier-common. Package maintainer scripts create them. On plain Debian the package is not installed by default, so the file usually never appears. The flag only covers packages that declare a reboot requirement and says nothing about which services hold old libraries. A host can have no flag file and still run a stale libssl in half its daemons. The needrestart project checks the processes themselves, which is why the two signals complement each other. The Dargslan book on Linux patch management covers building a full update policy around them.

dnf needs-restarting on RHEL, AlmaLinux and Fedora

On RHEL 8/9 and derivatives the command comes from dnf-plugins-core, which the dnf-utils package pulls in. On Fedora with DNF5 it ships in dnf5-plugins. Option names and output are close but not identical across versions, so check dnf needs-restarting --help on your release. RHEL 7 has a standalone needs-restarting binary from yum-utils instead.

Listing processes and services that use outdated files

Without options it prints one line per stale process. Run it as root, or you only see your own processes:

$ sudo dnf needs-restarting
1021 : /usr/sbin/sshd -D
1187 : /usr/sbin/crond -n
2210 : /usr/sbin/nginx -g daemon off;

$ sudo dnf needs-restarting -s
crond.service
nginx.service
sshd.service

The -s output is what you feed to systemctl restart. It compares each process's start time against the update time of the packages it uses, and it is not a scan for (deleted) mappings. Read the list before restarting, because it can include units you do not want to bounce blindly.

Using -r for the reboot decision and its exit code

$ sudo dnf needs-restarting -r
Core libraries or services have been updated since boot-up:
  * kernel
  * glibc

Reboot is required to fully utilize these updates.
More information: https://access.redhat.com/solutions/27943

$ echo $?
1

When nothing qualifies, it prints "No core libraries or services have been updated since boot-up. Reboot should not be necessary." and exits 0. That makes it script-friendly:

dnf needs-restarting -r || echo "reboot needed"

Note what this is: a hint based on a fixed package list (kernel, glibc, systemd, dbus, linux-firmware and a few others), checked against boot time. It does not inspect running processes. You can extend the list with files in the plugin's configuration directory under /etc/dnf/plugins/; the dnf-plugins-core documentation has the exact format for your version. The YUM/DNF package history cheat sheet helps when you need to see which transaction touched a flagged package.

Comparing the two tools side by side

Aspectneedrestartdnf needs-restarting
Detection methodDeleted mapped files, plus interpreter and script checksProcess start time vs package update time
Kernel checkRunning vs installed kernel, including ABIPackage list in -r only
Microcode checkNewer versions onlyVia package list (linux-firmware, microcode_ctl)
Automatic restartYes, -r aNo, report only
Script-friendly output-b batch, -p Nagios-s unit names
Exit codeNagios codes with -p-r: 1 means reboot
Config location/etc/needrestart//etc/dnf/plugins/
AvailabilityDebian, UbuntuRHEL family, Fedora

needrestart is the more capable tool. It acts, and it sees stale Python or Perl daemons that a pure package comparison misses. On the RHEL family, pair needs-restarting -s with a small wrapper that restarts a reviewed list of units, with databases left for a maintenance window.

Keeping databases and SSH from restarting on their own

Excluding units with override_rc

Create /etc/needrestart/conf.d/50-local.conf. Each key is a regex matched against the service name, and a value of 0 means never restart it:

$nrconf{override_rc}{qr(^postgresql)} = 0;
$nrconf{override_rc}{qr(^mariadb)} = 0;
$nrconf{override_rc}{qr(^mysql)} = 0;
$nrconf{override_rc}{qr(^docker)} = 0;
$nrconf{override_rc}{qr(^containerd)} = 0;

Restarting sshd does not drop existing sessions, so it is usually safe. Run sshd -t first, though, since a bad config would lock out new logins. Excluded units still show up in the report, and you restart them yourself.

Setting a safe default for unattended upgrades

Under non-interactive apt, needrestart can fall back to automatic restarts depending on version, so set the mode explicitly:

$nrconf{restart} = 'l';

In /etc/apt/apt.conf.d/50unattended-upgrades, keep reboots under your control, or schedule them:

Unattended-Upgrade::Automatic-Reboot "false";
// or, if you want scheduled reboots:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";

Warning: needrestart -r a on a primary database restarts it mid-day with no coordination. Expect dropped connections, a failover or an outage. Exclude database units, and restart replicas first.

When only a reboot will do: kernel, systemd, glibc and microcode

Comparing uname -r with the installed kernel

No service restart loads a new kernel. Compare the running version with the newest installed package:

# RHEL family
$ uname -r
5.14.0-362.8.1.el9_3.x86_64
$ rpm -q kernel --last | head -1
kernel-5.14.0-427.13.1.el9_4.x86_64   Tue 07 May 2024 10:12:33 AM UTC

# Debian/Ubuntu
$ dpkg -l 'linux-image-*' | grep ^ii
$ sudo needrestart -k
Pending kernel upgrade!
Running kernel version: 5.15.0-91-generic
Expected kernel version: 5.15.0-94-generic

If the versions differ, you are running the old kernel. rpm --last sorts by install time, so after a rollback or a manual install the first line may not be the newest version. needrestart -k does the comparison for you. Microcode and firmware are the same story: intel-microcode, amd64-microcode and linux-firmware only take effect at boot. Newer needrestart versions report microcode status, and needrestart --help shows which flag your release uses.

systemctl daemon-reexec and what live patching covers

After a systemd update, systemctl daemon-reexec serialises PID 1's state and re-executes it, so the new binary takes over without a reboot. A libc update affects PID 1 too, because it maps libc like any other process. Reexec gets you most of the way, but a reboot is cleaner and leaves no doubt about what is running.

Live patching (Canonical Livepatch, kpatch on RHEL, upstream kernel livepatch) swaps in replacement functions for selected kernel CVEs. It does not touch glibc, OpenSSL or any other userland code, and it does not cover every kernel fix. Vendors still ship new kernels that need a reboot eventually, so treat livepatch as a way to move the reboot into a planned window, not to cancel it.

Wiring the checks into Ansible and monitoring

An Ansible play that reports and conditionally reboots

Use one signal per OS family. Debian relies on the flag file, RHEL on the exit code of dnf needs-restarting -r, where 1 means reboot:

- hosts: all
  become: true
  serial: 1
  tasks:
    - name: Debian reboot flag
      ansible.builtin.stat:
        path: /var/run/reboot-required
      register: reboot_flag
      when: ansible_os_family == "Debian"

    - name: Stale services (Debian)
      ansible.builtin.command: needrestart -b
      register: nr_batch
      changed_when: false
      when: ansible_os_family == "Debian"

    - name: RHEL reboot check
      ansible.builtin.command: dnf needs-restarting -r
      register: nr_rhel
      changed_when: nr_rhel.rc == 1
      failed_when: nr_rhel.rc not in [0, 1]
      when: ansible_os_family == "RedHat"

    - name: Report services
      ansible.builtin.debug:
        msg: "{{ nr_batch.stdout_lines | select('match', '^NEEDRESTART-SVC:') | list }}"
      when: nr_batch is not skipped

    - name: Reboot if required
      ansible.builtin.reboot:
        reboot_timeout: 900
      when: (reboot_flag.stat.exists | default(false)) or (nr_rhel.rc | default(0)) == 1

serial: 1 keeps a fleet from rebooting all at once. Allowing only rc 0 and 1 means a missing plugin fails the run instead of looking like a clean host.

Exposing pending restarts to Prometheus or Nagios

For Nagios or NRPE, needrestart -p prints one status line and returns 0, 1 or 2. It needs root, so grant the monitoring user a sudoers rule for that exact command. For Prometheus, run a script from a systemd timer or cron and write into the node_exporter textfile directory:

#!/bin/sh
d=/var/lib/node_exporter/textfile_collector
v=0
if [ -e /var/run/reboot-required ]; then v=1
elif command -v dnf >/dev/null 2>&1; then
  dnf needs-restarting -r >/dev/null 2>&1; [ $? -eq 1 ] && v=1
fi
printf '# TYPE node_reboot_required gauge\nnode_reboot_required %s\n' "$v" > "$d/reboot.prom.$$"
mv "$d/reboot.prom.$$" "$d/reboot.prom"

The mv makes the update atomic, so the exporter never reads a half-written file. Alert when the gauge stays at 1 for longer than your patch window. The service restart monitoring cheat sheet has complementary checks.

Post-patch checklist you can paste into a runbook

  1. Record the running kernel: uname -r.
  2. Patch: apt upgrade or dnf update. Keep the transaction output.
  3. Kernel and core check: needrestart -k and ls /var/run/reboot-required on Debian/Ubuntu, dnf needs-restarting -r; echo $? on the RHEL family.
  4. Service check: needrestart -b | grep SVC or dnf needs-restarting -s.
  5. Restart the stateless services (web, cron, mail) and run sshd -t before restarting sshd.
  6. Schedule databases, container runtimes and anything with state for the maintenance window. Replicas go first.
  7. If the kernel, glibc, systemd, dbus or microcode changed, reboot one host at a time and confirm health checks before the next.
  8. Verify afterwards: uname -r matches the installed kernel, and the service list and flag file are empty.

If you want a complete patch policy with scheduling, rollback and exception handling, the Dargslan book Linux Patch Management & System Updates builds that around these checks.

Frequently Asked Questions

Do I need to reboot after every apt upgrade or dnf update?

No. Most updates only need the affected services restarted. A reboot is required when the kernel, glibc, systemd, dbus, firmware or microcode changed, or when you cannot safely restart everything that maps an updated library. Run the checks after each patch run and decide from the output instead of rebooting by habit.

Is it safe to let needrestart restart services automatically?

For stateless services such as nginx, cron or postfix, usually yes. For databases, container runtimes and clustered services, no. Set $nrconf{restart} = 'l' or exclude those units with override_rc, then restart them in a planned window. Automatic mode is a reasonable default only after you have excluded everything stateful.

Why does dnf needs-restarting -r say no reboot needed when a new kernel is installed?

The check compares boot time with the update time of a fixed package list. If the kernel was installed before the last boot, or a package was installed in a way the plugin does not count, it reports clean. Confirm with uname -r against rpm -q kernel --last | head -1, and check the plugin version.

How do I stop the needrestart prompt from appearing in apt over SSH or in CI?

Set the mode in a file under /etc/needrestart/conf.d/: $nrconf{restart} = 'l'; lists services without prompting. You can also set NEEDRESTART_MODE=l in the environment, and DEBIAN_FRONTEND=noninteractive covers the apt side. Check the comments in your shipped needrestart.conf, because defaults differ between versions.

Does kernel live patching remove the need to run needrestart?

No. Live patching applies only selected kernel fixes. It does nothing for glibc, OpenSSL or any other userland library, which is exactly what needrestart finds. You also still need a reboot eventually for kernel updates that are not live-patchable, plus firmware and microcode. Run both: livepatch for the kernel, needrestart for processes.

Share this article:
Dorian Thorne
About the Author

Dorian Thorne

Cloud Infrastructure, Cloud Architecture, Infrastructure Automation, Technical Documentation

Dorian Thorne is a cloud infrastructure specialist and technical author focused on the design, deployment, and operation of scalable cloud-based systems.

He has extensive experience working with cloud platforms and modern infrastructure practices, including virtualized environments, cloud networking, identity and acces...

Cloud Computing Cloud Networking Identity and Access Management Infrastructure as Code System Reliability

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.