# 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.

URL: https://levelrail.com/learn/server-basics/keep-a-server-updated/
Updated: 2026-11-27

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](https://levelrail.com/learn/server-basics/essential-linux-commands-for-servers/))) 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](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/docker/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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `Could not get lock /var/lib/dpkg/lock-frontend` | Another apt process is running | Wait a few minutes and retry |
| `E: Unable to locate package unattended-upgrades` | The package lists are stale | Run `sudo apt update` first |
| `Temporary failure resolving` during update | DNS or network problem | Check `ping 1.1.1.1` and the resolver settings |
| `The following packages have been kept back` | A change needs new dependencies | Run `sudo apt full-upgrade` after reading what it will do |
| Service is `inactive (dead)` | The timer or service is off | Run `sudo dpkg-reconfigure --priority=low unattended-upgrades` again |
| No space left on device while upgrading | Disk is full | Free space with `sudo apt autoremove` and see the disk usage lesson |
| `E: Release file ... is not valid yet` | The server clock is wrong | Fix the time with `timedatectl` and run `apt update` again |
| `dpkg was interrupted, you must manually run dpkg --configure -a` | A previous upgrade stopped half way | Run `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](https://levelrail.com/learn/server-basics/run-a-service-with-systemd/)) 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

| Task | Command |
| --- | --- |
| Refresh package lists | `sudo apt update` |
| Install updates | `sudo apt upgrade` |
| List what is waiting | `apt list --upgradable` |
| Turn on auto updates | `sudo dpkg-reconfigure --priority=low unattended-upgrades` |
| Test the job | `sudo 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.

## Related lessons

- [Initial server setup checklist](https://levelrail.com/learn/server-basics/initial-server-setup-checklist/)): updates in the first hour
- [Update a container to a new image](https://levelrail.com/learn/docker/update-a-container-to-a-new-image/)): the container side
- [Run a service with systemd](https://levelrail.com/learn/server-basics/run-a-service-with-systemd/)): make apps return after a reboot
- [Backups and the 3-2-1 rule](https://levelrail.com/learn/deploy-operate/backups-and-the-3-2-1-rule/)): back up before big upgrades

## Next

[Linux file permissions explained](https://levelrail.com/learn/server-basics/linux-file-permissions-explained/)).

## 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.
