Sub topic 9
Holding packages with apt-mark and dnf versionlock
This chapter assumes you can run apt or dnf with sudo, and that you have read the earlier chapters on reviewing and applying updates. You should know what "kept back" and "pending update" mean. You need a Debian/Ubuntu or RHEL-family test machine.
Why you would hold a package
Most of the time you want every update. Sometimes a newer version of one package is a problem:
- A new release of a database or web server changes configuration behavior, and you have not tested it yet.
- An application vendor only supports a specific library version.
- A known regression affects your workload, and the fix has not shipped.
A hold (apt) or version lock (dnf) tells the package manager to leave one package alone while everything else keeps patching. It is a temporary measure. A held package stops receiving security fixes, so every hold needs an owner, a reason and an end date.
What a hold actually does
A hold pins the currently installed version. It does not choose a version for you. If the package is at 1.24.0 when you hold it, it stays at 1.24.0. To get a different version, install that version first and hold it afterwards.
| Row | apt (Debian/Ubuntu) | dnf (RHEL family) |
|---|---|---|
| Mechanism | dpkg selection state `hold` | `versionlock` plugin and a lock list |
| Where it is stored | dpkg status database | `/etc/dnf/plugins/versionlock.list` |
| Set | `apt-mark hold PKG` | `dnf versionlock add PKG` |
| List | `apt-mark showhold` | `dnf versionlock list` |
| Release | `apt-mark unhold PKG` | `dnf versionlock delete PKG` |
| Needs extra package | No | `python3-dnf-plugin-versionlock` on RHEL 8 and 9 |
Holding with apt-mark
apt-mark is part of the base apt tooling, so nothing needs installing.
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginxWhile nginx is held, apt upgrade and apt full-upgrade do not upgrade it. They also do not remove it. If another pending upgrade needs a newer nginx, apt cannot satisfy it and reports that package as "kept back" instead.
The same state is visible through dpkg:
dpkg --get-selections | grep holdHeld packages show hold in the second column. Other packages show install.
Installing over a hold
A hold also guards against explicit changes. If you run apt install against a held package in a way that would change it, apt stops and asks you to pass --allow-change-held-packages. This is intentional. Use that flag only when you mean to move the held package.
sudo apt install --allow-change-held-packages nginxHolds and unattended-upgrades
unattended-upgrades runs apt's own logic, so it respects holds. This is why a hold is an effective way to keep one package out of automatic runs. It also means a forgotten hold quietly excludes that package from automation indefinitely.
Holding a kernel
Kernel package names are a common trap. On Ubuntu, linux-image-generic is a metapackage that depends on the newest versioned image. Holding the metapackage does not stop a new linux-image-6.x.y-generic from being installed as a dependency of something else. To freeze the kernel, you must hold the versioned image and headers you actually run:
uname -r
sudo apt-mark hold linux-image-$(uname -r) linux-headers-$(uname -r)Check with apt-mark showhold afterwards. Also hold the metapackage if you do not want it to pull in newer kernels on its next upgrade. Holding the kernel for long leaves you exposed to kernel vulnerabilities, so treat it as short-term only.
Locking with dnf versionlock
On RHEL 8 and 9 and compatible distributions, install the plugin first. It is not part of the default install.
sudo dnf install python3-dnf-plugin-versionlock
sudo dnf versionlock add nginx
dnf versionlock list
sudo dnf versionlock delete nginxversionlock add nginx records the installed version of nginx as a full name, for example nginx-1:1.20.1-14.el9_2.1.*, including epoch, version and release. From then on, dnf upgrade skips any newer build of that package.
To see what the plugin stored:
cat /etc/dnf/plugins/versionlock.listYou can edit that file by hand, but prefer the commands. They write entries in the correct format.
To remove every lock at once, which is useful when ending a maintenance freeze:
sudo dnf versionlock clearDependency conflicts
A lock can conflict with a pending update. If package B in an update requires a newer nginx than the locked one, dnf cannot install B. It reports a dependency problem, and the whole transaction may fail. You can use --skip-broken to drop the conflicting packages and apply the rest:
sudo dnf upgrade --skip-brokenRead what it skipped. Skipped packages are still unpatched.
Fedora and dnf5
On Fedora 41 and later, and any system using dnf5, the plugin comes in a different package and the command is similar but built in or packaged differently. Check dnf versionlock --help on your system. This chapter's commands target RHEL 8 and 9.
One-time and configuration-level alternatives
Sometimes you only need to skip a package for one run. You do not need a hold.
sudo dnf upgrade --exclude=nginxFor a permanent exclusion that is not tied to a specific version, dnf accepts an exclude= line in /etc/dnf/dnf.conf:
[main]
exclude=nginx*This differs from a version lock. An exclusion hides the package from all upgrade and install operations, including future security fixes and new installs. A lock pins one version. Use a lock when you know exactly which version you want.
On apt, the equivalent of a flexible preference is pinning with files in /etc/apt/preferences.d/. Pinning can prefer a specific version or repository, and it is more powerful and more error-prone than apt-mark hold. For "do not touch this package for now", apt-mark hold is simpler and easier to audit.
Choosing the right tool
Keeping holds from becoming forgotten risks
Holds are invisible during normal patching, because the package never shows up as pending. Put a check into your routine:
apt-mark showhold
dnf versionlock listRun the one for your distribution before each patch window, and compare the list with your records. For each held package, ask whether the reason still applies. You can also see whether a newer version exists without installing it:
apt list --upgradable 2>/dev/null | grep nginx
apt-cache policy nginxapt list --upgradable still lists held packages as upgradable. The hold stops the upgrade, not the listing. On dnf, a locked package does not appear in dnf check-update, so use dnf versionlock list instead.
Common errors
- Held the metapackage, not the real one. The kernel example above. Check with
apt-mark showholdand withuname -rafter the next reboot. - Package "kept back" with no hold set by you. The cause may be a new dependency that
apt upgradewill not install. Useapt full-upgradeafter review. Checkapt-mark showholdtoo, in case someone else set a hold. dnf versionlockis an unknown command. The plugin is not installed. Installpython3-dnf-plugin-versionlock.- Lock does not match after the package was reinstalled. The lock refers to a specific version-release. If you downgrade or install a different build, the lock may no longer match anything. Delete it and add it again.
- Hold survives a reinstall of other packages but not a purge. Removing a package normally clears its hold state. Recheck after removal and reinstallation.
Exercise
A server runs Ubuntu. You hold postgresql-15 before a patch window, then run sudo apt upgrade. The output says one other package is "kept back". What two commands do you run to find out what is holding things up, and what do you decide before running apt full-upgrade?
Show answer
Run apt-mark showhold to confirm the held packages, and apt list --upgradable or apt -s full-upgrade (a simulation) to see which package is kept back and what a full upgrade would change. Before running apt full-upgrade, check whether the kept-back package needs a newer postgresql-15 or other held package. If it does, either release the hold on purpose after testing, or leave both unpatched and record why.
Exercise
On a RHEL 9 machine, dnf check-update no longer shows a new nginx build that you know was published. What are two possible explanations, and how do you check each?
Show answer
The package may be version-locked, which you can check with dnf versionlock list. It may also be excluded by an exclude= line in /etc/dnf/dnf.conf or a repo file, which you can check with grep -r exclude /etc/dnf/dnf.conf /etc/yum.repos.d/. A stale metadata cache is a third possibility, checked by running sudo dnf makecache.