Chapter 10 · Lesson 4 of 10
Server security

Unattended security updates and reboots on Ubuntu

Unattended-upgrades installs security updates automatically on a schedule and can reboot the server at a set time when a kernel or core library needs it.

By the end you will

Turn on automatic security updates, choose a reboot time, test the setup with a dry run and read the update logs.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • Ubuntu or Debian server with sudo

Security fixes are only useful once they are installed. On a server nobody logs into every day, the practical way to stay patched is unattended-upgrades: it checks daily, installs security updates, and can reboot the machine at a time you choose when the kernel or a core library changed. This lesson builds on keep a server updated.

How it works#

Two files in /etc/apt/apt.conf.d/ drive it:

  • 20auto-upgrades is the on switch. It sets how often apt refreshes package lists and runs the unattended job, in days.
  • 50unattended-upgrades says what may be installed (the allowed origins, normally the security pocket of your release), which packages to skip, and what to do about reboots and mail.

A systemd timer (apt-daily and apt-daily-upgrade) runs the job daily. The run installs eligible updates and writes logs under /var/log/unattended-upgrades/. When a package needs a restart to take effect, it creates /var/run/reboot-required. Unattended-upgrades will leave the machine running with the old kernel until someone reboots, unless you turn on automatic reboot.

Step 1: Install and enable#

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

The dialog asks if stable updates should be installed automatically. Answer Yes. This writes /etc/apt/apt.conf.d/20auto-upgrades:

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

Each 1 means once a day. Many Ubuntu server images already ship with this on, so look at the file before changing it.

Step 2: Review what is allowed#

Open the main file:

bash
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

Find Unattended-Upgrade::Allowed-Origins. The security line is enabled by default and ordinary update lines are commented out with //. Keep it that way if you want the lowest risk: security fixes only, no surprise feature upgrades. If you run a service that must not restart unexpectedly, add its package to Unattended-Upgrade::Package-Blacklist and update it by hand.

Docker or other third-party repositories are not included unless you add their origin. That is usually what you want, since you should update containers deliberately. See update a container to a new image.

Step 3: Decide on automatic reboot#

Without a reboot, a kernel fix is installed but not running. Two lines in the same file control this:

output
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";

The server will reboot at 04:00 server time, only when /var/run/reboot-required exists. Choose your quietest time. Before enabling, confirm every service comes back by itself: systemd units are enabled and containers have a restart policy, so a reboot does not leave you down. If you cannot accept any downtime, leave it off and reboot by hand in a window, using monitoring to notice a failed start.

There is also Unattended-Upgrade::Automatic-Reboot-WithUsers, which controls whether it reboots when someone is logged in. Read the comment above it in the file.

Step 4: Dry run and force a run#

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

This prints the allowed origins, the packages it would upgrade and the ones it skips, without changing anything. To run it for real now rather than wait for the timer:

bash
sudo unattended-upgrade -d

Check that the timers are active too:

bash
systemctl list-timers 'apt-daily*'

Step 5: Read the logs#

bash
ls /var/log/unattended-upgrades/
sudo less /var/log/unattended-upgrades/unattended-upgrades.log

Look for lines such as Packages that will be upgraded: and All upgrades installed. The -dpkg.log file holds the package manager output, which is what you need when an upgrade fails. Make a habit of reading it after the first week.

Check it worked#

bash
cat /etc/apt/apt.conf.d/20auto-upgrades
systemctl list-timers 'apt-daily*'
sudo unattended-upgrade --dry-run --debug
ls /var/run/reboot-required 2>/dev/null && cat /var/run/reboot-required.pkgs

The timers should show a next run time. If the reboot flag exists, the .pkgs file names the packages that caused it. To see which kernel is running, use uname -r. For a wider system review see audit a Linux server.

Edge cases and gotchas#

  • Held by a lock. If another apt process runs, the job can fail with a lock error. It retries on the next run.
  • Services restart. Updating a library can restart services that use it, which is a brief interruption. Use needrestart output in the log to see what was restarted.
  • Kernel not running. Security fixed in the installed kernel does nothing until the reboot. A long uptime with a pending flag means you are exposed.
  • Time zone. Automatic-Reboot-Time uses the server’s local time zone, which is often UTC. Check with timedatectl.
  • Release upgrades. Unattended-upgrades does not move you to a new Ubuntu release. That is a separate, manual job.
  • Containers are not covered. Packages inside images are not updated by the host. Rebuild images regularly and scan them.

Troubleshooting#

SymptomLikely causeFix
E: Could not get lock /var/lib/dpkg/lock-frontendAnother apt run holds the lockWait for it to finish, then retry
No log files appearUnattended run is not enabledCheck 20auto-upgrades and the timers
No packages found that can be upgraded unattendedNothing eligible, or origins too narrowNormal if up to date, otherwise review Allowed-Origins
Reboot did not happenAutomatic-Reboot is false or no flag fileCheck the setting and /var/run/reboot-required
Server rebooted at a bad timeTime zone differs from expectationCheck timedatectl, change the time
dpkg was interrupted, you must manually run 'dpkg --configure -a'A run was cut offRun sudo dpkg --configure -a
Updates held backPackage in blacklist or phased updateRead the --dry-run --debug output

Quick reference#

bash
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgrades
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
sudo unattended-upgrade --dry-run --debug
ls /var/run/reboot-required
ls /var/log/unattended-upgrades/

Next#

Continue with Manage secrets on servers and containers.

Check what you learned4 questions
1Which file turns the daily automatic run on or off?
2What does the file /var/run/reboot-required mean?
3How do you rehearse a run without changing the system?
4What is a sensible time for Automatic-Reboot-Time?

Frequently asked questions

Is it safe to enable unattended-upgrades?

By default it installs only security updates from the official security origin, which is the low-risk choice. Services may restart during an update, so watch the first few runs on a server with important workloads.

How do I know if my server needs a reboot?

Check whether /var/run/reboot-required exists. Kernel and core library updates create it, and the file /var/run/reboot-required.pkgs lists the packages that caused it.

Where are unattended-upgrades logs?

In /var/log/unattended-upgrades/. The file unattended-upgrades.log lists what was installed, and unattended-upgrades-dpkg.log has the package manager output.

How do I test unattended-upgrades without installing anything?

Run sudo unattended-upgrade --dry-run --debug. It shows which packages would be upgraded and why others were held back.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios