When software packages and shared libraries are upgraded on an Ubuntu Server through apt upgrade, package managers replace executable binaries and shared object files on the disk filesystem. However, system daemons and background services currently running in active system memory (RAM) continue executing the old code loaded into their process space. If a security vulnerability in a critical dependency such as OpenSSL, glibc, or libsystemd is patched, the system remains vulnerable until those active daemons are restarted.
Rebooting an entire production server to refresh updated libraries causes unnecessary service disruption. The needrestart utility provides an efficient alternative by scanning active system processes, comparing mapped shared libraries in memory with filesystem versions, and restarting affected daemons cleanly without touching unrelated processes.
Installing needrestart on Ubuntu Server
The needrestart package is included in the default Ubuntu package repositories and is installed automatically in many standard server installations:
- Update the local APT index and install the package:
sudo apt update && sudo apt install -y needrestart
- Verify the installed version and command availability:
needrestart -v
Running an Interactive Process and Service Audit
To inspect your server for stale processes running outdated libraries after a package upgrade, run needrestart with administrative privileges:
- Execute the audit tool from the terminal:
sudo needrestart
The utility inspects three primary layers:
- Kernel Status: Checks whether a newly installed Linux kernel image requires a system reboot.
- Microcode Status: Verifies CPU microcode firmware updates.
- Process Inspection: Scans open file descriptors in
/procto identify running processes holding file handles to deleted or replaced library files (indicated by(deleted)links in/proc/$PID/maps).
If outdated daemons are detected, an interactive terminal UI presents a checklist of affected systemd units (such as Nginx, Apache, or Postfix), allowing you to select and restart them immediately.
Running needrestart in Non-Interactive and Batch Mode
When running automated deployment scripts or remote administration via SSH, you can execute needrestart in non-interactive batch mode to generate machine-readable output or automated lists:
- To output process audit results in batch mode without interactive prompts:
sudo needrestart -b
- To print only the names of systemd services that require a restart:
sudo needrestart -b | grep '^NEEDRESTART-SVC:'
- To inspect only specific process categories, such as kernel status or services:
sudo needrestart -k # Audit kernel only
sudo needrestart -m # Audit CPU microcode only
Configuring Automatic Daemon Restarts in needrestart.conf
By default, when apt operations finish, needrestart triggers an interactive prompt asking which services should be restarted. In headless environments or CI/CD pipelines, this can cause automated update jobs to hang waiting for input.
You can configure needrestart behaviour by editing its master configuration file:
- Open the configuration file in your terminal editor:
sudo nano /etc/needrestart/needrestart.conf
- Locate the
$nrconf{restart}directive and set your desired operational mode:
# Available options:
# 'i' => interactive prompts (default)
# 'a' => automatically restart services without asking
# 'l' => list services only (do not restart anything)
$nrconf{restart} = 'a';
- To exempt critical databases or stateful applications (such as PostgreSQL, MySQL, or Docker) from automatic restarts, add their service patterns to the blacklist regex array in the configuration:
# Prevent needrestart from automatically terminating database engines
push(@{$nrconf{override_rc}},
qr(^postgresql@),
qr(^mysql.service$),
qr(^redis-server.service$)
);
- Save the file and exit the editor. Subsequent package upgrades will now restart safe background daemons automatically while leaving stateful database engines running undisturbed.
Verifying System Health After Service Restarts
After needrestart has executed its restart routines, confirm that all modified services are functioning normally:
- Verify that no further services require attention:
sudo needrestart -b
If all libraries are synchronised, the output will return status code 0 with no pending service notices.
- Check the operational status of restarted services using
systemctl:
sudo systemctl status nginx
sudo systemctl --failed
Using needrestart regularly as part of your system maintenance routine ensures that patched vulnerabilities in shared libraries take effect immediately across all running services without necessitating server reboots.