Hosts that do not get security patches accumulate CVEs at a rate of roughly one critical kernel bug per quarter. Hosts that apply every patch the moment it lands risk breaking at 03:00 because a libc upgrade changed behaviour. The right answer is in between: apply security patches automatically, defer non-security updates to a maintenance window, and reboot when the kernel changes — at a scheduled time, not at apt’s whim. This article documents the configuration we ship on every Debian and Ubuntu host.
How to verify
Check the current posture.
dpkg -l unattended-upgrades apt-listchanges needrestart 2>/dev/null | tail -5
systemctl status unattended-upgrades --no-pager
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -30
grep -E '^[^/]' /etc/apt/apt.conf.d/50unattended-upgrades | head -40
cat /etc/apt/apt.conf.d/20auto-upgrades 2>/dev/null
ls /var/log/unattended-upgrades/
/etc/apt/apt.conf.d/20auto-upgrades is the small file that enables the periodic timer; without it, unattended-upgrades is installed but does nothing.
What’s happening
unattended-upgrades is a package that runs daily (via a systemd timer or the cron-apt legacy path) and applies packages from a configurable list of “origins.” The defaults on Ubuntu are conservative: only the ${distro_id}:${distro_codename}-security and the equivalent ESM-security pocket. On Debian, the default origin set is origin=Debian,codename=${distro_codename},label=Debian-Security. Either way, the daily run is a apt-get upgrade scoped to those origins only.
Two daemons cooperate:
apt-daily.service(and its timer) — refreshes the package list. Runs once or twice a day.apt-daily-upgrade.service(and its timer) — runsunattended-upgradeto apply the eligible packages.
The pair fires on a randomized schedule (typically morning local time) to spread load on package mirrors. The result is asynchronous: a fresh CVE in OpenSSL is patched within ~24 hours on every host that has the timer enabled.
Reboot is the wrinkle. A glibc or kernel upgrade lands on disk but does not take effect until the running processes (or the kernel itself) are restarted. needrestart is the companion tool that detects this and either prompts, restarts the affected services automatically, or signals you. We pair it with a scheduled reboot window so kernel updates do not silently sit there until manual intervention.
The procedure
-
Install the trio. On Debian, none of these are installed by default.
sudo apt install -y unattended-upgrades apt-listchanges needrestartOn Ubuntu,
unattended-upgradesis usually present;needrestartmay not be (Ubuntu 22.04+ ships it). -
Enable the daily timer. A small file with two lines turns the whole machinery on.
sudo tee /etc/apt/apt.conf.d/20auto-upgrades > /dev/null <<'EOF' APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "1"; APT::Periodic::AutocleanInterval "7"; APT::Periodic::Download-Upgradeable-Packages "1"; EOFThe
"1"is “run daily.”AutocleanInterval "7"cleans the apt cache weekly to keep/varfrom filling up. -
Review the allowed origins. This is the part where “auto-apply” can go wrong if you accept origins too broadly.
sudo nano /etc/apt/apt.conf.d/50unattended-upgradesThe default block for Ubuntu 24.04 looks like:
Unattended-Upgrade::Allowed-Origins { "${distro_id}:${distro_codename}-security"; "${distro_id}ESMApps:${distro_codename}-apps-security"; "${distro_id}ESM:${distro_codename}-infra-security"; };For Debian 12:
Unattended-Upgrade::Origins-Pattern { "origin=Debian,codename=${distro_codename},label=Debian-Security"; "origin=Debian,codename=${distro_codename}-security,label=Debian-Security"; };The conservative posture: only
-securitypockets. We do not auto-apply-updates(general updates) — those land in our weekly maintenance window via configuration management. -
Configure email notifications. Useful for fleets without centralized log analysis.
Unattended-Upgrade::Mail "ops@example.net"; Unattended-Upgrade::MailReport "on-change";Requires a working local mailer (
postfix,nullmailer, or relay configured). For fleets with central log forwarding, this is redundant — the upgrade-summary lines fromjournalctl -u unattended-upgradesend up in Loki/ELK anyway. -
Decide the auto-reboot policy. The dangerous part. Defaults:
Unattended-Upgrade::Automatic-Reboot "true"; Unattended-Upgrade::Automatic-Reboot-WithUsers "false"; Unattended-Upgrade::Automatic-Reboot-Time "03:00";Automatic-Reboot "true": reboot if a package upgrade requires it (kernel, etc.).WithUsers "false": do not reboot if there are active logged-in users (the on-call should not be kicked off).Reboot-Time "03:00": reboot at 03:00 local time.
For a fleet where reboots are scheduled by configuration management (not by the host itself), set
Automatic-Reboot "false"and let your weekly maintenance run handle it. We do this for production; we let dev hosts auto-reboot. -
Configure needrestart. It can run after every apt operation and restart affected services without rebooting.
sudo nano /etc/needrestart/needrestart.confSet:
$nrconf{restart} = 'a'; # auto-restart services without prompting $nrconf{kernelhints} = -1; # do not prompt about a stale kernel (we handle that in our reboot window)'a'means “auto-restart all” (which is mostly safe; daemons restart in the background). For interactive hosts,'i'is the default (prompt). -
Dry-run to see what would be applied. Always run this after a config change.
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -40Output should list the packages that match your origins. If empty, nothing security-eligible is pending — that is the steady state on a freshly-patched host.
-
Verify the logs after the next run.
sudo journalctl -u unattended-upgrades --since today --no-pager ls /var/log/unattended-upgrades/ sudo cat /var/log/unattended-upgrades/unattended-upgrades.log | tail -30Successful runs end with
All upgrades installed. Errors includedpkg --configure -afailures (a half-applied state from before) and held packages. -
Hold packages that should never auto-upgrade. Postgres or MySQL versions, for instance, where a minor version bump on production at 03:00 is unwise.
sudo apt-mark hold postgresql-14 apt-mark showholdHeld packages are skipped by unattended-upgrades.
-
For fleets, pin the config in configuration management.
- name: Configure unattended-upgrades ansible.builtin.template: src: 50unattended-upgrades.j2 dest: /etc/apt/apt.conf.d/50unattended-upgrades owner: root group: root mode: '0644' - name: Enable periodic timer ansible.builtin.copy: content: | APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "1"; dest: /etc/apt/apt.conf.d/20auto-upgrades
Common pitfalls
unattended-upgradesinstalled but20auto-upgradesmissing — the package is present but the timer never runs. Nothing patches.- Allowing
-updatesand-backportsorigins thinking “more updates is more security” — those origins include functional updates, not just security. Auto-applying them is what causes 03:00 outages. Automatic-Reboot "true"on a host with no maintenance window — at some point the reboot lands during the day and someone is surprised.needrestartset to'l'(list, do not act) on production — the alerts pile up because nothing restarts the affected daemons.- Cron-apt and unattended-upgrades both installed — they fight, you get random failures from apt-lock contention.
In our managed engagements, security patches apply within 24 hours on every host, kernel reboots happen in the documented weekly window, and the upgrade log is forwarded centrally so missed runs are visible. That patching cadence is part of the Managed Operations operating contract — and it is what makes a “did we patch CVE-XXXX-YYYY?” question answerable in seconds.