Automatic security updates on Debian and Ubuntu with unattended-upgrades
unattended-upgrades installs security updates from APT every day, without anyone logging in. On a server that nobody looks at for weeks, it is the difference between a fix installed the day it was published and a fix installed whenever someone remembers. Installing the package turns it on, and a few settings are worth changing. The commands were 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 shows 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 unattended-upgrades by default. Minimal and container images often do not have it.
What it installs
The file /etc/apt/apt.conf.d/50unattended-upgrades decides which repositories count. 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. Packages from other repositories (Docker, PostgreSQL, Node.js) are not
touched at all; see below.
When it runs
Two systemd timers drive it. The times are the same on Debian 12, 13 and Ubuntu 24.04:
| 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 |
The times are in the time zone of the server.
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 worth changing
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 decides
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 packages that depend on a listed package are held back as well:
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. Updating 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 would be installed, 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
Things that stop it
- Held packages (
apt-mark hold) are never upgraded. - 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
aptrunning at the same time makes the run wait or skip. - An end-of-life release does not get updates any more. Check the dates in the end-of-life guide.
Patchbrief 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 agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.