unattended-upgrades: Let Your Server Patch Itself While You Sleep (Safely)

The most dangerous patch is the one waiting for you to have a free afternoon. unattended-upgrades installs security fixes on its own. Here's how to check it's on, what it touches, and how to stop it rebooting your database at dinner.

Silas Voss

3 min read

There is a patch on your server's to-do list right now. It's been there since Tuesday. It's waiting for you to have a free afternoon.

You will not have a free afternoon. Nobody has had a free afternoon since 2019.

unattended-upgrades is the tool that stops waiting for you. It installs security updates on its own, quietly, every day. On Ubuntu it's usually installed and switched on already, which means it may have been saving you for years without a thank-you card. Today we check that it's working, decide what it may touch, and stop it from rebooting things at dinner.

Is it actually on?

Two settings decide whether it runs. Look here:

Bash
cat /etc/apt/apt.conf.d/20auto-upgrades

You want to see:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

"1" means every day. If the file is missing, or says "0", it's off. Turn it on the official way:

Bash
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Answer "Yes", and it writes that file for you.

The schedule itself comes from two systemd timers, apt-daily.timer (refresh the package lists) and apt-daily-upgrade.timer (install). If you read Tux's systemd timers post, you know the trick:

Bash
systemctl list-timers 'apt-daily*'

What is it allowed to touch?

The rules live in /etc/apt/apt.conf.d/50unattended-upgrades. The important part is the list of allowed origins. On Ubuntu, the defaults include a line like:

"${distro_id}:${distro_codename}-security";

That's the security pocket: fixes for known vulnerabilities, kept deliberately small and boring. Boring is the point. It's a different thing from "upgrade everything to the newest version whenever", which is how you wake up to a surprise PHP version.

To keep a package away from automatic updates entirely, list it:

Unattended-Upgrade::Package-Blacklist {
    "postgresql-";
};

Entries are matched as patterns, so postgresql- covers the whole family. Use this sparingly. Every package on the list is one you've promised to patch by hand. Promise carefully.

Try it without consequences

Before trusting it, watch it think out loud:

Bash
sudo unattended-upgrade --dry-run --debug

It goes through what it would install and why, and changes nothing. If it says something you didn't expect, now is the time to find out, not after.

What it actually did ends up here:

Bash
less /var/log/unattended-upgrades/unattended-upgrades.log

Read it once a week, or better, have monitoring read it for you. "I assumed it was working" is a sentence I hear mostly at incident reviews.

Reboots: the part that bites

Kernel fixes need a reboot. unattended-upgrades can do that too, and you get to choose:

Unattended-Upgrade::Automatic-Reboot "false";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
  • false (the usual default): it patches, leaves /var/run/reboot-required behind and waits for a human. Safe, but only as good as the human.
  • true: it reboots at the time you set. Great for stateless web nodes behind a load balancer. Exciting for a lone database server. "Exciting" is not a word you want in an incident report.

Pick per server, not per mood. Two more settings worth turning on:

Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Mail "root";

The first stops old kernels from filling /boot until updates start failing. The second sends a report, provided the server can actually send mail. Check that, or you're mailing reports into the void. The void does not read them.

Restarting what it patched

Updating a library doesn't fix the processes that already loaded the old one. That's needrestart's job, and it works hand in hand with unattended-upgrades. See last week's post, "Patched Isn't Running", for setting that up.

What to do today

  1. cat /etc/apt/apt.conf.d/20auto-upgrades on each server and make sure both lines say "1".
  2. Run sudo unattended-upgrade --dry-run --debug once and read what it says.
  3. Decide reboots per server: automatic for stateless boxes, a human for databases.
  4. Put the log, or the reboot-required flag, somewhere a person will actually see it.

Automated patching isn't about trusting the machine. It's about not trusting your free afternoon.