Sub topic 7
Bring the system up to date
A freshly installed server is already out of date. The installation image was built weeks or months ago, and security fixes have shipped since. Updating is usually the first thing you do after logging in, before you install anything else.
This chapter assumes you can log in to the server, have sudo access, and know what a package manager is. It covers Debian/Ubuntu (apt) and the RHEL family (dnf), including the points where updates go wrong.
Why update first
Updating first has three benefits.
- You close known vulnerabilities before the server accepts traffic.
- You start from a known state. Later problems are easier to trace when you know the baseline was current.
- You apply kernel and library updates while a reboot costs nothing, because the server is not yet doing any work.
How package updates work
A package manager keeps a local copy of each repository's metadata. This is the list of available packages and their versions. Updating has two steps:
- Refresh the metadata so the system knows what is available.
- Compare the installed packages with that list and install newer versions.
On Debian and Ubuntu these steps are separate commands. On the RHEL family, dnf refreshes expired metadata automatically, and --refresh forces a refresh.
Run the update
Run the commands for your distribution family.
# Debian/Ubuntu
sudo apt update
sudo apt full-upgrade
# RHEL family
sudo dnf upgrade --refreshOn Debian-based systems, apt update only refreshes metadata. It installs nothing. apt full-upgrade installs the updates, and it may also remove packages when a new version requires it. For example, a kernel update can replace an old dependency.
apt upgrade is the more conservative command. It never removes packages, so it can hold back updates that need a removal or a new dependency. On a new server, full-upgrade is the usual choice because there is little to break.
Review before you confirm
Both tools show a summary and ask for confirmation. Read it, because it tells you what will change. Look for:
- The number of packages to upgrade, install, and remove.
- Whether a kernel package such as
linux-image-*orkernel-coreis included. - Any package that will be removed that you did not expect.
To preview the list without changing anything:
# Debian/Ubuntu
apt list --upgradable
# RHEL family
dnf check-upgradednf check-upgrade exits with code 100 when updates are available and 0 when there are none. Scripts can use this, but do not read it as a failure.
Does the server need a reboot?
Some updates only take effect after a restart. This applies most often to the kernel and to core libraries such as glibc and systemd. A running process keeps using the old library version in memory until it restarts.
On Debian and Ubuntu, a file marks a pending reboot:
if [ -f /var/run/reboot-required ]; then
echo "Reboot required"
cat /var/run/reboot-required.pkgs
fiOn the RHEL family, the needs-restarting tool comes from the dnf-utils package (on RHEL 8 and later it is part of yum-utils/dnf-utils, depending on the release). Install it if it is missing:
sudo dnf install -y dnf-utils
sudo needs-restarting -rWith -r, the command reports whether a full reboot is needed and sets its exit code to 1 if so. Without -r, it lists individual processes that should be restarted.
To compare the running kernel with the newest installed one:
uname -r
# Debian/Ubuntu: installed kernel images
dpkg --list 'linux-image-*' | grep '^ii'
# RHEL family: installed kernels
rpm -q kernel-coreIf the running version is older than the newest installed one, reboot:
sudo rebootAfter the machine returns, log in again and confirm with uname -r.
Common errors and fixes
| Symptom | Likely cause | What to do | Column | Column |
|---|---|---|---|---|
| `Could not get lock /var/lib/dpkg/lock-frontend` | Another `apt` or `dpkg` process is running, often an automatic update that started at first boot. | Wait a few minutes and retry. Check with `ps aux \ | grep -E 'apt\ | dpkg'`. Do not delete the lock file while a process holds it. |
| `Temporary failure resolving 'archive.ubuntu.com'` | DNS or network is not working. | Test with `ping -c 2 1.1.1.1`, then `resolvectl status`. Fix networking first. | ||
| `The following packages have been kept back` | `apt upgrade` cannot apply an update without installing or removing something. | Use `sudo apt full-upgrade`. | ||
| `Failed to download metadata for repo` | The mirror is down or the repository was retired. | Retry later, or change the mirror. Check that the release is still supported. | ||
| `GPG error: ... NO_PUBKEY` or `public key is not installed` | A third-party repository's signing key is missing or expired. | Re-import the key from the vendor, or remove the repository if you do not need it. | ||
| `No space left on device` | The `/boot` or root partition is full, often with old kernels. | Run `df -h`, then `sudo apt autoremove --purge` or `sudo dnf remove --oldinstallonly`. | ||
| Interrupted upgrade (SSH dropped, power loss) | `dpkg` or `dnf` stopped partway. | Debian: `sudo dpkg --configure -a`, then `sudo apt -f install`. RHEL: `sudo dnf history list`, then `sudo dnf-3 check` or `sudo dnf distro-sync`. |
Run long updates in a persistent session
If your SSH connection drops during an upgrade, the package manager can be killed halfway and leave packages unconfigured. On a server that is reachable only over SSH, protect yourself. Use the console, or start tmux before you update:
tmux new -s update
sudo apt full-upgradeIf the connection drops, log in again and run tmux attach -t update.
Configuration prompts during upgrade
Debian-based upgrades sometimes stop and ask whether to keep your version of a configuration file or install the package maintainer's version. On a fresh server you have changed little, so the choice rarely matters. Later, on a configured server, the safe default is to keep your current file. You can then compare it with the new default, which apt saves with a .dpkg-dist suffix.
On Ubuntu Server, a needrestart prompt can also appear. It lists services that use outdated libraries and asks which to restart. Accepting the default list is normally fine. It is a good reason not to run updates over a connection whose service you are about to restart.
Release upgrades are a different task
Updating packages within a release is not the same as moving to a new release. The commands above stay inside the release you installed, for example Ubuntu 24.04 or Rocky Linux 9. Upgrading to the next release, with do-release-upgrade or a dnf system upgrade, is a larger operation with its own risks. Do not do it as part of first-boot setup. If you chose a release that is near its end of support, reinstall with a newer one.
Check what changed
Both package managers keep a history. This helps when you need to explain, or undo, something later.
# Debian/Ubuntu
grep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.log | tail -n 6
# RHEL family
sudo dnf history list
sudo dnf history info lastOn the RHEL family, sudo dnf history undo last can reverse a transaction in many cases. Debian has no direct equivalent, so take a snapshot before major updates when you can. The final chapters cover snapshots of the clean baseline.
A short routine
- Refresh metadata and list what is pending.
- Install the updates in a persistent session.
- Check for a required reboot, and reboot if one is needed.
- After the reboot, confirm the kernel version and run
systemctl --failedto catch units that failed to start.
Exercise
You run sudo apt update on a new Ubuntu server and it finishes without errors. A colleague says the server is now up to date. Is that correct? What would you run next, and how do you find out whether a restart is needed afterward?
Show answer
No. apt update only refreshes the package metadata. It installs nothing. Run sudo apt full-upgrade to install the pending updates. Afterward, check whether /var/run/reboot-required exists, or compare uname -r with the newest installed linux-image-* package. If a reboot is required, run sudo reboot and confirm the new kernel with uname -r.
Exercise
During sudo apt upgrade, the output says "The following packages have been kept back". What does that mean, and how do you resolve it?
Show answer
apt upgrade never removes packages or installs new dependencies. The kept-back packages need one of those actions to upgrade. Review the plan with sudo apt full-upgrade, which is allowed to add and remove packages. Read the list of removals before you confirm.