Automatic security updates on Debian and Ubuntu with unattended-upgrades
unattended-upgrades installs security updates from APT every day, without anyone logging in. A server that nobody looks at for weeks gets each security fix within about a day of its release. Tested on Debian 12, Debian 13 and Ubuntu 24.04.
Turn it on
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
On Debian 12, Debian 13 and Ubuntu 24.04, installing the package already turns it on. The dpkg-reconfigure
step asks the question again ("Automatically download and install stable updates?") and writes the answer
to /etc/apt/apt.conf.d/20auto-upgrades:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
The two lines tell the daily APT job to refresh the package lists and to run unattended-upgrades every day (the value is the interval in days). Check the result with:
apt-config dump APT::Periodic
Ubuntu Server installs it by default; minimal and container images often do not have it.
What it installs
/etc/apt/apt.conf.d/50unattended-upgrades lists the repositories it installs from. Debian 12 and 13 allow
the security repository and the point releases of the main archive:
Unattended-Upgrade::Origins-Pattern {
"origin=Debian,codename=${distro_codename},label=Debian";
"origin=Debian,codename=${distro_codename},label=Debian-Security";
"origin=Debian,codename=${distro_codename}-security,label=Debian-Security";
};
Ubuntu 24.04 allows the security pocket and, with Ubuntu Pro, the ESM pockets:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
"${distro_id}ESMApps:${distro_codename}-apps-security";
"${distro_id}ESM:${distro_codename}-infra-security";
};
Updates from -updates (bug fixes between point releases on Debian, all non-security updates on Ubuntu) stay
manual unless you add their line. unattended-upgrades leaves packages from other repositories (Docker,
PostgreSQL, Node.js) alone; see third-party repositories.
When it runs
Two systemd timers start it. The times are the same on Debian 12, 13 and Ubuntu 24.04, in the time zone of the server:
| Timer | When | What |
|---|---|---|
apt-daily.timer |
06:00 and 18:00, plus a random delay of up to 12 hours | refreshes the package lists |
apt-daily-upgrade.timer |
06:00, plus a random delay of up to 60 minutes | runs unattended-upgrades |
To run at a fixed time, override the upgrade timer:
sudo systemctl edit apt-daily-upgrade.timer
[Timer]
OnCalendar=
OnCalendar=*-*-* 03:00
RandomizedDelaySec=15m
Settings to change
Put your changes in a file of your own, so that a package update of 50unattended-upgrades does not conflict
with them:
sudo tee /etc/apt/apt.conf.d/52unattended-upgrades-local >/dev/null <<'EOF'
// Reboot when a package asks for it (kernel, libc), at a quiet hour.
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
// Remove dependencies that are no longer needed.
Unattended-Upgrade::Remove-Unused-Dependencies "true";
EOF
Automatic-Reboot reboots only when /var/run/reboot-required exists, which happens after kernel and some
library updates. It reboots even when users are logged in, unless you set
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";. Without Automatic-Reboot, someone has to reboot by
hand; the reboot guide explains how to tell when. Old kernel packages
are removed by default (Remove-Unused-Kernel-Packages).
For a mail report, set Unattended-Upgrade::Mail "you@example.com";. Unattended-Upgrade::MailReport sets
when it mails: on-change (the default), only-on-error or always. Mail needs a working mail setup on the
server (a local MTA or a relay); without one, the logs are the record.
To keep a package out of automatic updates, list it in Unattended-Upgrade::Package-Blacklist. The entries
are regular expressions, and unattended-upgrades also holds back packages that depend on a listed package:
Unattended-Upgrade::Package-Blacklist {
"postgresql-";
};
Third-party repositories
To include another repository, add a line to Unattended-Upgrade::Origins-Pattern with the values that
apt-cache policy prints for it:
apt-cache policy | grep 'o=Docker'
The line looks like release o=Docker,a=trixie,l=Docker CE,c=stable,b=amd64. The short keys map to the
pattern keys: o is origin, a is archive (the suite), n is codename, l is label, c is
component. The Docker repository sets the suite but not a codename, so match on archive. Add it in your
local file; a second Origins-Pattern block adds to the list from 50unattended-upgrades:
Unattended-Upgrade::Origins-Pattern {
"origin=Docker,archive=${distro_codename}";
};
On Ubuntu, 50unattended-upgrades uses the older Allowed-Origins list ("origin:archive" pairs); both
lists work, and the entries of all of them count. An update of Docker Engine restarts the Docker daemon:
running containers stop, and only those with a restart policy start again, unless "live-restore": true is
set in /etc/docker/daemon.json (which covers patch releases only). Decide whether you want that at night.
Check that it works
A dry run shows what it would install, without changing anything:
sudo unattended-upgrades --dry-run --debug
The logs of the real runs:
less /var/log/unattended-upgrades/unattended-upgrades.log
grep -E ' (install|upgrade) ' /var/log/dpkg.log | tail
systemctl list-timers 'apt-daily*'
Pending updates that unattended-upgrades did not install (non-security updates, held packages, packages from other repositories):
apt list --upgradable
apt-mark showhold
What stops it
| Cause | What happens |
|---|---|
A held package (apt-mark hold) |
It is never upgraded. |
apt-daily.timer disabled or masked |
The package lists are not refreshed, so unattended-upgrades does not see new updates. sudo systemctl unmask apt-daily.timer and sudo systemctl enable --now apt-daily.timer turn it back on. |
| A configuration file you changed that the new package version also changes | dpkg would ask what to do, so unattended-upgrades holds the package back. The log names it; upgrade it by hand with apt. |
| The dpkg lock | A manual apt running at the same time makes the run wait or skip. |
| An end-of-life release | The release does not get updates any more. Check the dates in the end-of-life guide. |
Vulnpocalypse checks every day that automatic updates are active on each server and lists the security updates that are still waiting.
Sources
- Debian wiki: UnattendedUpgrades: install,
dpkg-reconfigure, local configuration file, logs - unattended-upgrades README:
Origins-Pattern,Automatic-Reboot,Package-Blacklist, mail options - Ubuntu Server: automatic updates
- systemd.timer(5):
OnCalendar=,RandomizedDelaySec= - Docker: live restore: containers keep running while the daemon restarts, for patch releases
Check your servers every day
The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse