Most successful attacks on servers do not use clever new exploits. They use known vulnerabilities in software that was never updated — an old OpenSSH, an outdated OpenSSL, a kernel bug fixed months earlier. Installing security patches promptly is one of the cheapest and most effective things you can do, and Linux can do it for you automatically.
This guide sets up automatic security updates on the major server distributions, explains how to handle reboots, and shows how to check that it is actually working.
What should update automatically?
| Update type | Automatic? | Why |
|---|---|---|
| Security patches (OpenSSL, OpenSSH, kernel, libc…) | Yes | Small, targeted fixes; risk of breakage is low and the risk of waiting is high |
| Regular bug-fix updates | Optional | Usually safe; enable if you do not test updates yourself |
| Major version upgrades (Ubuntu 22.04 → 24.04, PHP 8.2 → 8.3) | No | Can change behaviour; do these by hand with a backup |
| Third-party repositories (Docker, MariaDB, Node.js) | Case by case | A new major version can arrive through the same repo |
Ubuntu and Debian: unattended-upgrades
Ubuntu usually has the package installed already; Debian minimal images may not.
sudo apt update
sudo apt install unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades # answer "Yes"
This creates /etc/apt/apt.conf.d/20auto-upgrades:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
Both values mean “every day”.
Choose what gets installed
The main settings live in /etc/apt/apt.conf.d/50unattended-upgrades. Rather than editing it (package upgrades may want to replace it), put your changes in a later file:
sudo nano /etc/apt/apt.conf.d/52unattended-upgrades-local
// Remove unused dependencies and old kernels
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
// Reboot automatically if a kernel or libc update needs it, at a quiet time
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
// Email a report (requires a working mail command on the server)
// Unattended-Upgrade::Mail "you@example.com";
// Unattended-Upgrade::MailReport "on-change";
// Never auto-update these packages
Unattended-Upgrade::Package-Blacklist {
"mariadb-server";
};
By default only the security origin is enabled, which is what most servers want. To also install regular updates, uncomment the -updates line in the Allowed-Origins (Ubuntu) or Origins-Pattern (Debian) section of 50unattended-upgrades.
Test it
Do a dry run and see what would happen:
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -30
Logs of real runs are kept in /var/log/unattended-upgrades/. Check them occasionally:
sudo tail -n 50 /var/log/unattended-upgrades/unattended-upgrades.log
AlmaLinux, Rocky Linux and CentOS Stream: dnf-automatic
sudo dnf install dnf-automatic
sudo nano /etc/dnf/automatic.conf
Set at least:
[commands]
upgrade_type = security
download_updates = yes
apply_updates = yes
# reboot = when-needed # available on newer releases; see below
[emitters]
emit_via = stdio
upgrade_type = security limits automatic installs to packages with security advisories. Then enable the timer:
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic*
The timer runs once a day with a random delay. Output goes to the journal:
sudo journalctl -u dnf-automatic -n 50
Reboots: the part people forget
Updated libraries and kernels only take effect after the processes using them restart — and for the kernel, after a reboot. A server that installs patches but never reboots is still running vulnerable code.
Check whether a reboot is pending:
# Ubuntu / Debian
[ -f /var/run/reboot-required ] && cat /var/run/reboot-required.pkgs
# AlmaLinux / Rocky
sudo dnf needs-restarting -r
Options:
- Automatic reboot at a fixed quiet time (shown above for Ubuntu). Simple and effective for a single web server with a short restart.
- Manual reboot within a week, scheduled in your maintenance window. Better when downtime must be announced or several servers must be rebooted in order.
- Restart services only.
sudo needrestart(Debian/Ubuntu) lists and restarts services using outdated libraries without a full reboot. It does not cover kernel updates.
Live kernel patching services exist for some distributions, but for most small and medium deployments a short scheduled reboot is the most reliable choice.
Make updates safe
Automatic patching is low-risk, but not zero-risk. Reduce it further:
- Have backups. Updates rarely break things; when they do, a recent backup makes it a minor incident. See automated backups with restic.
- Pin what must not change. Hold a package at its current version:
sudo apt-mark hold mariadb-server # Debian / Ubuntu
sudo dnf install 'dnf-command(versionlock)' && sudo dnf versionlock add mariadb-server
- Check third-party repositories. If you added Docker, Node.js or a database vendor’s repo, decide whether those should update automatically. Many teams update them by hand.
- Monitor the site. An external check that alerts you if the website goes down after a nightly update catches the rare problem quickly — see uptime monitoring with Uptime Kuma.
Control panels and Windows
- aaPanel, 1Panel and BT panel manage their own components (web server, PHP versions). Keep the OS updates on as above and update the panel from its own interface.
- Windows Server uses Windows Update; configure active hours and a maintenance window in Settings → Windows Update → Advanced options or with Group Policy. See Windows Server initial setup.
Manual updates are still needed
Automatic security patches do not replace a regular maintenance routine. Once a month, log in and:
sudo apt update && sudo apt list --upgradable # or: sudo dnf check-update
sudo apt upgrade # or: sudo dnf upgrade
And plan distribution upgrades before your OS reaches end of life. Running an unsupported release means security patches stop arriving altogether — automatic updates cannot help then. If the jump is large, deploying a fresh server with the new release and migrating to it is often cleaner than upgrading in place.
Checklist
- unattended-upgrades or dnf-automatic installed and enabled
- Dry run or log shows packages being processed
- Reboot policy decided (automatic time, or manual within a week)
- Critical packages held if they must not change unexpectedly
- Backups and uptime monitoring in place