Skip to content

Unattended security upgrades on Debian and Ubuntu

Auto-applying security patches with a sane reboot policy is the floor for a defensible fleet. Here is the configuration that keeps hosts patched without breaking them.

Hosts that do not get security patches accumulate CVEs at a rate of roughly one critical kernel bug per quarter. Hosts that apply every patch the moment it lands risk breaking at 03:00 because a libc upgrade changed behaviour. The right answer is in between: apply security patches automatically, defer non-security updates to a maintenance window, and reboot when the kernel changes — at a scheduled time, not at apt’s whim. This article documents the configuration we ship on every Debian and Ubuntu host.

How to verify

Check the current posture.

dpkg -l unattended-upgrades apt-listchanges needrestart 2>/dev/null | tail -5
systemctl status unattended-upgrades --no-pager
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -30
grep -E '^[^/]' /etc/apt/apt.conf.d/50unattended-upgrades | head -40
cat /etc/apt/apt.conf.d/20auto-upgrades 2>/dev/null
ls /var/log/unattended-upgrades/

/etc/apt/apt.conf.d/20auto-upgrades is the small file that enables the periodic timer; without it, unattended-upgrades is installed but does nothing.

What’s happening

unattended-upgrades is a package that runs daily (via a systemd timer or the cron-apt legacy path) and applies packages from a configurable list of “origins.” The defaults on Ubuntu are conservative: only the ${distro_id}:${distro_codename}-security and the equivalent ESM-security pocket. On Debian, the default origin set is origin=Debian,codename=${distro_codename},label=Debian-Security. Either way, the daily run is a apt-get upgrade scoped to those origins only.

Two daemons cooperate:

  • apt-daily.service (and its timer) — refreshes the package list. Runs once or twice a day.
  • apt-daily-upgrade.service (and its timer) — runs unattended-upgrade to apply the eligible packages.

The pair fires on a randomized schedule (typically morning local time) to spread load on package mirrors. The result is asynchronous: a fresh CVE in OpenSSL is patched within ~24 hours on every host that has the timer enabled.

Reboot is the wrinkle. A glibc or kernel upgrade lands on disk but does not take effect until the running processes (or the kernel itself) are restarted. needrestart is the companion tool that detects this and either prompts, restarts the affected services automatically, or signals you. We pair it with a scheduled reboot window so kernel updates do not silently sit there until manual intervention.

The procedure

  1. Install the trio. On Debian, none of these are installed by default.

    sudo apt install -y unattended-upgrades apt-listchanges needrestart

    On Ubuntu, unattended-upgrades is usually present; needrestart may not be (Ubuntu 22.04+ ships it).

  2. Enable the daily timer. A small file with two lines turns the whole machinery on.

    sudo tee /etc/apt/apt.conf.d/20auto-upgrades > /dev/null <<'EOF'
    APT::Periodic::Update-Package-Lists "1";
    APT::Periodic::Unattended-Upgrade "1";
    APT::Periodic::AutocleanInterval "7";
    APT::Periodic::Download-Upgradeable-Packages "1";
    EOF

    The "1" is “run daily.” AutocleanInterval "7" cleans the apt cache weekly to keep /var from filling up.

  3. Review the allowed origins. This is the part where “auto-apply” can go wrong if you accept origins too broadly.

    sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

    The default block for Ubuntu 24.04 looks like:

    Unattended-Upgrade::Allowed-Origins {
            "${distro_id}:${distro_codename}-security";
            "${distro_id}ESMApps:${distro_codename}-apps-security";
            "${distro_id}ESM:${distro_codename}-infra-security";
    };

    For Debian 12:

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

    The conservative posture: only -security pockets. We do not auto-apply -updates (general updates) — those land in our weekly maintenance window via configuration management.

  4. Configure email notifications. Useful for fleets without centralized log analysis.

    Unattended-Upgrade::Mail "ops@example.net";
    Unattended-Upgrade::MailReport "on-change";

    Requires a working local mailer (postfix, nullmailer, or relay configured). For fleets with central log forwarding, this is redundant — the upgrade-summary lines from journalctl -u unattended-upgrades end up in Loki/ELK anyway.

  5. Decide the auto-reboot policy. The dangerous part. Defaults:

    Unattended-Upgrade::Automatic-Reboot "true";
    Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
    Unattended-Upgrade::Automatic-Reboot-Time "03:00";
    • Automatic-Reboot "true": reboot if a package upgrade requires it (kernel, etc.).
    • WithUsers "false": do not reboot if there are active logged-in users (the on-call should not be kicked off).
    • Reboot-Time "03:00": reboot at 03:00 local time.

    For a fleet where reboots are scheduled by configuration management (not by the host itself), set Automatic-Reboot "false" and let your weekly maintenance run handle it. We do this for production; we let dev hosts auto-reboot.

  6. Configure needrestart. It can run after every apt operation and restart affected services without rebooting.

    sudo nano /etc/needrestart/needrestart.conf

    Set:

    $nrconf{restart} = 'a';      # auto-restart services without prompting
    $nrconf{kernelhints} = -1;   # do not prompt about a stale kernel (we handle that in our reboot window)

    'a' means “auto-restart all” (which is mostly safe; daemons restart in the background). For interactive hosts, 'i' is the default (prompt).

  7. Dry-run to see what would be applied. Always run this after a config change.

    sudo unattended-upgrade --dry-run --debug 2>&1 | tail -40

    Output should list the packages that match your origins. If empty, nothing security-eligible is pending — that is the steady state on a freshly-patched host.

  8. Verify the logs after the next run.

    sudo journalctl -u unattended-upgrades --since today --no-pager
    ls /var/log/unattended-upgrades/
    sudo cat /var/log/unattended-upgrades/unattended-upgrades.log | tail -30

    Successful runs end with All upgrades installed. Errors include dpkg --configure -a failures (a half-applied state from before) and held packages.

  9. Hold packages that should never auto-upgrade. Postgres or MySQL versions, for instance, where a minor version bump on production at 03:00 is unwise.

    sudo apt-mark hold postgresql-14
    apt-mark showhold

    Held packages are skipped by unattended-upgrades.

  10. For fleets, pin the config in configuration management.

    - name: Configure unattended-upgrades
      ansible.builtin.template:
        src: 50unattended-upgrades.j2
        dest: /etc/apt/apt.conf.d/50unattended-upgrades
        owner: root
        group: root
        mode: '0644'
    - name: Enable periodic timer
      ansible.builtin.copy:
        content: |
          APT::Periodic::Update-Package-Lists "1";
          APT::Periodic::Unattended-Upgrade "1";
        dest: /etc/apt/apt.conf.d/20auto-upgrades

Common pitfalls

  • unattended-upgrades installed but 20auto-upgrades missing — the package is present but the timer never runs. Nothing patches.
  • Allowing -updates and -backports origins thinking “more updates is more security” — those origins include functional updates, not just security. Auto-applying them is what causes 03:00 outages.
  • Automatic-Reboot "true" on a host with no maintenance window — at some point the reboot lands during the day and someone is surprised.
  • needrestart set to 'l' (list, do not act) on production — the alerts pile up because nothing restarts the affected daemons.
  • Cron-apt and unattended-upgrades both installed — they fight, you get random failures from apt-lock contention.

In our managed engagements, security patches apply within 24 hours on every host, kernel reboots happen in the documented weekly window, and the upgrade log is forwarded centrally so missed runs are visible. That patching cadence is part of the Managed Operations operating contract — and it is what makes a “did we patch CVE-XXXX-YYYY?” question answerable in seconds.