Security1 min read

Automatic security updates with unattended-upgrades

Let Debian and Ubuntu install security updates automatically, decide what gets updated, schedule reboots and check that everything works.

Most successful attacks exploit vulnerabilities for which an update has long been available. With unattended-upgrades your server installs security updates by itself, every day, without you having to remember.

Install and enable

sudo apt update
sudo apt install unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades

Answer the question with Yes. This creates /etc/apt/apt.conf.d/20auto-upgrades:

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

The server now checks for updates daily and installs them.

What gets updated?

By default only security updates from the official sources. That is the safe choice, because these updates only fix bugs and introduce nothing new. The settings are in /etc/apt/apt.conf.d/50unattended-upgrades. On Debian the important part looks like this:

Unattended-Upgrade::Origins-Pattern {
        "origin=Debian,codename=${distro_codename},label=Debian-Security";
        "origin=Debian,codename=${distro_codename}-security,label=Debian-Security";
};

If you also want regular bug fixes for the stable release, add the line with ${distro_codename}-updates. Packages from third-party sources such as Docker or Caddy are better updated deliberately by hand.

Exclude packages

If a package should never be updated automatically, for example because your application needs exactly this version:

Unattended-Upgrade::Package-Blacklist {
    "mariadb-server";
};

Automatic reboots

Some updates, especially kernel updates, only take effect after a reboot. Whether one is needed is shown by the file /var/run/reboot-required. You can let the server reboot automatically at night for this:

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";

If you allow automatic reboots, make sure all services come back up by themselves: systemctl enable for your own services, restart: unless-stopped for Docker containers.

Without automatic reboots, check now and then:

[ -f /var/run/reboot-required ] && cat /var/run/reboot-required.pkgs

Test that it works

A dry run that shows what would happen without installing anything:

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

What was actually installed is in the log:

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

And when the next run is:

systemctl list-timers apt-daily*

And major version upgrades?

unattended-upgrades never switches to a new major version by itself, for example from Debian 12 to 13. That stays your decision. Make a backup first, for example a backup in the TryNxt panel.

Written by TryNxt team

We build and run TryNxt ourselves and write down here what we learn along the way.