Automatic security updates on Debian and Ubuntu with unattended-upgrades
How I set up unattended-upgrades on Debian and Ubuntu servers: security-only origins, a controlled reboot window, a dry run, and logs worth checking.
Contents
Patching is a chore, so let the server do it
Most compromised servers I have read about were not hit by clever zero-days. They were running a package with a fix that had been available for weeks. Patching by hand works when you have one machine and a good memory. Once you have a handful of VPSs, a Raspberry Pi, and a staging box, it is the first thing that slips.
On Debian and Ubuntu the answer is unattended-upgrades. It installs updates from origins you choose, on a schedule, and can reboot in a window you control. This is how I configure it on servers I look after.
Install and enable
Ubuntu usually ships it already. On a minimal Debian install you may need to add it:
sudo apt update
sudo apt install unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades
The dpkg-reconfigure step writes /etc/apt/apt.conf.d/20auto-upgrades. Check that it contains these two lines:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
The value is an interval in days. 1 means refresh the package lists and run the upgrade once a day.
Choose what gets installed
The main configuration lives in /etc/apt/apt.conf.d/50unattended-upgrades. Rather than editing the packaged file, I put my overrides in a separate file such as /etc/apt/apt.conf.d/52unattended-upgrades-local. Files are read in lexical order, so later files win, and package upgrades will not ask me to merge changes into a file I touched.
The most important setting is which origins are allowed. On Ubuntu the default already limits automatic installs to the security pocket:
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";
};
On Debian the default uses Origins-Pattern and looks like this:
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";
};
I leave -updates and third-party repositories out on production servers. Security fixes are backported carefully and rarely change behaviour; feature updates and vendor repositories are better rolled out deliberately, after testing on staging.
If a package must never be upgraded automatically, for example a database engine whose upgrade needs a maintenance window, block it explicitly:
Unattended-Upgrade::Package-Blacklist {
"postgresql-";
};
Entries are regular expressions, so postgresql- matches every versioned PostgreSQL package.
Clean up and reboot on your terms
Kernel and libc updates only take full effect after a reboot. When one is pending, Debian and Ubuntu create /var/run/reboot-required, and /var/run/reboot-required.pkgs lists the packages that asked for it. You can let unattended-upgrades handle the reboot:
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";
Removing old kernels keeps /boot from filling up, which is a classic way for a later update to fail. Automatic-Reboot-WithUsers "false" skips the reboot while someone is logged in. Whether automatic reboots are acceptable depends on the service: for a single-instance app I prefer a manual reboot plus an alert; for nodes behind a load balancer an automatic one is fine.
For processes that use an updated library but do not need a full reboot, needrestart can detect and restart the affected services after an upgrade.
Control when it runs
The schedule comes from two systemd timers: apt-daily.timer downloads, and apt-daily-upgrade.timer installs. See when they will fire next with:
systemctl list-timers 'apt-daily*'
To move the install run into a quiet window, override the timer rather than editing the unit shipped by the package:
sudo systemctl edit apt-daily-upgrade.timer
[Timer]
OnCalendar=
OnCalendar=*-*-* 03:00
RandomizedDelaySec=30m
The empty OnCalendar= clears the default before setting the new one. The random delay stops a fleet of servers from all hitting the mirror and restarting at the same second.
Test before you trust it
A dry run shows which origins match and what would be installed, without changing anything:
sudo unattended-upgrade --dry-run --debug
If the output says no packages are allowed, the origin patterns are usually the cause. Compare them with the origin and label fields printed by apt-cache policy.
Know what happened
Every run is logged in /var/log/unattended-upgrades/, and the low-level package changes are in /var/log/dpkg.log. A quick check after a run:
sudo tail -n 50 /var/log/unattended-upgrades/unattended-upgrades.log
grep " upgrade " /var/log/dpkg.log | tail
If the server can send mail, set Unattended-Upgrade::Mail to an address you read. Recent versions also support Unattended-Upgrade::MailReport "on-change"; so you only hear about runs that changed something or failed. Without mail, a monitoring check that alerts when /var/run/reboot-required has existed for more than a few days covers the most common gap.
Summary
Automatic security updates are not a replacement for a patching policy, but they close the window between a fix being published and a fix being installed. Allow only security origins, block packages that need a maintenance window, pick a reboot window, run the dry run once, and look at the log now and then. That is a small amount of setup for a big reduction in risk.