Sub topic 14
Turn on automatic security updates
Unpatched services are one of the most common ways a server gets compromised. The previous chapter covered applying pending updates by hand. This chapter automates the security subset so the server keeps patching itself between your visits.
This chapter assumes you have a working admin user with sudo, a server that has been fully updated once, and basic familiarity with systemd units and editing files under /etc.
Why automate security updates
A manual apt full-upgrade only protects the server on the day you run it. Most vulnerabilities become public with a fix already available, so the gap between publication and your next login is the exposure window. Automatic updates shrink that window to hours.
Automation has a cost. A package update can restart a service, change a configuration default, or occasionally break an application. The usual compromise is:
- Apply security updates automatically.
- Apply general feature and bug-fix updates on a schedule you control.
- Reboot only when you decide to, or in a defined maintenance window.
How the two families do it
Debian and Ubuntu use the unattended-upgrades package, driven by systemd timers and two configuration files. RHEL-family systems use dnf-automatic, also driven by a systemd timer.
| Aspect | Debian/Ubuntu | RHEL family |
|---|---|---|
| Package | `unattended-upgrades` | `dnf-automatic` |
| Main config | `/etc/apt/apt.conf.d/50unattended-upgrades` | `/etc/dnf/automatic.conf` |
| Enable switch | `/etc/apt/apt.conf.d/20auto-upgrades` | A systemd timer unit |
| Default scope | Security origin only | Configurable (`upgrade_type`) |
| Log | `/var/log/unattended-upgrades/` | `journalctl -u dnf-automatic*` |
Debian and Ubuntu: unattended-upgrades
Install the package and turn on the periodic job.
sudo apt update
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgradesThe dpkg-reconfigure step writes /etc/apt/apt.conf.d/20auto-upgrades. You can also create it yourself:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";The value 1 means "run every 1 day". The first line refreshes package metadata. The second runs the upgrade.
Choose what gets installed
The file /etc/apt/apt.conf.d/50unattended-upgrades lists allowed origins. On a stock install the security origin is enabled and the general updates origin is commented out. Leave it that way for a conservative setup. Key settings to review:
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";
};
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";Origin names differ by distribution. Debian uses entries such as origin=Debian,codename=${distro_codename}-security. Copy the format that already exists in your file instead of mixing styles.
Test before trusting it
A dry run shows what would be installed without changing anything.
sudo unattended-upgrade --dry-run --debug
systemctl list-timers 'apt-daily*'
sudo tail -n 30 /var/log/unattended-upgrades/unattended-upgrades.logThe timers apt-daily.timer and apt-daily-upgrade.timer should both appear with a next run time. If they do not, the package is installed but nothing will run it.
RHEL family: dnf-automatic
Install the package and edit its configuration.
sudo dnf install dnf-automatic
sudo nano /etc/dnf/automatic.confSet the following values in the file.
[commands]
upgrade_type = security
download_updates = yes
apply_updates = yes
[emitters]
emit_via = stdioupgrade_type = security limits installs to packages with a security advisory. Setting it to default installs everything. With apply_updates = no the tool only downloads and reports, which is a good first stage on a server you are still learning to trust.
Enable the timer that downloads and applies updates:
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers 'dnf-automatic*'Newer releases also ship dnf-automatic-install.timer and related variants that fix the behavior regardless of the config file. Use one timer, not several. Check which units exist with systemctl list-unit-files 'dnf-automatic*'.
Handling reboots
Some updates only take effect after a reboot. The kernel, glibc and systemd are the usual cases. A patched library that a running process still has loaded stays vulnerable until that process restarts.
- Debian and Ubuntu create
/var/run/reboot-requiredwhen a reboot is needed. Check withtest -f /var/run/reboot-required && echo reboot needed. - RHEL-family systems provide
needs-restarting -rfrom thednf-utilspackage (yum-utilson older releases). It exits with status 1 when a reboot is needed. - To restart only affected services, use
sudo needs-restarting -son RHEL or theneedrestartpackage on Debian and Ubuntu.
Choose a policy that fits the server:
- Single server with a short outage tolerated: enable
Automatic-Rebootwith a quiet-hours time. - Redundant pool behind a load balancer: reboot one node at a time using your own tooling.
- Anything else: leave reboots manual and monitor for the flag file.
Common problems
- Nothing happens. The timer is not enabled, or the machine is usually off at the scheduled time. Check
systemctl list-timers. Timers markedPersistent=truecatch up after boot. - The package manager is locked. An unattended run holds the dpkg lock while it works. Wait for it instead of deleting lock files.
- Held or pinned packages are skipped. This is by design. Review them with
apt-mark showhold. - Third-party repositories are ignored. The allowed-origins list only matches what you name. Adding a third-party origin means trusting that vendor to push updates unattended.
- Updates change a service's behavior. Restrict to security origins, and keep a snapshot of the baseline so you can roll back.
- No one reads the log. Configure mail or a monitoring check. On Debian,
Unattended-Upgrade::Mailsends a report when a mail command is available.
Verification commands
Use these to confirm the setup is active and working.
# Debian/Ubuntu
systemctl is-enabled apt-daily-upgrade.timer
apt-config dump | grep -i Periodic
grep -v '^\s*//' /etc/apt/apt.conf.d/50unattended-upgrades | grep -v '^$'
# RHEL family
systemctl is-enabled dnf-automatic.timer
grep -E '^(upgrade_type|apply_updates)' /etc/dnf/automatic.confExercise
A Debian server has unattended-upgrades installed, and /var/log/unattended-upgrades/ is empty after a week. Name two checks that would locate the problem.
Show answer
First, run systemctl list-timers 'apt-daily*' to confirm that apt-daily.timer and apt-daily-upgrade.timer are active and have a next run time. Second, check /etc/apt/apt.conf.d/20auto-upgrades (or apt-config dump | grep Periodic) to confirm that APT::Periodic::Unattended-Upgrade is set to "1". A dry run with sudo unattended-upgrade --dry-run --debug also shows whether the allowed origins match any pending packages.
Exercise
On Rocky Linux you set upgrade_type = security in /etc/dnf/automatic.conf but the server still has no scheduled runs. What is missing, and what command fixes it?
Show answer
The configuration file only defines behavior. A systemd timer must be enabled to start the job. Run sudo systemctl enable --now dnf-automatic.timer, then confirm with systemctl list-timers 'dnf-automatic*'.
Exercise
Why can a server that installs security updates automatically still be vulnerable to a patched flaw in a system library?
Show answer
Running processes keep the old library loaded in memory until they restart. The package files on disk are updated, but the vulnerable code stays in use until the affected services are restarted or the server is rebooted. Check /var/run/reboot-required, needs-restarting -r, or needrestart.