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

URL: https://levelrail.com/learn/security/unattended-security-updates-and-reboots/
Updated: 2026-11-27

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](https://levelrail.com/learn/server-basics/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`:

```text
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](https://levelrail.com/learn/docker/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:

```text
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](https://levelrail.com/learn/docker/docker-restart-policies/)), 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](https://levelrail.com/learn/deploy-operate/monitor-uptime-simply/)) 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](https://levelrail.com/learn/security/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](https://levelrail.com/learn/security/scan-docker-images-with-trivy/)).

## Troubleshooting

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `E: Could not get lock /var/lib/dpkg/lock-frontend` | Another apt run holds the lock | Wait for it to finish, then retry |
| No log files appear | Unattended run is not enabled | Check `20auto-upgrades` and the timers |
| `No packages found that can be upgraded unattended` | Nothing eligible, or origins too narrow | Normal if up to date, otherwise review Allowed-Origins |
| Reboot did not happen | Automatic-Reboot is false or no flag file | Check the setting and `/var/run/reboot-required` |
| Server rebooted at a bad time | Time zone differs from expectation | Check `timedatectl`, change the time |
| `dpkg was interrupted, you must manually run 'dpkg --configure -a'` | A run was cut off | Run `sudo dpkg --configure -a` |
| Updates held back | Package in blacklist or phased update | Read 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/
```

## Related lessons

- [Keep a server updated](https://levelrail.com/learn/server-basics/keep-a-server-updated/)): manual updates with apt.
- [Package management with apt](https://levelrail.com/learn/linux/package-management-with-apt/)): how apt sources and holds work.
- [Docker restart policies](https://levelrail.com/learn/docker/docker-restart-policies/)): make containers survive a reboot.
- [Scan Docker images with Trivy](https://levelrail.com/learn/security/scan-docker-images-with-trivy/)): patch the software inside containers too.
- [Audit a Linux server](https://levelrail.com/learn/security/audit-a-linux-server/)): confirm the server state after updates.

## Next

Continue with [Manage secrets on servers and containers](https://levelrail.com/learn/security/manage-secrets-on-servers-and-containers/)).

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

- [Ubuntu Server docs, automatic updates](https://ubuntu.com/server/docs/automatic-updates)
- [unattended-upgrade man page](https://manpages.ubuntu.com/manpages/unattended-upgrade.8.html)
