Sub topic 6
Applying updates: upgrade, full-upgrade and dnf upgrade
This chapter assumes you can use a shell and sudo, know what a package repository is, and have already refreshed metadata and reviewed pending updates, as covered in the earlier chapters. You will learn what each apply command does, what it refuses to do, and what to check when it stops partway.
What "apply" means on each system
Applying updates means asking the package manager to compute a transaction and then run it. A transaction is the set of packages to install, upgrade or remove, worked out from the updates in the repository metadata and the dependencies of the packages you already have. The commands differ mainly in which kinds of change they allow in that transaction.
| Command | Upgrades installed packages | Installs new packages | Removes packages |
|---|---|---|---|
| `apt upgrade` | Yes | Only as new dependencies | Never |
| `apt-get upgrade` | Yes | Never | Never |
| `apt full-upgrade` | Yes | Yes | Yes, when needed to resolve |
| `apt-get dist-upgrade` | Yes | Yes | Yes, when needed to resolve |
| `dnf upgrade` | Yes | Yes, as dependencies | Yes, when a package is obsoleted or replaced |
apt-get dist-upgrade does the same job as apt full-upgrade. Despite the name, neither command moves you to a new distribution release. That is a separate project, covered in a later chapter.
Debian and Ubuntu: upgrade
A normal run has three steps: refresh, look, apply.
sudo apt update
apt list --upgradable
sudo apt upgradeapt upgrade shows a summary and asks for confirmation. Read four lines of it:
- "The following packages will be upgraded" is the expected list.
- "The following NEW packages will be installed" lists dependencies that the upgrade pulled in.
- "The following packages have been kept back" lists packages that were not upgraded.
- The final count line reads like "12 upgraded, 1 newly installed, 0 to remove and 3 not upgraded". Check that it says 0 to remove.
Why packages are kept back
A package is kept back when upgrading it would require something upgrade is not allowed to do. There are three common causes:
- The new version needs a package removed, for example because two packages now conflict.
- The new version depends on a package that is not installed. This matters only for
apt-get upgrade, which never installs new packages. Plainapt upgradeinstalls the new dependency. - On Ubuntu, the update is in a phased rollout. Ubuntu releases some updates to a growing percentage of machines over several days, and a machine that is not yet in the percentage sees the package as kept back. Waiting is a valid answer here.
Kernel updates show the difference between the two tools. A kernel update usually arrives as a new package with a new version in its name, such as linux-image-6.8.0-45-generic. A metapackage such as linux-image-generic then depends on it. apt upgrade installs the new kernel package as a new dependency. apt-get upgrade keeps the metapackage back, so the kernel never changes. Scripts that use apt-get upgrade can silently skip kernel updates this way.
To resolve a kept-back package, first find out why:
apt list --upgradable
sudo apt -s full-upgradeThe -s flag simulates the run and changes nothing. If the simulation shows only the removals you expect, run sudo apt full-upgrade. To upgrade one package without touching others, use sudo apt install --only-upgrade nginx.
When full-upgrade is the right tool
Use full-upgrade when the simulation shows that a removal is the intended result of the update. A common case is a library package renamed after a soname change, where the old package is replaced by a new one. Do not use it by habit on production servers. Always read the "to remove" list first, because a removal can take a service package with it.
RHEL family: dnf upgrade
dnf check-update
echo "exit code: $?"
sudo dnf upgradednf check-update exits with 100 when updates are available and 0 when there are none. Any other code is an error, such as a repository that could not be reached. A script that tests only for "non-zero" will misread the 100.
dnf upgrade has no kept-back state. It resolves the whole transaction in one pass. If a package is obsoleted by a new one, dnf installs the new package and removes the old one in the same transaction, so dnf upgrade combines what apt upgrade and apt full-upgrade do. This makes the confirmation screen important. It lists "Installing", "Upgrading", "Removing" and "Replacing" sections, and you should read the Removing section on every run.
By default dnf also insists on the best available version. If the newest version of a package cannot be installed because of a dependency problem, dnf stops with an error instead of quietly choosing an older version. The error text names the conflicting packages, which is the information you need.
To preview a run without applying it, answer no automatically:
sudo dnf upgrade --assumenoThis prints the full transaction and exits without changing anything. To limit the change, name the package or use --exclude:
sudo dnf upgrade nginx
sudo dnf upgrade --exclude='kernel*'Excluding on the command line is a one-time measure. For a lasting restriction, use a version lock, which a later chapter covers.
Choosing a command
Mermaid diagrams must be in a diagram fence, so here is the decision flow in the required form.
Configuration file prompts
Updates sometimes ship a new version of a file in /etc. If you changed that file, the package manager must choose between your version and the maintainer's version.
On Debian and Ubuntu, dpkg stops and asks. The options are to keep your version, install the maintainer's version, or show the differences. Keeping yours is the safe default, but the service may then miss new settings. After the run, look for leftovers:
sudo find /etc -name '*.dpkg-dist' -o -name '*.dpkg-new' -o -name '*.dpkg-old'On RHEL-family systems, rpm does not ask. If you edited a config file, rpm keeps your file and writes the new default beside it as .rpmnew. In some cases it saves your old file as .rpmsave and installs the new one. Check for both:
sudo find /etc -name '*.rpmnew' -o -name '*.rpmsave'Compare each pair with diff and merge any new settings you need.
For unattended runs on Debian, you can set the choice in advance. This keeps your files and never prompts:
sudo DEBIAN_FRONTEND=noninteractive apt-get -y \
-o Dpkg::Options::="--force-confdef" \
-o Dpkg::Options::="--force-confold" upgrade--force-confdef takes the default when there is one, and --force-confold keeps your version otherwise. Note that this uses apt-get upgrade, so it keeps kernels back, as described above. Prefer apt-get -y dist-upgrade or apt -y upgrade if kernels should move.
Lock errors and interrupted runs
Only one package operation can run at a time. If you see Could not get lock /var/lib/dpkg/lock-frontend, another process holds it. It is often unattended-upgrades or a background refresh. Check what it is before doing anything else:
ps aux | grep -E 'apt|dpkg|unattended' | grep -v grepWait for it to finish. Do not delete lock files. On dnf you will see "Waiting for process with pid N to release the lock", and dnf waits for you.
If a run was cut off, for example by a dropped SSH session, dpkg can be left half-configured. Run these two commands in order:
sudo dpkg --configure -a
sudo apt -f installRun package upgrades inside tmux or screen on remote hosts so a lost connection does not stop the transaction partway. On a dnf system, a cut-off run is checked with sudo dnf history list, which shows whether the last transaction finished.
After the run
A successful transaction does not mean the system is fully patched. Running processes keep using the old library versions in memory until they restart, and a new kernel does not take effect until reboot. The next chapter covers that decision in detail. For now, always capture what changed:
grep -E 'Start-Date|Commandline|Upgrade:' /var/log/apt/history.log | tail -n 5
sudo dnf history info lastSummary of habits
- Simulate before applying:
apt -s full-upgradeordnf upgrade --assumeno. - Read the removal list every time.
- Treat kept-back packages as a question to answer, not noise to ignore.
- Know which tool your scripts call, since
apt-get upgradeandapt upgradediffer. - Check for leftover config files after each run.
Exercise
A script on an Ubuntu server runs apt-get -y upgrade weekly. The server has not moved to a newer kernel in months, and the output shows linux-image-generic among the kept-back packages. What is happening, and what would you change?
Show answer
apt-get upgrade never installs new packages. A new kernel arrives as a new package that the linux-image-generic metapackage must depend on, so the metapackage is kept back and the kernel never updates. Change the script to apt-get -y dist-upgrade or apt -y full-upgrade, or use plain apt -y upgrade, which installs new dependencies. Simulate with apt -s full-upgrade first and confirm that the removal list is acceptable. A reboot is still needed afterward to run the new kernel.
Exercise
On a RHEL-family server, dnf check-update prints a list of packages and your script's set -e aborts. Why, and how should the script treat the result?
Show answer
dnf check-update exits with 100 when updates are available, and set -e treats any non-zero code as failure. The script should accept 100 as "updates available" and 0 as "none", and treat any other code as an error. For example: dnf check-update; rc=$?; [ "$rc" -eq 0 ] || [ "$rc" -eq 100 ] || exit "$rc".
Exercise
You run sudo apt upgrade and the summary reads "0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded". Name two possible causes and one command that helps tell them apart.
Show answer
Possible causes are that the updates need a removal that upgrade will not perform, or that Ubuntu is phasing the updates and this machine is not yet included. Run sudo apt -s full-upgrade to simulate. If it shows removals, the first cause applies. If it shows no removals and still holds the packages, or apt-cache policy PACKAGE mentions a phased update percentage, the second cause applies and waiting is reasonable.