TExcelTAD
Home › Linux Server Administration › Patching & Unattended Upgrades

Patching & Unattended Upgrades

Most compromised servers weren't hit by an unknown zero-day; they were running software with a vulnerability fixed weeks or months earlier. Patching is the least glamorous and most effective security control you have. The goal is a routine where security updates install themselves, bigger changes are planned, and you always know which servers still need a restart. Package Managers covered apt and dnf themselves; this lesson is the server policy around them.

Three kinds of update

Kind Example How to handle
security updates within a release OpenSSL, OpenSSH, the kernel, glibc fixes automatically, daily
regular updates within a release bug fixes, minor versions automatically or weekly, after they've been tested on staging
release upgrades Ubuntu 22.04 → 24.04, RHEL 9 → 10 planned projects; on cloud servers usually by building new servers from a new image instead of upgrading in place

Know your distribution's support windows (Ubuntu LTS: 5 years of standard support, more with Ubuntu Pro; RHEL: 10 years; Debian: about 3 years plus LTS) and keep an inventory of what runs where; an end-of-life OS gets no security updates at all.

unattended-upgrades on Debian and Ubuntu

Ubuntu Server installs and enables it by default; it runs from two systemd timers, apt-daily.timer (download package lists) and apt-daily-upgrade.timer (install). The policy lives in two files:

text
// /etc/apt/apt.conf.d/20auto-upgrades
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
text
// /etc/apt/apt.conf.d/52unattended-upgrades-local   (your overrides; keep the packaged 50unattended-upgrades untouched)
Unattended-Upgrade::Allowed-Origins {
    "${distro_id}:${distro_codename}-security";
    "${distro_id}ESMApps:${distro_codename}-apps-security";
    "${distro_id}ESM:${distro_codename}-infra-security";
};
Unattended-Upgrade::Package-Blacklist {
    "postgresql-16";            // upgrade the database deliberately, with a maintenance window
};
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";
Unattended-Upgrade::Mail "ops@example.com";
Unattended-Upgrade::MailReport "only-on-error";
bash
$ sudo unattended-upgrade --dry-run --debug 2>&1 | grep -E 'Allowed origins|Packages that will be upgraded'
Allowed origins are: o=Ubuntu,a=noble-security, o=UbuntuESMApps,a=noble-apps-security, o=UbuntuESM,a=noble-infra-security
Packages that will be upgraded: libssl3t64 openssl openssh-client openssh-server openssh-sftp-server
$ tail -3 /var/log/unattended-upgrades/unattended-upgrades.log
2026-01-15 06:24:11,402 INFO Packages that will be upgraded: libssl3t64 openssl
2026-01-15 06:24:11,403 INFO Writing dpkg log to /var/log/unattended-upgrades/unattended-upgrades-dpkg.log
2026-01-15 06:24:19,118 INFO All upgrades installed

On RHEL and Fedora the equivalent is dnf-automatic (/etc/dnf/automatic.conf with upgrade_type = security and apply_updates = yes, then systemctl enable --now dnf-automatic.timer).

When do the timers fire?

The stock apt-daily-upgrade.timer runs at 06:00 plus a random delay of up to an hour. For a fleet you want upgrades inside a maintenance window, and you want to know exactly when it is: change the timer with a drop-in (Drop-ins & Overrides) and check the schedule with systemd-analyze before rolling it out:

bash · try it yourself
mkdir -p apt-daily-upgrade.timer.d
cat > apt-daily-upgrade.timer <<'EOF'
[Unit]
Description=Daily apt upgrade and clean activities
[Timer]
OnCalendar=*-*-* 6:00
RandomizedDelaySec=60m
Persistent=true
[Install]
WantedBy=timers.target
EOF
cat > apt-daily-upgrade.service <<'EOF'
[Service]
Type=oneshot
ExecStart=/usr/bin/true
EOF
cat > apt-daily-upgrade.timer.d/maintenance-window.conf <<'EOF'
[Timer]
OnCalendar=
OnCalendar=Tue,Thu *-*-* 02:00
RandomizedDelaySec=30m
EOF
systemd-analyze verify ./apt-daily-upgrade.timer && echo "timer + drop-in: OK"
systemd-analyze calendar --base-time="2026-01-15 10:00:00 UTC" --iterations=4 "Tue,Thu *-*-* 02:00" | grep -E 'Normalized|elapse|Iteration'
timer + drop-in: OK
Normalized form: Tue,Thu *-*-* 02:00:00
    Next elapse: Tue 2026-01-20 02:00:00 UTC
   Iteration #2: Thu 2026-01-22 02:00:00 UTC
   Iteration #3: Tue 2026-01-27 02:00:00 UTC
   Iteration #4: Thu 2026-01-29 02:00:00 UTC

The empty OnCalendar= clears the vendor's schedule first (it is a list setting); without it the timer would fire at both times. With RandomizedDelaySec=30m every server picks its own moment between 02:00 and 02:30, so a fleet doesn't hit the mirror, or restart all replicas of a service, at once.

Restarts: the part people skip

Installing a fixed library doesn't fix running processes: they keep the old, vulnerable copy mapped in memory until they restart. Installing a new kernel does nothing until you reboot.

bash
$ cat /var/run/reboot-required.pkgs 2>/dev/null
linux-image-6.8.0-90-generic
linux-base
$ sudo needrestart -r l -b                  # list mode, batch output
NEEDRESTART-VER: 3.6
NEEDRESTART-KCUR: 6.8.0-88-generic
NEEDRESTART-KEXP: 6.8.0-90-generic
NEEDRESTART-KSTA: 3
NEEDRESTART-SVC: nginx.service
NEEDRESTART-SVC: inventory.service

needrestart (installed by default on Ubuntu Server) lists services still using deleted, replaced libraries and offers to restart them after each apt run; KSTA: 3 means a newer kernel is installed than the one running. Options for the reboot itself:

  • Scheduled automatic reboots (Automatic-Reboot "true" with a time) for stateless servers behind a load balancer, staggered so not all replicas restart together.
  • Coordinated reboots: tools like kured (Kubernetes) or a fleet scheduler drain one node at a time.
  • Live patching (Canonical Livepatch, RHEL kpatch) applies critical kernel fixes without a reboot, buying time but not replacing reboots.
  • Immutable servers: don't patch in place at all; build a new image with the updates and replace the servers (Infrastructure as Code & Immutable Infrastructure).

Holding and pinning

Sometimes a package must not move: a database with a planned upgrade, a kernel with a known driver problem.

bash
$ sudo apt-mark hold postgresql-16
postgresql-16 set on hold.
$ apt-mark showhold
postgresql-16
$ sudo dnf versionlock add postgresql-server     # RHEL, with the versionlock plugin

Every hold needs an owner and an end date in your ticket system: a forgotten hold is a slowly growing vulnerability.

Test yourself

OpenSSL was patched by unattended-upgrades this morning. Are running services protected?

  1. Yes, immediately
  2. Only after they restart: running processes keep the old library in memory (needrestart lists them)
  3. Only after a kernel update
  4. Only if they use TLS 1.3

Why add RandomizedDelaySec= to the upgrade timer on a fleet?

  1. It makes upgrades faster
  2. Each server picks a different moment, so the mirror isn't hammered and replicas don't all restart at once
  3. It is required by apt
  4. It disables Persistent=

What is the safest default policy for a typical production server?

  1. Never update without a ticket
  2. Install security updates automatically every day, schedule reboots, and plan release upgrades
  3. Upgrade to the next release automatically
  4. Only update when something breaks