Skip to content

Unattended-upgrades plus needrestart — patching without 3 AM reboots

Wiring unattended-upgrades for security patches, then using needrestart to detect when a daemon restart (or a reboot) is actually required.

unattended-upgrades patches Debian and Ubuntu hosts on a schedule without operator input. needrestart answers the question that follows every patch: which daemons are still using the old shared library, and do you need to reboot to pick up a kernel update? Together they replace the manual patch tuesday with a system that handles 95 % of the work and surfaces only the cases that need a human.

How to verify

Check that both are installed and that the most recent run worked:

sudo dpkg -l unattended-upgrades needrestart
sudo systemctl list-timers | grep apt
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -20
sudo cat /var/log/unattended-upgrades/unattended-upgrades.log | tail -30
sudo needrestart -r l    # report processes needing a restart
[ -f /var/run/reboot-required ] && cat /var/run/reboot-required.pkgs

The dry-run flag shows what would be installed without doing it. The reboot-required file is the kernel’s promise that a reboot is necessary; needrestart goes further and reports per-service.

What’s happening

unattended-upgrades is a Python script that consumes apt policy and a configuration file (/etc/apt/apt.conf.d/50unattended-upgrades) describing which package origins are eligible for automatic upgrade. By default only the security origin is enabled — ${distro_id}:${distro_codename}-security on Ubuntu, Debian-Security on Debian. A daily systemd timer (apt-daily.timer plus apt-daily-upgrade.timer) runs the script.

needrestart runs after each apt operation (via a dpkg hook). It walks /proc/*/maps, finds processes that mapped files which were replaced on disk by the upgrade, and reports them. Optional: it can restart matching services automatically — useful for daemons that handle reload-without-restart cleanly, dangerous for those that do not.

The catch: by default, needrestart prompts interactively. On a host running unattended-upgrades in the background, the interactive prompt blocks the upgrade indefinitely. The two are configured to coexist, but the defaults sometimes don’t.

The procedure

  1. Install both. sudo apt install unattended-upgrades needrestart. The unattended-upgrades package may prompt to enable itself — choose yes; otherwise enable with sudo dpkg-reconfigure --priority=low unattended-upgrades.
  2. Configure origins. Edit /etc/apt/apt.conf.d/50unattended-upgrades. Uncomment the lines for ${distro_id}:${distro_codename}-updates if you want point releases too; leave *backports commented unless you have a specific need. Set Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; so old kernel debris doesn’t fill /boot.
  3. Set the auto-reboot policy. In 50unattended-upgrades: Unattended-Upgrade::Automatic-Reboot "true"; and Unattended-Upgrade::Automatic-Reboot-Time "03:00";. The reboot only fires when /var/run/reboot-required is present — typically after a kernel or systemd upgrade.
  4. Configure needrestart to be non-interactive. Edit /etc/needrestart/needrestart.conf and set $nrconf{restart} = 'a'; (auto-restart services) or 'l' (list only, don’t restart). For systems where you want to control restarts manually, use 'l' and let your monitoring surface the report.
  5. Tame the kernel hint. $nrconf{kernelhints} = -1; in needrestart.conf suppresses the “kernel changed, reboot needed” interactive prompt — the unattended-upgrades reboot policy already handles it.
  6. Forward log entries to your aggregator. /var/log/unattended-upgrades/unattended-upgrades.log and /var/log/unattended-upgrades/unattended-upgrades-dpkg.log are the audit trail. Stream both via promtail/syslog and alert on Pending package or failed to install lines.
  7. Test the full path before you trust it. sudo unattended-upgrade --dry-run --debug shows what the next run would do. Then trigger a real run: sudo unattended-upgrade -d. Read the log, confirm a daemon you expect to restart actually did.
  8. Set up exclusions where they matter. Unattended-Upgrade::Package-Blacklist {"linux-image-.*"; "containerd";} keeps named packages off the auto-upgrade path — useful for the container runtime where you want a controlled rollout.

Common pitfalls

  • The apt-daily.timer runs once a day with a 12-hour randomized delay. Two hosts behind one egress IP may both try to upgrade simultaneously and exhaust an apt mirror — stagger with RandomizedDelaySec.
  • Automatic-Reboot does not check uptime or load — it reboots at the configured time regardless. On critical hosts, gate the reboot through a cluster-aware tool (drain the node, evict pods) and disable the built-in reboot.
  • needrestart’s auto-restart of dbus or systemd itself can leave a host in a broken state where logins fail. Explicitly exclude these in nrconf{blacklist}.
  • The unattended-upgrades log does not include security advisories. To map “what changed” to “why it changed”, apt changelog <package> or usn-list (Ubuntu Security Notice tool) supplements the picture.
  • A host without internet access (air-gapped) silently does nothing — the timer fires, the apt update fails, and no patches are applied. Monitor the last successful upgrade timestamp, not just the timer status.

In the engagements we run, patch cadence is reported per-host with an explicit pending-reboot column, and the reboot is choreographed through the cluster or load balancer when uptime matters. That tooling is part of Managed Operations — automatic patching is necessary but not sufficient.