Linux Administration
Linux Package Management: apt, dpkg, dnf, rpm, Repositories and Signing
Beginner9 min readUpdated
Package managers are the difference between a server you can reason about and a pile of hand-copied binaries. This guide explains what a package really is, how the Debian and RHEL toolchains differ in vocabulary but not in idea, how repository signing establishes trust, and how to hold, downgrade and inspect packages. It closes with the cases where no package exists and you have to build or vendor software yourself.
What you will learn
- Describe the contents of a .deb or .rpm and what installing one changes on disk
- Use apt and dnf confidently for search, install, upgrade, removal and cleanup
- Add a third-party repository with a verified signing key rather than a blind curl-to-shell
- Diagnose a dependency conflict and hold, pin or downgrade a package deliberately
- Decide between a distribution package, a language package manager and building from source
A package manager does three things that copying files around cannot: it records exactly which files belong to which piece of software, it knows what each piece requires in order to work, and it can verify that what you downloaded is what the maintainers published. Everything else — the commands, the caches, the repository formats — exists to serve those three properties.
Two toolchains dominate. Debian, Ubuntu and their derivatives use .deb archives with dpkg underneath and apt on top. Fedora, RHEL, Rocky Linux, AlmaLinux and SUSE use .rpm archives with rpm underneath and dnf (or zypper) on top. The vocabularies differ, the concepts do not, so this guide presents them side by side and points out where the behaviour genuinely diverges.
What is inside a package
A package is an archive plus metadata. The archive is the file tree to unpack, laid out according to the conventions described in the filesystem hierarchy: binaries into /usr/bin, libraries into /usr/lib, configuration into /etc, documentation and man pages into /usr/share, unit files into /usr/lib/systemd/system. The metadata is the interesting part:
- Identity — name, version, release number, architecture (
amd64,arm64,noarch). - Dependencies — what must be present for this to work, usually expressed against virtual capabilities such as
libssl3orhttpd-mmnrather than package names, so alternatives can satisfy them. - Conflicts, provides, replaces — how it relates to other packages.
- Maintainer scripts — small shell fragments run before and after installation or removal, which create system accounts, generate keys, reload systemd or restart a service.
- Configuration file list — files the manager must never silently overwrite because you may have edited them.
- Checksums for every installed file.
You can look inside without installing anything:
dpkg --info ./nginx_1.26.0-1_amd64.deb # metadata
dpkg --contents ./nginx_1.26.0-1_amd64.deb # file list
rpm -qip ./nginx-1.26.0-1.el9.x86_64.rpm # metadata
rpm -qlp ./nginx-1.26.0-1.el9.x86_64.rpm # file list
That configuration file list explains a behaviour that puzzles newcomers. When an upgrade ships a changed default and you have edited the file, Debian prompts you to keep, replace or diff it, while RPM installs the new version alongside yours as *.rpmnew (or saves yours as *.rpmsave). After any large upgrade, find /etc -name '*.rpmnew' -o -name '*.dpkg-dist' is worth running.
The same operations in two dialects
| Task | Debian / Ubuntu | RHEL family |
|---|---|---|
| Refresh metadata | sudo apt update | automatic; sudo dnf makecache to force |
| Install | sudo apt install nginx | sudo dnf install nginx |
| Remove, keeping config | sudo apt remove nginx | sudo dnf remove nginx |
| Remove config too | sudo apt purge nginx | no exact equivalent |
| Upgrade everything | sudo apt upgrade / full-upgrade | sudo dnf upgrade |
| Search | apt search keyword | dnf search keyword |
| Show details | apt show nginx | dnf info nginx |
| Which package owns a file | dpkg -S /usr/sbin/nginx | rpm -qf /usr/sbin/nginx |
| List a package's files | dpkg -L nginx | rpm -ql nginx |
| Is it installed | dpkg -l nginx | rpm -q nginx |
| Install a local file | sudo apt install ./pkg.deb | sudo dnf install ./pkg.rpm |
| Clean the cache | sudo apt clean | sudo dnf clean all |
| Remove unused dependencies | sudo apt autoremove | sudo dnf autoremove |
Three points where the difference is real rather than cosmetic. First, apt update and apt upgrade are separate steps and forgetting the first is the single most common cause of "package not found"; dnf refreshes metadata on its own schedule. Second, apt upgrade refuses to remove any package, so it will hold back an upgrade that requires one — apt full-upgrade (formerly dist-upgrade) is the version that may remove things and is what you need for a release upgrade. Third, dnf history records every transaction and dnf history undo 42 rolls one back, a capability apt has no direct equivalent for.
$ dnf history list | head -4
ID | Command line | Date and time | Action(s) | Altered
------------------------------------------------------------------------------
42 | install nginx php-fpm | 2026-09-05 14:02 | Install | 31
41 | upgrade | 2026-09-01 03:11 | Upgrade | 118
Always installing local files through the front end (apt install ./pkg.deb) rather than through dpkg -i matters: the front end will pull the dependencies, whereas dpkg leaves you with a half-configured package and a apt --fix-broken install to run afterwards.
Repositories and the chain of trust
A repository is an HTTP directory containing packages plus an index. The index is signed with a maintainer's key, and every package's checksum is recorded in the index. Your machine holds the corresponding public keys, so the chain runs: trusted key verifies the signature on the index, index contains the checksum, checksum verifies the downloaded package. Break any link and the package manager refuses to install.
On Debian and Ubuntu, sources live in /etc/apt/sources.list.d/ and keys in /etc/apt/keyrings/. The current, correct pattern binds one key to one source with signed-by, rather than adding a key to a global trusted set where it could sign anything:
sudo install -d -m 0755 /etc/apt/keyrings
curl -fsSL https://download.example.com/gpg.key \
| sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg
sudo chmod 0644 /etc/apt/keyrings/example.gpg
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/example.gpg] \
https://download.example.com/debian bookworm main" \
| sudo tee /etc/apt/sources.list.d/example.list
sudo apt update
The deprecated apt-key add writes into a global keyring and is removed from current releases; if a vendor's instructions still tell you to use it, they are out of date. Newer releases also accept a single .sources file in deb822 format, which is easier to read and lets you embed the key inline.
The RHEL-family equivalent is a .repo file in /etc/yum.repos.d/:
[example]
name=Example packages
baseurl=https://download.example.com/el9/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://download.example.com/RPM-GPG-KEY-example
gpgcheck=1 is the line that matters. Turning it off to make an installation work is how a compromised or corrupted mirror ends up in production. If a key is genuinely unavailable, get it from the vendor over a channel you trust and import it with rpm --import, rather than disabling the check.
Because repository names such as bookworm-updates and el9 pin a distribution release, mixing repositories from different releases produces a system that no maintainer has ever tested. If you need one newer package, look for an official backports repository (bookworm-backports) or a module stream (dnf module list postgresql) before adding a foreign source.
Dependency resolution and the mess it can make
The resolver's job is to find a set of package versions satisfying every declared requirement. Most of the time this is invisible. It becomes visible in a few recognisable situations.
Held-back upgrades. apt upgrade reports "the following packages have been kept back" when the new version needs an additional package installed or an existing one removed. Run apt full-upgrade, or install the specific package by name and read what it proposes.
Broken or half-configured state. Usually the result of a dpkg -i without dependencies, or an interrupted upgrade. Recover with:
sudo apt --fix-broken install
sudo dpkg --configure -a
sudo apt install -f
Conflicting versions from two repositories. Check where a candidate comes from before installing:
$ apt policy nginx
nginx:
Installed: 1.22.1-9
Candidate: 1.26.0-1
Version table:
1.26.0-1 500
500 https://download.example.com/debian bookworm/main amd64 Packages
*** 1.22.1-9 500
500 http://deb.debian.org/debian bookworm/main amd64 Packages
The 500 is a priority; apt-cache policy and dnf repoquery --qf '%{name} %{version} %{reponame}' are the tools for answering "why did I get that version?".
Simulation before commitment. Both front ends can show the plan without executing it, which is worth doing on any server that matters:
apt-get install --dry-run some-package
dnf install --assumeno some-package
apt list --upgradable
dnf check-update
Holding, pinning and downgrading
Sometimes you need a specific version — a database that must match an application, or a driver that regressed in a newer release.
sudo apt install nginx=1.26.0-1 # exact version
sudo apt-mark hold nginx # exclude from future upgrades
apt-mark showhold
sudo apt-mark unhold nginx
sudo dnf install nginx-1.26.0-1.el9
sudo dnf downgrade nginx
sudo dnf versionlock add nginx # needs the versionlock plugin
APT also supports finer control through pinning files in /etc/apt/preferences.d/, giving a repository or a package glob a priority number so that, for example, one package comes from backports while everything else stays on stable:
Package: postgresql*
Pin: release n=bookworm-backports
Pin-Priority: 990
Every hold and pin is technical debt with a security cost, because it excludes the package from security updates. Write the reason and an expiry date in a comment, and review them.
What did that package actually put on my system?
Reverse queries are how you understand an inherited server.
dpkg -S /usr/sbin/nginx # which package owns this file
dpkg -L nginx | grep '^/etc' # its configuration files
dpkg --verify nginx # report files that differ from the package
apt-mark showmanual # packages you asked for, not pulled in
zcat /var/log/apt/history.log*.gz | less # what was installed and when
rpm -qf /usr/sbin/nginx
rpm -ql nginx
rpm -V nginx # verification: size, mode, checksum, owner
rpm -q --changelog nginx | head -30
rpm -qa --last | head # recently installed
dpkg --verify and rpm -V compare every installed file against the checksums recorded at install time. Output lines such as S.5....T. c /etc/nginx/nginx.conf say the size, checksum and timestamp changed on a file marked as configuration — expected. The same flags on a binary in /usr/sbin are not expected and deserve investigation.
Keeping a machine patched
Security updates are the highest-value routine work on any server. Both families can apply them without a human present.
# Debian / Ubuntu
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
sudo unattended-upgrade --dry-run --debug
# RHEL family
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
sudo dnf updateinfo list security
Configure automation to apply security updates only, and to log what it did. Restarts are the part people forget: a patched OpenSSL does nothing for a process that is still running the old library. needrestart on Debian and needs-restarting -r on RHEL report which services (or whether the whole machine) must be restarted. Automatic reboots belong on a schedule you control, not at 03:00 by default.
When there is no package
Occasionally software you need is not packaged for your distribution, or the packaged version is too old. Building from source follows a well-worn shape:
sudo apt install build-essential pkg-config libpcre2-dev libssl-dev zlib1g-dev
curl -fLO https://example.org/releases/widget-3.2.1.tar.gz
curl -fLO https://example.org/releases/widget-3.2.1.tar.gz.sha256
sha256sum -c widget-3.2.1.tar.gz.sha256
tar -xzf widget-3.2.1.tar.gz && cd widget-3.2.1
./configure --prefix=/usr/local
make -j"$(nproc)"
sudo make install
Install under /usr/local (the default --prefix for exactly this reason) or into /opt/widget-3.2.1, never over the distribution's /usr. Verify the checksum or signature before unpacking, keep the build dependencies listed somewhere, and remember that you have just taken on responsibility for tracking that project's security advisories yourself — nothing will notify you. checkinstall can wrap a make install into a rough .deb so at least the file list is recorded.
Snap, Flatpak and language ecosystems
Two newer formats bundle an application with its dependencies. Snap (Ubuntu) and Flatpak (cross-distribution, desktop-oriented) both isolate the application from the system libraries, which solves version conflicts and adds sandboxing at the cost of disk space, slower first start and a second update mechanism to monitor. They are a reasonable choice for desktop applications and a poor default for server components, where the distribution package integrates better with systemd, logging and the rest of the machine.
Language package managers — pip, npm, Maven, Cargo, Go modules — belong to your application, not to the operating system. The rule that avoids most pain: never sudo pip install into the system Python, because the distribution's own tools depend on it. Use a virtual environment, a project-local node_modules, or a container image built as described in Docker fundamentals. Recent Python builds enforce this themselves by refusing to install into the system interpreter without an explicit override flag.
Common mistakes
- Installing with
dpkg -iand stopping there. Dependencies are not fetched; useapt install ./file.deb. - Disabling
gpgcheckto get past an error. You have removed the only defence against a tampered mirror. - Mixing repositories across distribution releases. It works until a shared library upgrade breaks half the system.
- Leaving a hold in place forever. Security updates stop arriving silently. Date every hold.
apt upgradeon a remote host without checking what restarts. SSH itself, or the network stack, can be among them; use a multiplexer so a dropped connection does not abort the transaction.sudo pip installorsudo npm install -gon a server. System tools and your application then share one dependency tree, and one upgrade breaks the other.- Ignoring
*.dpkg-distand*.rpmnewfiles. Your configuration silently diverges from what the package now expects.
Key takeaways
- A package is files plus metadata: dependencies, maintainer scripts, a list of protected configuration files and per-file checksums.
apt/dpkganddnf/rpmdiffer in vocabulary far more than in concept; the front end resolves dependencies, the low-level tool handles one archive.- Repository trust rests on a signed index; add third-party sources with a dedicated key and
signed-by, and never turngpgcheckoff. apt policy,dnf repoqueryanddnf historyexplain where a version came from and let you undo a transaction.- Holds and pins are deliberate, documented, dated exceptions — they suppress security updates for as long as they exist.
dpkg -S,rpm -qf,dpkg --verifyandrpm -Vturn an unfamiliar server into something you can audit.- Prefer distribution packages for system components, project-scoped tools for application libraries, and
/usr/localor/optwhen you must build it yourself.
Sources and further reading
Frequently asked questions
- What is the difference between apt, apt-get and dpkg?
- dpkg installs and removes a single .deb file and knows nothing about repositories or dependencies. apt-get and its friend apt-cache sit above dpkg, download from repositories and resolve dependencies; their output is stable and safe for scripts. apt is a newer front end over the same libraries with friendlier output, progress bars and colour, intended for interactive use rather than automation.
- Is it safe to run apt upgrade on a production server?
- It is safe in the sense that it never removes packages, but it can restart services and replace a kernel. Read the list before confirming, apply changes in a staging environment first, and know that a new kernel only takes effect after a reboot. Check whether a reboot is required with the /var/run/reboot-required marker on Debian systems or needs-restarting -r on RHEL-family hosts.
- How do I stop a package from being upgraded?
- On Debian and Ubuntu use apt-mark hold packagename, which excludes it from every upgrade until you run apt-mark unhold. On RHEL-family systems add exclude=packagename to /etc/dnf/dnf.conf, or use dnf versionlock from the python3-dnf-plugin-versionlock package. In both cases record why the hold exists, because a forgotten hold silently blocks security updates for years.
- Should I install language libraries with apt or with pip, npm and Maven?
- Use the distribution package for anything the operating system itself depends on, particularly the Python interpreter and its system modules. Use the language tool inside a project-scoped environment — a virtualenv, node_modules, a Maven repository — for application dependencies, so that upgrading the distribution never breaks your application and installing your application never breaks the distribution.