Skip to contentcloudvix
cloudvix
GuideServers & hosting

Automatic security updates on Ubuntu 24.04, and when to reboot

Denys DunaievskyiFounder, Cloudvix
  • 11 min read
  • Published
  • LevelBeginner
Guides
On this page

Key takeaways

  • By default only packages from Ubuntu’s security archive are installed automatically; ordinary updates are not.
  • A separate file wins over the shipped one, because apt reads /etc/apt/apt.conf.d in name order and the last value wins.
  • The default origins cover Ubuntu’s own packages only.

Security fixes help only once they are installed, and a server that nobody logs in to for months will not install them by itself. Ubuntu can do it for you. This guide switches that on, shows how to read what it did and settles the part that is easy to get wrong: when the server restarts.

Before you start

You need an Ubuntu 24.04 server and a user with sudo, as in the first-hour guide. Nothing here needs a restart to set up. Decide first how you feel about a reboot at night: a server that restarts by itself is safer, but everything that runs on it must then come back by itself too.

  • an Ubuntu 24.04 server and a user with sudo
  • a quiet hour of the day for a possible restart, in the time zone of the server (timedatectl shows it)
  • a list of what must start again after a restart: your containers and your own services

The steps

See what is already set

Many Ubuntu images come with the tool installed, but do not assume it. This prints the two settings that matter; 1 means every day, 0 means off.

On the serverbash
apt-config dump | grep -E 'APT::Periodic::(Update-Package-Lists|Unattended-Upgrade)'

Install it and switch it on

The second command asks one question: answer Yes. It writes /etc/apt/apt.conf.d/20auto-upgrades. Expect two lines in that file, both with the value 1.

On the serverbash
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgrades

Know what it installs

By default only packages from Ubuntu’s security archive are installed automatically; ordinary updates are not. That keeps the chance of an update breaking your app small. Lines that start with // are switched off: expect the -security line to be active and the -updates line to be commented out.

On the serverbash
grep -n -A10 'Allowed-Origins' /etc/apt/apt.conf.d/50unattended-upgrades

Run it once by hand

The dry run changes nothing. It prints which origins it allows and which packages it would install. The answer “No packages found that can be upgraded unattended” is normal on a server that is up to date.

On the serverbash
sudo unattended-upgrade --dry-run --debug

Check that it is scheduled

There are two timers: one refreshes the package list, the other installs. Each shows when it ran last and when it runs next. They run with a random delay on purpose, so do not expect an exact time.

On the serverbash
systemctl list-timers apt-daily.timer apt-daily-upgrade.timer

Read what it did

The log lists every run, the packages it installed and any error. The second command shows what is still waiting: those are updates outside the security archive, and nothing installs them for you.

On the serverbash
sudo tail -n 40 /var/log/unattended-upgrades/unattended-upgrades.log
apt list --upgradable 2>/dev/null

Find out whether a restart is waiting

After a kernel or library update the new code only runs after a restart. The first line shows the marker file that apt leaves behind. needrestart goes further: it also lists services that still run old library code. In its machine-readable output, NEEDRESTART-KSTA=1 means the running kernel is current, and NEEDRESTART-SVC lines name services to restart.

On the serverbash
cat /var/run/reboot-required 2>/dev/null || echo "no restart waiting"
uname -r
sudo apt install -y needrestart
sudo needrestart -b

Choose: restart by hand or automatically

By hand means you look for the marker file now and then and run sudo reboot at a time that suits you. Automatically means unattended-upgrades restarts the server only when a restart is needed, at the time you set, and not while someone is logged in. A separate file wins over the shipped one, because apt reads /etc/apt/apt.conf.d in name order and the last value wins.

On the server · /etc/apt/apt.conf.d/52reboot-policybash
sudo tee /etc/apt/apt.conf.d/52reboot-policy > /dev/null <<'EOF'
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
Unattended-Upgrade::Automatic-Reboot-WithUsers "false";
EOF
apt-config dump | grep 'Unattended-Upgrade::Automatic-Reboot'
Warning

A reboot stops everything on the server for a minute or two. Switch the automatic reboot on only after the next step shows that your services come back.

Make sure everything comes back

Restart once at a time you choose and look at the result. Services that systemd starts at boot return by themselves; Docker containers need a restart policy such as unless-stopped, which the Docker guide sets. The second block runs after you log in again: it shows how long the server has been up, which units failed and which containers run.

On the serverbash
sudo reboot
On the serverbash
uptime -p
systemctl --failed
sudo docker ps

Update what the automatic updates do not cover

The default origins cover Ubuntu’s own packages only. Docker Engine from Docker’s repository, or anything from another repository, is not updated automatically. Look at the list now and then and update on purpose.

On the serverbash
sudo apt update
apt list --upgradable 2>/dev/null
sudo apt upgrade
Warning

Upgrading Docker Engine can restart your containers. Do it at a quiet time.

Check the result

Look for three things. Both Periodic settings are 1, the timers show a next run, and the marker file is gone after a restart. If you chose an automatic reboot, look at the reboot records the next morning: the second command lists the last three.

On the serverbash
apt-config dump | grep -E 'Periodic::Unattended-Upgrade|Automatic-Reboot'
last -x reboot | head -n 3

If something goes wrong

Could not get lock /var/lib/dpkg/lock-frontend

Another apt process is running, often unattended-upgrades itself. Wait a few minutes and try again. Do not delete the lock files.

The server restarted and my app is down

The app was not set to start by itself. For Docker use --restart unless-stopped, or restart: unless-stopped in a compose file; for your own program use an enabled systemd service. Set Automatic-Reboot to false in the policy file until this works.

The dry run lists packages but nothing gets installed

Read the log. A package can be held back, or the timer has not run yet: it runs once a day with a random delay.

A package was never updated

It probably comes from a repository outside the allowed origins, such as Docker’s. The last step shows how to update those on purpose.

Where to go next

Updates and restarts are one of twelve checks in the server security checklist. Links to it and to the guides this one builds on are at the end of this page.

About the author

Denys Dunaievskyi

Founder, Cloudvix

Runs Cloudvix and is responsible for the guides published here.

Related guides

Your server is a few clicks away.

Choose the plan and the system in one order form; the location too, when there is more than one.

Create server