Most Linux servers that get broken into aren't hit by a clever zero-day. They're running a package that was patched weeks ago, because nobody logged in to run apt upgrade. If you look after a VPS, a home server or a Raspberry Pi, automatic security updates are the cheapest protection you can add, and on Ubuntu and Debian the tool for it is already in the archive: unattended-upgrades.
This guide covers Ubuntu 24.04 and 26.04 LTS and Debian 12 (bookworm) and 13 (trixie). The package works the same way on all of them; only the default list of allowed sources differs.
What unattended-upgrades actually does
Two systemd timers drive the whole thing:
- apt-daily.timer refreshes the package lists (the same as
apt update), roughly twice a day with a random delay. - apt-daily-upgrade.timer runs
unattended-upgradeshortly after 6am with a random delay, which installs any upgrades that match your rules.
The rules live in /etc/apt/apt.conf.d/50unattended-upgrades. By default only packages from the security pocket are installed automatically. Normal bug-fix updates wait for you. That's a sensible default for servers: security fixes are small and targeted, while larger updates are better applied when you're watching.
Install and switch it on
On Ubuntu Server it's normally installed already. On Debian, and on minimal or cloud images, install it yourself:
sudo apt update sudo apt install unattended-upgrades apt-listchanges
Then turn on the periodic jobs:
sudo dpkg-reconfigure -plow unattended-upgrades
Answer Yes. This writes /etc/apt/apt.conf.d/20auto-upgrades, which should contain exactly two lines:
APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Unattended-Upgrade "1";
The "1" means "every day". If either line is missing or set to "0", nothing is installed automatically, even though the package is present. That's the most common reason people think they have auto-updates when they don't.
Check which sources are allowed
Open the main config:
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
On Ubuntu you'll see an Allowed-Origins block like this (lines starting with // are comments):
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";
// "${distro_id}:${distro_codename}-updates";
};
On Debian the same idea uses Origins-Pattern:
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";
};
The ${distro_codename} variable expands to noble, bookworm, trixie and so on, so the file keeps working after a release upgrade. Leave the security lines alone. Whether you also enable -updates is a judgement call: on a single-purpose home server I do; on anything customers depend on, I don't.
Keep your changes in a separate file
Don't edit 50unattended-upgrades more than you have to. When the package is upgraded, apt asks whether to keep your version or the maintainer's, and it's easy to pick the wrong one. APT reads every file in /etc/apt/apt.conf.d/ in name order, and a later file overrides single values from an earlier one. So put your settings in a file that sorts after it:
sudo nano /etc/apt/apt.conf.d/52unattended-upgrades-local
A good starting point for a small server:
// Remove old kernels and packages that are no longer needed Unattended-Upgrade::Remove-Unused-Kernel-Packages "true"; Unattended-Upgrade::Remove-New-Unused-Dependencies "true"; Unattended-Upgrade::Remove-Unused-Dependencies "true"; // Reboot automatically when a patch needs it, at a quiet time Unattended-Upgrade::Automatic-Reboot "true"; Unattended-Upgrade::Automatic-Reboot-Time "03:30"; Unattended-Upgrade::Automatic-Reboot-WithUsers "true"; // Email a report only when something was installed or failed Unattended-Upgrade::Mail "you@example.com"; Unattended-Upgrade::MailReport "on-change";
Lists such as Allowed-Origins are added to rather than replaced, so you can append an extra origin from this file too.
Reboots: the part people skip
Kernel, glibc and OpenSSL fixes don't fully take effect until the machine reboots or the affected services restart. When a package needs a reboot, Ubuntu and Debian create /var/run/reboot-required, and the list of packages that asked for it goes into /var/run/reboot-required.pkgs:
cat /var/run/reboot-required.pkgs 2>/dev/null || echo "no reboot needed"
You have three reasonable choices:
- Automatic-Reboot "true" with a fixed time, as above. Best for single servers that can be down for a minute at 3:30am.
- Leave it off and get told. The email report and the login message ("System restart required") remind you, and you reboot when it suits.
- Live patching. Ubuntu Pro (free for personal use on a handful of machines) includes Livepatch for kernel fixes, which reduces how often you need to reboot.
On Ubuntu, needrestart also runs after upgrades and restarts services that are still using old libraries, so most non-kernel fixes take effect without a reboot.
Email reports
Unattended-Upgrade::Mail only works if the server can send email. A full mail server isn't needed. Installing bsd-mailx with a relay such as msmtp-mta pointed at your email provider's SMTP server is enough. Test it with echo test | mail -s test you@example.com before you rely on it. If you'd rather not deal with mail, the log files below are the fallback.
Hold back packages you don't want touched
Some packages are better upgraded by hand, such as a database you'd rather upgrade during a maintenance window. Exclude them with Package-Blacklist. Entries are regular expressions:
Unattended-Upgrade::Package-Blacklist {
"mariadb-";
"postgresql-";
};
For a one-off hold, sudo apt-mark hold <package> works for both manual and automatic upgrades. Remember to apt-mark unhold it later, because held packages also miss their security fixes.
Third-party repositories (Docker, PHP, Node)
Packages from other repositories, such as Docker's apt repo, Ondřej Surý's PHP PPA or NodeSource, aren't upgraded unless you add their origin. Find the origin and suite with:
apt-cache policy docker-ce | head -20
Look for the release line, which shows values such as o=Docker (origin) and a= or n= (archive and codename). Then add a matching pattern in your local file:
Unattended-Upgrade::Origins-Pattern {
"origin=Docker,codename=${distro_codename}";
};
Think before you do this. An automatic Docker engine upgrade restarts every container on the host. For Docker I prefer to leave it manual and run apt upgrade when I'm ready.
Test it now instead of waiting a day
A dry run shows exactly what would be installed and why, without changing anything:
sudo unattended-upgrade --dry-run --debug
Check that the output lists your allowed origins and doesn't end with errors. To make it run for real straight away:
sudo unattended-upgrade -v
Confirm it keeps working
Look at the timers:
systemctl list-timers 'apt-daily*'
Both should show a next run time. Then read the logs:
/var/log/unattended-upgrades/unattended-upgrades.loglists what was checked and installed on each run./var/log/unattended-upgrades/unattended-upgrades-dpkg.loghas the full package output if something failed./var/log/apt/history.logrecords every apt transaction, automatic or not.
A quick way to see the last few automatic runs:
sudo grep -h "Packages that will be upgraded\|No packages found" /var/log/unattended-upgrades/unattended-upgrades.log | tail -5
Change when updates run
If 6am is a bad time, for example because a backup runs then, override the upgrade timer without editing the packaged unit:
sudo systemctl edit apt-daily-upgrade.timer
Add:
[Timer] OnCalendar= OnCalendar=*-*-* 02:00 RandomizedDelaySec=30m
The empty OnCalendar= line clears the default schedule before setting yours. Save, then confirm with systemctl list-timers apt-daily-upgrade.timer. My systemd units guide explains timers in more detail.
Troubleshooting
- Nothing ever installs. Check
20auto-upgradeshas both lines set to"1", then run the dry run and read the "Allowed origins" line. - "Lock could not be acquired". Another apt process was running. It retries on the next run. If it happens every day, look for a stuck
aptordpkgprocess. - dpkg was interrupted. Run
sudo dpkg --configure -aand thensudo apt -f install. - Laptops. It skips runs on battery when
Unattended-Upgrade::OnlyOnACPoweris true, which is a good default. - Disk full. Old kernels fill small
/bootpartitions.Remove-Unused-Kernel-Packages "true"prevents that.
Summary
Install the package, make sure 20auto-upgrades is switched on, keep your own settings in 52unattended-upgrades-local, decide how you'll handle reboots, and check the log once a week. It takes ten minutes, and it closes the gap that most real-world compromises go through. Pair it with SSH hardening and a firewall and a small server is in good shape.
Comments (0)
No comments yet. Be the first to leave one!
Leave a Comment