Securing VPS/Servers

Automatic Security Updates on Linux: unattended-upgrades and dnf-automatic

Set up automatic security updates on Ubuntu, Debian, AlmaLinux and Rocky Linux, confirm that they run, and handle the restarts and reboots that make the fixes take effect.

Most servers that get broken into were running software with a fix already available. Automatic security updates close that gap without anyone having to remember. This guide sets them up on both Linux families, shows how to confirm they run, and covers the part most tutorials skip: an update only protects you once the affected service or the kernel has been restarted.

Commands are given for Ubuntu 26.04/24.04 and Debian 13 (apt) and for AlmaLinux and Rocky Linux 9 and 10 (dnf). They are standard tools from each distribution; this guide is a reference and was not run on a test server for this article.

Ubuntu and Debian: unattended-upgrades

sudo apt update
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Answer "Yes". This writes /etc/apt/apt.conf.d/20auto-upgrades with two lines that switch on the daily run:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

What gets installed is decided in /etc/apt/apt.conf.d/50unattended-upgrades. By default that is the security updates of your release. Useful options in the same file (remove the // to enable a line):

Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:30";
Unattended-Upgrade::Mail "you@example.com";

Mail needs a working mail command on the server. Automatic reboot is discussed below.

Test it:

sudo unattended-upgrade --dry-run --debug

Confirm it runs:

systemctl list-timers 'apt-daily*'
sudo tail -n 30 /var/log/unattended-upgrades/unattended-upgrades.log

AlmaLinux and Rocky Linux: dnf-automatic

sudo dnf install dnf-automatic

Edit /etc/dnf/automatic.conf. In the [commands] section:

upgrade_type = security
apply_updates = yes

Out of the box, apply_updates is no: updates are downloaded and you are only notified. upgrade_type = security limits installation to security advisories; use default to install all updates. If a security-only run never finds anything while dnf check-update does, a repository you use does not publish advisory data, and default is the safer choice.

sudo systemctl enable --now dnf-automatic.timer

Confirm it runs:

systemctl list-timers 'dnf-automatic*'
sudo journalctl -u dnf-automatic --since "3 days ago"

Package and timer names have changed between major versions before. If these are not found on your release, dnf search automatic and systemctl list-unit-files | grep -i automatic show the current ones. Newer versions of dnf-automatic also have a reboot setting; man dnf-automatic lists what yours supports.

The missing half: restarts

Installing a fixed package replaces files on disk. Programs that were already running keep the old code in memory until they restart, and a new kernel is not used until the server reboots.

Is a reboot needed?

# Ubuntu, Debian
cat /var/run/reboot-required 2>/dev/null && cat /var/run/reboot-required.pkgs

# AlmaLinux, Rocky Linux (dnf-utils package)
sudo needs-restarting -r

Which services still run old code?

# Ubuntu, Debian
sudo apt install needrestart
sudo needrestart

# AlmaLinux, Rocky Linux
sudo needs-restarting -s

On Debian and Ubuntu, needrestart also hooks into apt and restarts affected services after upgrades.

Should the server reboot by itself?

ApproachGood forCost
Automatic reboot at a fixed quiet hourSingle servers where a minute of downtime at night is acceptableA short outage you did not schedule; services must start cleanly on boot
Manual reboot in a monthly windowSites where you want to watch the restartKernel fixes wait until you act, and people forget
Kernel live patching (Ubuntu Pro Livepatch, KernelCare and similar)Servers that must not restart oftenA subscription for some services; reboots are reduced, not eliminated

If you choose automatic reboots, test first that the server comes back by itself: reboot it once by hand and confirm that the website, database and any containers are up without your help. systemctl is-enabled nginx mariadb shows whether services start at boot.

What automatic updates do not cover

  • Software from outside the distribution: WordPress core, plugins and themes, Composer and npm packages, binaries you downloaded by hand.
  • Container images. A patched host does not patch what runs inside containers. Rebuild or pull images regularly.
  • Third-party repositories, unless you add them to the allowed origins (Debian, Ubuntu). Check that the PHP, database or panel repository you depend on is included.
  • Release upgrades. Moving from one major version to the next is a separate, manual job.
  • End-of-life systems. When the distribution stops publishing fixes, the updater has nothing to install and reports no problem.

Recent examples of why the first two matter: the WordPress 7.1.5 and PHP-FPM fixes of November 2026 each needed their own update path.

Common worries

"An update might break my site." Security updates within a stable release are deliberately conservative: they fix the flaw and keep behaviour the same. Breakage is rare and usually much cheaper than a compromise. If a particular package must not change by itself, exclude it (Unattended-Upgrade::Package-Blacklist on Debian and Ubuntu, exclude= in /etc/dnf/dnf.conf) and update it by hand, on a schedule you actually keep.

"I want to know what changed." Enable the mail option, or read the logs shown above in your weekly check.

"What if an update fails halfway?" Run sudo dpkg --configure -a then sudo apt -f install on Debian and Ubuntu; on AlmaLinux and Rocky Linux, sudo dnf history shows the last transaction and sudo dnf history undo last can reverse it.

Quick verification

QuestionUbuntu, DebianAlmaLinux, Rocky Linux
Is it scheduled?systemctl list-timers 'apt-daily*'systemctl list-timers 'dnf-automatic*'
Did it run?/var/log/unattended-upgrades/journalctl -u dnf-automatic
Anything pending?apt list --upgradablednf check-update
Reboot needed?/var/run/reboot-requiredneeds-restarting -r

Automatic updates are step 1 of the hardening checklist and the first automated task on the maintenance checklist.

Frequently asked questions

Are unattended upgrades safe on a production server?

For security updates of a stable release, yes, and far safer than leaving known flaws open. Keep backups and a way to hear about failures.

How do I stop unattended-upgrades from rebooting?

Set Unattended-Upgrade::Automatic-Reboot "false";, which is the default. You then have to reboot yourself when the system asks.

How do I turn automatic updates off?

sudo dpkg-reconfigure -plow unattended-upgrades and answer "No", or sudo systemctl disable --now dnf-automatic.timer. Only do this if something else applies updates on a schedule.

Do automatic updates install new major versions of PHP or MySQL?

No. Within a release, distributions keep the same major version and apply fixes to it.

Share story:

Continue reading other guides on How VPS.