Patched Isn't Running: Use needrestart to Close the Gap After Updates

Installing a fixed library doesn't fix the processes that already loaded the old one. needrestart finds them, and the reboot-required file tells you when only a reboot will do.

Silas Voss

4 min read

Every security bulletin ends the same way: update the package. Most people stop there, and on Linux that leaves a gap. When apt installs a fixed OpenSSL, the new library goes onto disk. Your web server, your mail daemon and your SSH server started last week, and they still have the old library loaded in memory. The fix is installed, but the running services aren't using it yet.

needrestart is the tool that closes that gap, and it's very likely already on your server.

What needrestart checks

After a package upgrade, needrestart looks at every running process and asks three questions:

  • Is it running a binary or library that has since been replaced on disk? If so, the service needs a restart to pick up the new code.
  • Is the running kernel older than the newest one installed? If so, only a reboot will help.
  • Is the CPU microcode out of date? Same answer: reboot.

On Debian and Ubuntu it hooks into apt, so it runs on its own after apt upgrade. If you've ever seen the purple "Which services should be restarted?" screen, that was needrestart.

Run it yourself

You don't have to wait for apt. To list what needs attention without changing anything:

Bash
sudo needrestart -r l

-r l means "restart mode: list only". You'll see a short report like this:

Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.

Or, after a library update, you'll see something like this:

Services to be restarted:
 systemctl restart nginx.service
 systemctl restart postfix@-.service

Those lines are the commands to run. Restart one service at a time and check each one comes back before you move on to the next.

Decide how automatic it should be

The behaviour lives in /etc/needrestart/needrestart.conf. The setting that matters is:

PERL
$nrconf{restart} = 'i';
  • 'i' asks you interactively. That's fine on a box you're sitting at, but useless for unattended upgrades.
  • 'l' only lists. Pair it with monitoring, so somebody actually reads the list.
  • 'a' restarts services automatically. This closes the gap fastest. It's also the setting that restarts your database at 3 a.m. without asking.

Recent Ubuntu releases restart most services on their own when apt runs without a terminal. Check what your release does rather than assuming. Don't edit the main file; put your choice in a file under /etc/needrestart/conf.d/ so package upgrades don't overwrite it.

A sensible middle ground for small fleets: automatic for stateless services (web servers, workers), and listed for databases and anything that drops client connections when it restarts.

When only a reboot will do

A kernel fix doesn't help until you reboot into it. Ubuntu and Debian leave a flag when a reboot is due:

Bash
cat /var/run/reboot-required
cat /var/run/reboot-required.pkgs

The first file exists only when a reboot is needed. The second lists the packages that asked for one. Put a check on that first file in your monitoring. A server that has needed a reboot for 40 days is a server running 40-day-old kernel bugs.

Compare what you're running with what's installed:

Bash
uname -r
ls /boot/vmlinuz-*

If the newest file in /boot is newer than uname -r, the patch is waiting on the reboot.

needrestart needs patching too

needrestart runs as root and inspects other processes, which makes it a target itself. In November 2024, Qualys disclosed local privilege-escalation flaws in it that had been there for about a decade. Distributions shipped fixed versions quickly, and the fix came the usual way: an update. Keep it in your normal patch cycle.

What to do today

  1. Run sudo needrestart -r l on each server and restart what it lists, one service at a time.
  2. Check /var/run/reboot-required and plan reboots for any server that has it.
  3. Choose a restart mode on purpose and put it in a file under conf.d.
  4. Add the reboot-required check to monitoring so this stops depending on memory.

Installing the patch is half of patching. needrestart tells you how much of the other half is left.