Sub topic 12
Automating Updates with unattended-upgrades and dnf-automatic
This chapter assumes you can already review and apply updates by hand with apt or dnf. It also assumes you know how to read systemctl timer status and how to tell when a reboot is needed. Automation runs those same steps on a schedule, so the manual commands remain your debugging tools.
What automation should and should not do
Unattended updates close the gap between a vendor publishing a fix and you installing it. On a fleet, that gap is often longer than the exploit window. The risk is that a package changes under a running service with nobody watching.
A conservative policy balances the two:
- Automatically install security updates only.
- Do not reboot automatically unless the server is redundant or you set a quiet window.
- Send a report, so a human sees what changed.
- Keep a manual path for held packages and major upgrades.
Both tools implement this policy, but with different defaults and different configuration files.
| Aspect | Debian/Ubuntu | RHEL-family |
|---|---|---|
| Package | `unattended-upgrades` | `dnf-automatic` |
| Main config | `/etc/apt/apt.conf.d/50unattended-upgrades` | `/etc/dnf/automatic.conf` |
| Schedule | `apt-daily.timer`, `apt-daily-upgrade.timer` | `dnf-automatic*.timer` |
| Security filter | Allowed-Origins list | `upgrade_type = security` |
| Reboot option | `Automatic-Reboot` | `reboot` (newer dnf versions) |
| Log | `/var/log/unattended-upgrades/` | journal, plus the configured emitter |
How the pieces fit together
Neither tool runs as a long-lived daemon. A systemd timer starts a one-shot service, which exits when the work is done. When automation misbehaves, check the timer first (systemctl list-timers), then the service's journal.
Debian and Ubuntu: unattended-upgrades
Enable it
Install the package and write the two periodic settings. The first refreshes package lists daily. The second runs unattended-upgrade daily. The final command is a dry run that shows what would be installed.
sudo apt install unattended-upgrades
sudo tee /etc/apt/apt.conf.d/20auto-upgrades >/dev/null <<'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOF
sudo unattended-upgrade --dry-run --debugThe values are days between runs. "1" means daily and "0" disables the job. On Ubuntu, the installer often creates 20auto-upgrades for you, so check the file before overwriting it.
Choose what is allowed
The file /etc/apt/apt.conf.d/50unattended-upgrades decides which packages qualify. The key setting is Unattended-Upgrade::Allowed-Origins. It lists the repositories whose updates may be installed without you. A package from an origin that is not listed is never touched, even if it is upgradable.
On Ubuntu the default list includes the release pocket and its -security pocket. Debian's default includes the security archive. A conservative setup keeps only the security entries and comments out -updates and -proposed. Comments in this file start with //.
A reasonable set of options looks like this:
Unattended-Upgrade::Package-Blacklist {
"nginx";
};
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false";
Unattended-Upgrade::Mail "root";
Unattended-Upgrade::MailReport "on-change";Package-Blacklistskips packages you want to patch by hand. It uses regular expressions, so anchor names ("^nginx$") if you need an exact match.Remove-Unused-Dependenciesdoes the work ofautoremoveafter each run.Automatic-Rebootstays"false"until you have thought about it (see below).Mailneeds a working mail transfer agent on the host. Without one, the report goes nowhere and you will not see an error.
Check that it ran
Three places tell you what happened:
systemctl list-timers 'apt-daily*'
sudo tail -n 30 /var/log/unattended-upgrades/unattended-upgrades.log
grep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.logThe first shows the next and last run times. The second shows which packages matched and which were skipped, and why. The third is the normal apt history. A log line such as "No packages found that can be upgraded unattended" means the run worked and nothing qualified.
RHEL-family: dnf-automatic
Install and configure
Install the dnf-automatic package. All behavior is controlled by /etc/dnf/automatic.conf. The package's default config only downloads or notifies on some releases, so read the file before assuming it installs anything. This excerpt installs security updates only:
# /etc/dnf/automatic.conf (excerpt)
[commands]
upgrade_type = security
apply_updates = yesStart the timer with sudo systemctl enable --now dnf-automatic.timer.
Four settings in [commands] matter most:
upgrade_typeisdefaultfor all updates orsecurityfor security fixes only.download_updatescontrols whether packages are downloaded.apply_updatescontrols whether they are installed.random_sleepdelays the start by a random number of seconds, so many servers do not hit the mirror at once.
Pick the right timer
The package ships several timers, each a preset for the download and apply settings:
| Timer | Behavior |
|---|---|
| `dnf-automatic.timer` | Follows `automatic.conf` |
| `dnf-automatic-notifyonly.timer` | Reports pending updates, installs nothing |
| `dnf-automatic-download.timer` | Downloads but does not install |
| `dnf-automatic-install.timer` | Downloads and installs |
The notify-only timer is a good first step on a new server. You see what automation would do for a week before you let it act.
Reporting
The [emitters] section sets emit_via. The values stdio (output to the journal or terminal), email and motd can be combined as a comma-separated list. For email, fill in [email] with email_from, email_to and email_host. As with apt, a report that cannot be delivered gives you a false sense of safety, so send a test.
To see past runs:
systemctl status dnf-automatic.timer
journalctl -u dnf-automatic.service --since yesterday
sudo dnf history listBecause dnf-automatic uses dnf, its transactions appear in dnf history, and you can inspect or undo them like any manual one.
Reboots and restarts under automation
Installing a kernel or glibc update does not change what is running until a reboot. Automation can hide this: the server reports "patched" while the old kernel is still in memory.
On Debian and Ubuntu, Unattended-Upgrade::Automatic-Reboot "true" reboots when /var/run/reboot-required exists. Add Unattended-Upgrade::Automatic-Reboot-Time "02:00" to set a quiet time instead of rebooting immediately. Unattended-Upgrade::Automatic-Reboot-WithUsers "false" stops a reboot while users are logged in.
On RHEL-family systems, newer dnf versions offer a reboot option in [commands]: never, when-changed or when-needed. Check man dnf.automatic on your host to confirm that your release supports it. If it does not, use a separate scheduled check built on dnf needs-restarting -r, which exits with 1 when a reboot is recommended.
If you do not reboot automatically, you need another way to notice. This check works on Debian and Ubuntu and can run from cron or a monitoring agent:
if [ -f /var/run/reboot-required ]; then
echo "Reboot required"
cat /var/run/reboot-required.pkgs 2>/dev/null
fiService restarts are a related risk. Libraries updated in place may restart daemons automatically (on Debian via needrestart, on RHEL via package scriptlets). A restart you did not choose can drop connections, so schedule the timer for a low-traffic window.
Common problems
- Nothing happens. The timer is disabled, or the periodic settings are
"0". Checksystemctl list-timersand the config files. - A package is never updated. It is held, blacklisted, or from an origin not in
Allowed-Origins. The unattended-upgrades log names the reason. - Lock errors. A manual
aptrun overlaps the automatic one. Wait for the other process to finish rather than deleting lock files. - Config prompts block upgrades. A package asks about a modified config file. Unattended-upgrades keeps the existing file by default, and the new default is left beside it for you to review.
- No reports. The host has no working mail setup. Test with a manual message.
- Stale mirrors. All servers update at once and one mirror lags. Use the random delay and consider an internal mirror.
Fitting automation into a staged rollout
Automation does not replace testing. A common pattern is to enable it fully on a test or canary group first, so any bad update appears there. Production servers then run the same tool a few days later, either by using a later timer window or by pointing them at an internal repository snapshot that you update only after the canary group looks healthy. Verify with the logs and with a service check after each run.
Exercise
A Debian server has APT::Periodic::Unattended-Upgrade "1"; set, but the nginx package never gets security updates, and apt-mark showhold is empty. Name two other places to look, and the command that shows the reason.
Show answer
Check Unattended-Upgrade::Package-Blacklist in /etc/apt/apt.conf.d/50unattended-upgrades for an nginx entry, and check Unattended-Upgrade::Allowed-Origins in the same file for the repository that supplies nginx. If nginx comes from a third-party repository, its origin is not allowed by default. Run sudo unattended-upgrade --dry-run --debug, or read /var/log/unattended-upgrades/unattended-upgrades.log, to see why the package was skipped.
Exercise
You want a RHEL-family server to install security updates automatically, but to only send you a report for all other updates. Can one automatic.conf do this? What would you do instead?
Show answer
No. upgrade_type is a single setting, so one configuration applies either to security updates or to all updates. Set upgrade_type = security and apply_updates = yes for the install job. For a report on everything, use a separate check such as dnf check-update (exit code 100 means updates are available), or run a second configuration with the notify-only timer. Keep in mind that two jobs should not run at the same time.