Sub topic 2
How apt and dnf relate to dpkg and rpm
This chapter assumes you are comfortable with a shell and sudo, and that you know what a package and a repository are. It also assumes you have a Debian/Ubuntu or RHEL-family machine to try the commands on. Later chapters use apt and dnf for every patching task. Knowing what sits underneath them helps you read their errors and fix a half-finished update.
Two layers, not two competing tools
Each distribution family has two layers of package tooling.
| Layer | Debian/Ubuntu | RHEL-family (RHEL, Rocky, Alma, Fedora) |
|---|---|---|
| Low level: works on package files and the local database | `dpkg` | `rpm` |
| High level: works with repositories, metadata and dependencies | `apt` (and `apt-get`, `apt-cache`) | `dnf` (`yum` is a link to `dnf` on RHEL 8 and 9) |
| Package file format | `.deb` | `.rpm` |
The low-level tool unpacks one package file, runs its install scripts and records the result in a local database. It does not know where packages come from. If a dependency is missing, it stops with an error.
The high-level tool does everything around that step. It downloads repository metadata, works out which packages need installing or upgrading, and fetches and verifies the files. It then passes the verified files to dpkg or rpm to do the install.
The practical consequence is that apt and dnf share one source of truth about what is installed with the low-level tools. A package you install with dpkg -i shows up in apt list --installed. A package you install with rpm -i shows up in dnf list installed. The reverse is not true for repositories. The high-level tool only knows about updates for packages that exist in a configured repository.
What each layer keeps on disk
Knowing the locations makes troubleshooting concrete.
| Purpose | Debian/Ubuntu | RHEL-family |
|---|---|---|
| Installed-package database | `/var/lib/dpkg/status` | the rpm database under `/var/lib/rpm` (a link to `/usr/lib/sysimage/rpm` on newer releases) |
| Downloaded repository metadata | `/var/lib/apt/lists/` | `/var/cache/dnf/` |
| Downloaded package files | `/var/cache/apt/archives/` | `/var/cache/dnf/` |
| Low-level log | `/var/log/dpkg.log` | `/var/log/dnf.rpm.log` on RHEL 9 and `/var/log/dnf.log` on RHEL 8 |
| High-level log | `/var/log/apt/history.log` and `term.log` | `/var/log/dnf.log` |
The installed-package database is the file apt and dnf trust when they decide what is already installed. Do not edit it by hand. If you suspect corruption, back it up first.
Querying the low-level database
The low-level tools answer "what is on this machine" questions without touching the network. They are fast and work when repositories are unreachable.
| Question | Debian/Ubuntu | RHEL-family |
|---|---|---|
| Is package X installed, and which version? | `dpkg -s X` | `rpm -q X` |
| List all installed packages | `dpkg -l` | `rpm -qa` |
| Which files does X contain? | `dpkg -L X` | `rpm -ql X` |
| Which package owns this file? | `dpkg -S /path` | `rpm -qf /path` |
| Show package details | `dpkg -s X` | `rpm -qi X` |
| Check installed files against the database | `dpkg -V X` | `rpm -V X` |
The "which package owns this file" question is the most useful during an incident. The example below works on either family.
f=/usr/bin/ls
if command -v dpkg >/dev/null 2>&1; then
dpkg -S "$f"
elif command -v rpm >/dev/null 2>&1; then
rpm -qf "$f"
fiOn Ubuntu this prints coreutils: /usr/bin/ls. On a RHEL-family system it prints a name such as coreutils-8.32-34.el9.x86_64. Once you know the owning package, apt or dnf can tell you whether an update exists for it.
Verification is worth a closer look. rpm -V X prints one line for each file that differs from the recorded state (size, checksum, permissions, owner or modification time). It prints nothing when everything matches. Files that an administrator is expected to change, such as configuration files, appear with a c marker. That is normal and not a sign of tampering.
Installing a single file: the dependency trap
You will sometimes receive a vendor .deb or .rpm file instead of a repository. The tool you choose changes what happens.
# Debian/Ubuntu
sudo dpkg -i ./vendor-agent.deb # no dependency resolution
sudo apt install ./vendor-agent.deb # resolves dependencies from repositories
# RHEL-family
sudo rpm -i ./vendor-agent.rpm # no dependency resolution
sudo dnf install ./vendor-agent.rpm # resolves dependencies from repositoriesThe ./ prefix matters. Without it, apt and dnf treat the argument as a package name and search the repositories. With it, they treat it as a path.
If dpkg -i fails on missing dependencies, it leaves the package unpacked but not configured. The system is then in a half-installed state, and later apt runs complain until you repair it:
sudo apt --fix-broken installPrefer apt install ./file.deb or dnf install ./file.rpm for local files. These resolve dependencies in one step.
Remember also that a package installed from a loose file does not update itself. Nothing in a repository references it, so apt upgrade and dnf upgrade skip it. Track such packages separately, or host them in an internal repository.
Interrupted runs and locks
Both layers use lock files so that only one process changes the package database at a time. This prevents corruption, and it causes some of the most common patching errors.
- Lock held:
aptreportsCould not get lock /var/lib/dpkg/lock-frontend. Another process holds the lock, oftenunattended-upgradeor a package-update helper that started at boot. Check withps aux | grep -E 'apt|dpkg', then wait.dnfprintsWaiting for process with pid N to finishand retries. - Interrupted dpkg: a dropped SSH session or power loss during an install can leave packages unconfigured. The message
dpkg was interrupted, you must manually run 'sudo dpkg --configure -a'means exactly that. Run the command, then runsudo apt --fix-broken installif errors remain. - Interrupted dnf transaction: run
dnf history listto see whether the last transaction finished. If it did not,sudo dnf history redo lastorsudo dnf checkcan help. On RHEL 8 and 9,dnf-utilsprovidesyum-complete-transactionfor the same purpose.
Do not delete lock files to get past these errors. If no process holds the lock, dpkg --configure -a is the supported fix. Removing a lock while a real process is running can corrupt the database.
To avoid losing a patch run to a dropped connection, run long updates inside tmux or screen.
Configuration files during an upgrade
Packages ship configuration files that administrators edit. Both package formats have a rule for what to do when an upgrade brings a new default version of a file you changed.
- dpkg marks these files as conffiles. If you changed one and the package ships a new version,
dpkgasks whether to keep your version or install the maintainer's. In a non-interactive run, you set the answer ahead of time. For example,-o Dpkg::Options::="--force-confold"keeps your file. - rpm does not ask. If you changed a file,
rpmsaves the new default beside it asfile.rpmnew. For some packages it renames your edited file tofile.rpmsaveand installs the new default. After adnf upgrade, search for leftover files withsudo find /etc -name '*.rpmnew' -o -name '*.rpmsave'.
Both approaches protect your changes. The risk is a service that starts with an old configuration that no longer matches a new version of the program. Review these files after patching.
apt versus apt-get, and dnf versus yum
On Debian and Ubuntu, apt is the interface meant for people. It has progress bars and combined commands. apt-get and apt-cache are older, with output formats kept stable for scripts. Use apt-get in scripts and cron jobs. Its output does not change between releases, and apt prints a warning that its command-line interface is not stable.
On RHEL 8 and later, yum is a link to dnf, so the two behave identically. RHEL 7 used the older yum, which has a different dependency solver and different plugin names. This tutorial covers dnf.
Kernel packages
The low-level tools treat most packages as "replace the old version with the new one". Kernels are the exception. You want several versions installed so you can boot an older one if a new kernel fails.
dnfhandles this itself. It lists kernel packages as "install-only" and keeps a limited number, set byinstallonly_limitin/etc/dnf/dnf.conf, which defaults to 3.aptinstalls each kernel as a separately named package, such aslinux-image-6.8.0-45-generic. Old ones stay untilapt autoremoveremoves them.- With
rpmdirectly, userpm -i(install) for kernels, notrpm -U(upgrade).-Uremoves the old kernel and leaves you no fallback.
When to use the low-level tool directly
Use apt and dnf for installing and updating. Use dpkg and rpm for inspecting and for repairs.
Reach for dpkg or rpm directly when you need to find which package owns a file, check whether files were modified, list installed packages while repositories are down, or finish an interrupted install with dpkg --configure -a.
Exercise
A colleague runs sudo dpkg -i ./agent.deb on an Ubuntu server and it fails with a missing dependency. Later, sudo apt upgrade reports that packages are not fully installed. Name the two commands that fix this, in order, and say why apt install ./agent.deb would have avoided the problem.
Show answer
First run sudo dpkg --configure -a to finish configuring the half-installed package, then sudo apt --fix-broken install to fetch and install the missing dependency. apt install ./agent.deb would have resolved and downloaded the dependency from the repositories before handing the file to dpkg, so the package would never have been left half-installed.
Exercise
You find an unfamiliar file, /usr/sbin/chronyd, on a RHEL 9 server. Which command shows the package that owns it, and which command checks whether the file still matches what the package installed?
Show answer
rpm -qf /usr/sbin/chronyd shows the owning package. Then run rpm -V with that package's name (for example rpm -V chrony). No output means every file matches the recorded state.