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:
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:
$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:
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:
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
Installing the patch is half of patching. needrestart tells you how much of the other half is left.
Security
More like this
Security
Who Did That? Tracing sudo and Logins on a Linux Server
The config changed overnight and nobody did it. Everyone says so. The logs disagree. How to read sudo logs and login records, record whole sudo sessions, watch key files with auditd, and why the logs shouldn't only live on the box.
Security
What fail2ban Actually Stops (and the Things It Was Never Going To)
fail2ban is the bouncer who throws out anyone who gets their name wrong three times. Useful, and not a security strategy. How to set up its SSH jail, check who it has banned, and know what it can't protect you from.
Security
How to Read a CVE Without Panicking: CVSS, KEV and EPSS Explained
A 9.8 CVE drops on a Friday and your group chat catches fire. Before you cancel the weekend, learn how to read a CVE: what the score measures, what it doesn't, and the two lists that tell you whether anyone is actually attacking it.