Exclusive Metin2 game shield on FreeBSD: Automatic siege mode, VIP list, and whitelist from your dashboard. See how it works
Create a server

Guide

How to Enable Automatic Security Updates in Ubuntu

Many compromised servers aren't taken down by unknown vulnerabilities, but by patches that had been available for weeks and that no one installed. Ubuntu and Debian include unattended-upgrades, which installs security updates for you every day. When configured properly, it saves you that work without giving you any surprises.

What Gets Updated and What Doesn't

In Ubuntu, by default, it installs packages from the security repository (-security), the base repository for your version, and—if enabled—ESM. Regular updates -updates and distribution version upgrades are excluded. This strikes the right balance for a production server: it fixes vulnerabilities without changing the behavior of your services.

1. Install and activate it

sudo apt updatesudo apt install -y unattended-upgrades apt-listchangessudo dpkg-reconfigure -plow unattended-upgrades

Select Yes. /etc/apt/apt.conf.d/20auto-upgrades will be created with the following content:

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

1 means once a day. It is executed by the systemd timers apt-daily and apt-daily-upgrade, at a time with a slight random margin to avoid overloading the repositories:

systemctl list-timers 'apt-daily*'

2. Save your settings in a separate file

Do not edit 50unattended-upgrades: when the package is updated, you will be prompted to choose between your version and the new one. Save your settings in a separate file, which will be read later:

sudo tee /etc/apt/apt.conf.d/52unattended-upgrades-local > /dev/null <<'EOF'Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";Unattended-Upgrade::Remove-Unused-Dependencies "true";Unattended-Upgrade::Automatic-Reboot "false";Unattended-Upgrade::Automatic-Reboot-Time "04:30";Unattended-Upgrade::Mail "[email protected]";Unattended-Upgrade::MailReport "only-on-error";EOF
  • Automatic Reboot: Some patches, such as those for the kernel or the C library, are only applied after a reboot. Start with "false" and check for yourself if a reboot is pending (step 4). If you change it to "true", once an update that requires a reboot is complete, the reboot will be scheduled for the time specified in Automatic-Reboot-Time; if that time has already passed, it will occur at the same time the following day.
  • Email: You need the VPS to be able to send email, for example using Postfix or an SMTP relay. Without that, notifications won't be sent.
  • Remove-Unused-Kernel-Packages: prevents /boot from filling up with old kernels.

3. Exclude whatever you'd prefer to update manually

If a package restarts a critical service when it is updated and you don't want that to happen without you being present (the production database, for example), add it to the list of exclusions in the same file:

Unattended-Upgrade::Package-Blacklist {    "mariadb-server";};

Keep in mind that those packages won't receive patches until you update them yourself. Check the list every few weeks.

4. Check to see if it works

Run a simulation that shows what would be installed without changing anything:

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

The next day, check what has actually been installed:

sudo tail -n 50 /var/log/unattended-upgrades/unattended-upgrades.log

And check to see if there's a pending restart:

cat /var/run/reboot-required 2>/dev/null || echo 'No reboot needed'

Automatic or manual restart?

Enable this on websites and services that restart automatically and can tolerate a one-minute outage in the early morning. On a game server with players in different time zones or on a non-replicated database, a scheduled restart can do more harm than good: leave Automatic-Reboot in "false", let the patches install, and schedule the restart yourself during a maintenance window. The needrestart package tells you which services are still using outdated libraries after each update.

ext step

Automatic updates cover security patches, not version upgrades. When your Ubuntu is nearing the end of its support period, plan to upgrade to the next LTS version or deploy a new Cloud VPS with the latest version and migrate your services.

Related guides