On this page
- Why automate security updates on a VPS?
- How do I enable unattended-upgrades on Ubuntu and Debian?
- Which updates does it install by default?
- How do I set a reboot policy?
- What does needrestart do after an update?
- How do I set up dnf-automatic on Rocky Linux and AlmaLinux?
- How do I test that updates really run?
- What can go wrong and how do I fix it?
- Questions
Why automate security updates on a VPS?
Automating patches closes known holes within a day instead of whenever you next remember to log in. A server with a public IPv4 and IPv6 address is scanned constantly, and most successful break-ins use flaws that already had a fix published. Small projects suffer most here: a bot, a personal site or a node that runs for months untouched slowly falls behind.
Automatic updates are not a replacement for planned maintenance. Major release upgrades, new kernels that change behaviour and application stacks pinned to exact versions still need a human. The goal of this guide is a narrow, safe policy: security fixes install themselves, everything else waits for you, and reboots happen at a time you pick. Pair it with the daily backups add-on or restic so a bad package can always be rolled back by restoring data.
How do I enable unattended-upgrades on Ubuntu and Debian?
Install the package (Ubuntu images already carry it, some Debian builds do not) and switch on the daily run:
sudo apt update
sudo apt install unattended-upgrades apt-listchanges
sudo dpkg-reconfigure -plow unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgradesThe reconfigure step writes two lines into 20auto-upgrades:
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";The value is an interval in days. Two systemd timers drive the work: apt-daily.timer refreshes package lists and apt-daily-upgrade.timer installs updates, each with a random delay so thousands of machines do not hit the mirrors at once. Check them with:
systemctl list-timers "apt-daily*"Which updates does it install by default?
Out of the box only the security pocket is allowed. Open /etc/apt/apt.conf.d/50unattended-upgrades and look at the origins block. On Ubuntu it contains lines such as "${distro_id}:${distro_codename}-security"; plus the ESM entries, and the -updates line is commented out. Debian uses Unattended-Upgrade::Origins-Pattern with label=Debian-Security.
Leave that as it is for a production server. If you want all stable updates on a hobby box, remove the // in front of the updates line. To hold back one package, for example a database you upgrade by hand, add it to the blacklist section:
Unattended-Upgrade::Package-Blacklist {
"postgresql-16";
};Entries are regular expressions, so "linux-"; would pause every kernel package.
How do I set a reboot policy?
Decide whether the server may reboot itself; kernel and libc fixes only take effect after a restart. In the same 50unattended-upgrades file, uncomment and adjust:
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
Unattended-Upgrade::Automatic-Reboot-Time "04:10";With these settings the machine reboots at 04:10 server time only when a package created /var/run/reboot-required, and skips the reboot while someone is logged in. Check the server clock and zone first (see time zone and time sync). If a reboot would hurt, for example a Bitcoin node in the middle of initial sync, keep Automatic-Reboot at false and read the flag yourself:
cat /var/run/reboot-required.pkgs 2>/dev/null || echo "no reboot needed"What does needrestart do after an update?
needrestart finds running daemons that still use an old copy of an updated library and restarts them, so a patched OpenSSL is really in use without a full reboot. Ubuntu 22.04 and 24.04 include it; on Debian install it with sudo apt install needrestart. It runs automatically after every apt transaction.
The restart mode lives in /etc/needrestart/needrestart.conf or a drop-in file under /etc/needrestart/conf.d/. The letter i asks interactively, l only lists, and a restarts without asking. For unattended servers set automatic mode in a drop-in:
echo "\$nrconf{restart} = 'a';" | sudo tee /etc/needrestart/conf.d/50-auto.conf
sudo needrestart -r l
sudo needrestart -kThe -k check reports whether the running kernel is older than the newest installed one. Services that must never bounce can be excluded with $nrconf{override_rc} entries in the same drop-in.
How do I set up dnf-automatic on Rocky Linux and AlmaLinux?
Install dnf-automatic, edit one file and enable the timer. Rocky Linux 9 and AlmaLinux 9 both publish security advisories in their repository metadata, so a security-only policy works as expected:
sudo dnf install dnf-automatic
sudo vi /etc/dnf/automatic.confIn the [commands] section set:
upgrade_type = security
download_updates = yes
apply_updates = yesThen start the timer and look at what is pending:
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer
sudo dnf updateinfo list --securityThe [emitters] section can write results to the journal (emit_via = stdio) or send mail. Recent dnf builds also accept a reboot = when-needed option; run man dnf-automatic to see whether your version has it. If not, schedule a check with sudo dnf needs-restarting -r, which exits with code 1 when a reboot is required.
How do I test that updates really run?
Run each tool by hand once and read the logs. On Ubuntu and Debian a dry run shows which packages would be chosen and why:
sudo unattended-upgrade --dry-run --debug
sudo tail -n 50 /var/log/unattended-upgrades/unattended-upgrades.log
grep -h " install " /var/log/dpkg.log | tailOn Rocky and Alma start the service directly and follow its journal:
sudo systemctl start dnf-automatic.service
journalctl -u dnf-automatic.service -n 40
sudo dnf history list | headA day later, check again that the timer fired on its own. An empty log after several days usually means the timer is disabled or the origins pattern matches nothing.
What can go wrong and how do I fix it?
Most failures come from a locked package database, a full disk or a held package. If unattended-upgrades reports that dpkg was interrupted, finish the previous run:
sudo dpkg --configure -a
sudo apt -f installWhen /boot or the root disk is full, old kernels pile up; free space with sudo apt autoremove --purge or, on Rocky and Alma, lower installonly_limit in /etc/dnf/dnf.conf. The disk usage guide shows how to find the rest. If an update broke a service, look at journalctl -u name -b, downgrade the one package with apt install package=version or dnf downgrade package, and add it to the blacklist or the exclude= line until a fixed build arrives. Because there is no browser console on our servers, keep at least one way back in: test SSH after every kernel reboot, and keep the console Restart action in mind if a boot hangs.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Automatic security updates
Will automatic updates break my applications?
Security updates within a stable release are built to be compatible, so breakage is rare. Restricting the policy to security fixes, holding back the packages your app depends on and keeping backups covers most of the remaining risk.
Should the server reboot itself?
For web servers and bots a short nightly reboot window is usually fine and keeps the kernel current. For long running jobs, nodes or game servers, leave automatic reboots off and plan them yourself with the reboot guide.
How do I get a mail when updates install?
Set Unattended-Upgrade::Mail on Debian or Ubuntu, or emit_via = email in dnf-automatic. Outgoing mail needs a working relay; port 25 may not be open, so use an authenticated SMTP relay on port 587 or ask support by ticket first.
Does this also update Docker images or language packages?
No. It only covers system packages from apt or dnf. Containers, pip, npm and Composer packages need their own update routine.
How do I check the last time updates ran?
Read /var/log/unattended-upgrades/unattended-upgrades.log on Ubuntu and Debian, or run sudo dnf history list and journalctl -u dnf-automatic on Rocky and Alma.
Is FreeBSD covered?
Not by these tools. FreeBSD uses freebsd-update for the base system and pkg upgrade for packages; see the FreeBSD upgrade guide.
More in Server basics
- Create a sudo user
- Add a swap file
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Run an app as a systemd service
- Schedule jobs with cron and systemd timers
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release