Chapter 1 · Lesson 5 of 19
Server basics

Keep an Ubuntu server updated automatically

Unattended-upgrades installs security updates on Ubuntu automatically, so known vulnerabilities do not wait for you to log in.

By the end you will

Update an Ubuntu server and turn on automatic security updates.

Time
12 min
Level
Beginner
Check
4 questions
You need
  • An Ubuntu server with sudo access

Software has bugs, and some of them are security holes that attackers scan the internet for. When a fix is published, the clock starts: servers that are not updated stay open. A server you set up once and forgot is the typical target. Automatic security updates close most of that gap without you remembering to log in.

How it works#

Ubuntu installs software as packages (managed with apt, see essential Linux commands) from repositories, lists of packages kept by Ubuntu. apt update downloads the current lists from those repositories. It does not install anything. apt upgrade then compares what you have against the lists and installs newer versions.

Updates come in channels. The security channel carries fixes for known vulnerabilities and is meant to change as little as possible. Other channels carry general bug fixes. Unattended-upgrades is a small program run by a systemd timer, usually daily, and by default installs from the security channel only.

Some updates need a restart to take effect. A new kernel does not run until the machine reboots. A new library is only used by programs that start after the update, so a long-running service may keep the old copy in memory. When an update needs a reboot, Ubuntu creates /var/run/reboot-required. Unattended-upgrades does not reboot by default, so that file is your signal.

A worked example: on Monday a flaw in a library is announced. Ubuntu publishes a fixed package to the security channel. On Tuesday morning the unattended-upgrades timer runs, sees the new version, downloads it and installs it. The package manager replaces the library file. A program that is already running still holds the old copy in memory, so the fix applies the next time it starts. If the update was for the kernel, /var/run/reboot-required appears and the new kernel runs only after the reboot.

Step 1: Update now#

bash
sudo apt update
sudo apt upgrade

apt update refreshes the lists and apt upgrade installs newer versions. You are asked to confirm with Y.

What you should see: after apt update, a line near the end saying how many packages can be upgraded, or that all packages are up to date. After apt upgrade, a list of packages and a progress display. If a purple or blue screen asks which services to restart, accept the defaults.

Step 2: Turn on automatic security updates#

bash
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

Answer Yes when asked whether to download and install stable updates automatically. The first command installs the program, which is already present on many Ubuntu images. The second writes the setting that turns the timer on.

What you should see: a text dialog with the question and Yes or No options.

Step 3: Confirm the service is running#

bash
systemctl status unattended-upgrades

What you should see: the service as active. Logs are in /var/log/unattended-upgrades/. For a test run that changes nothing, use:

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

What you should see: debug lines ending with the list of allowed origins and either packages that would be upgraded or a note that nothing needs upgrading.

Step 4: Notice a pending reboot#

bash
test -f /var/run/reboot-required && echo "reboot needed"

If it prints the message, reboot at a quiet moment with sudo reboot. To see which packages asked for it, read /var/run/reboot-required.pkgs if that file exists.

Check it worked#

Run apt list --upgradable. After a full upgrade it should list no packages, or only ones held back on purpose. Then look at /var/log/unattended-upgrades/unattended-upgrades.log after a day: you should see entries from the daily run, even if the line only says no packages were found.

Edge cases and gotchas#

  • apt can be locked by the automatic run. If apt complains it cannot get a lock, the updater is working. Wait and retry. Do not delete lock files by hand.
  • A reboot is not automatic. Unattended-upgrades can be set to reboot, but by default it is off. A server may run an old kernel for weeks until you restart it. Check for /var/run/reboot-required on a schedule.
  • Rebooting restarts everything. Containers without a restart policy stay down, and services not enabled at boot do not come back. Before you reboot, confirm your apps start on boot.
  • Third party repositories are not covered by default. Docker’s apt repository, for example, is not in the security allowed origins. Update those packages yourself.
  • Disk space can block updates. A full /boot or root disk makes apt fail. Check df -h if an upgrade stops half way.
  • Holding a package back is an option, not a habit. sudo apt-mark hold name keeps one version, but it also keeps its vulnerabilities. Remove the hold when you can.
  • Docker containers are not updated by apt. Pull a newer image and recreate the container, as covered in update a container to a new image.
  • Long running services keep old libraries. After an update, needrestart or a reboot is what makes processes use the new files. A reboot is the simple answer.

Troubleshooting#

SymptomLikely causeFix
Could not get lock /var/lib/dpkg/lock-frontendAnother apt process is runningWait a few minutes and retry
E: Unable to locate package unattended-upgradesThe package lists are staleRun sudo apt update first
Temporary failure resolving during updateDNS or network problemCheck ping 1.1.1.1 and the resolver settings
The following packages have been kept backA change needs new dependenciesRun sudo apt full-upgrade after reading what it will do
Service is inactive (dead)The timer or service is offRun sudo dpkg-reconfigure --priority=low unattended-upgrades again
No space left on device while upgradingDisk is fullFree space with sudo apt autoremove and see the disk usage lesson
E: Release file ... is not valid yetThe server clock is wrongFix the time with timedatectl and run apt update again
dpkg was interrupted, you must manually run dpkg --configure -aA previous upgrade stopped half wayRun sudo dpkg --configure -a and retry

Real-world scenario#

Leo runs a small site on a server he set up a year ago and has not touched since. A friend mentions a recent security fix, so he logs in and runs sudo apt update. Dozens of packages are waiting, including a kernel.

He takes a provider snapshot, runs sudo apt upgrade, sees /var/run/reboot-required, and reboots at night. The site comes back after a minute because the app is a systemd service that starts at boot. Then he turns on unattended-upgrades and adds a calendar reminder to check for a pending reboot on the first of each month.

The year of missed updates was the real risk. The routine afterwards takes minutes.

Quick reference#

TaskCommand
Refresh package listssudo apt update
Install updatessudo apt upgrade
List what is waitingapt list --upgradable
Turn on auto updatessudo dpkg-reconfigure --priority=low unattended-upgrades
Test the jobsudo unattended-upgrade --dry-run --debug
Reboot needed?test -f /var/run/reboot-required && echo yes

Security and safety notes#

  • Do not interrupt apt upgrade while it is writing packages. A half finished upgrade can leave the system in a broken state.
  • Take a backup or provider snapshot before a big upgrade on a server that matters.
  • Rebooting briefly takes your apps offline, so choose a quiet moment.

Next#

Linux file permissions explained.

Check what you learned4 questions
1What does unattended-upgrades do?
2How can you tell a reboot is pending?
3Which pair of commands updates packages by hand?
4A container runs an old library. Will sudo apt upgrade on the host fix it?

Frequently asked questions

Will automatic updates break my apps?

Security updates are small and rarely change behaviour, but a kernel or library update can need a reboot. Check for a pending reboot now and then.

Do automatic updates also upgrade Ubuntu to a new release?

No. Unattended-upgrades applies updates within your current release. Moving to a new Ubuntu release is a separate, deliberate step.

Why does apt say it cannot get the lock?

Another apt or dpkg process is running, often the automatic updater. Wait a minute and try again before you do anything drastic.

Do Docker images get these updates too?

No. Host updates cover the server's own packages. A container carries its own packages, so rebuild or pull a newer image to update what is inside.

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