Skip to main content

Managing Linux Packages with apt, yum, and dnf

Manage Linux packages with apt, yum, and dnf. Install, pin versions, add signed repositories, and automate security updates on production servers.

30 Terms

Overview and What You Will Learn

A Linux package manager installs, upgrades, and removes software from signed repositories, and resolves every dependency for you. Debian and Ubuntu use apt with .deb packages. RHEL, Rocky, Fedora, and Amazon Linux use dnf (the successor to yum) with .rpm packages.

By the end of this lab you will:

  • Explain how a package moves from a repository to your server
  • Update indexes, install, search, and remove packages with apt and dnf
  • Inspect installed packages, their files, and which package owns a file
  • Pin or hold versions so upgrades cannot surprise you
  • Add a third-party repository with a verified signing key
  • Turn on automatic security updates
  • Use package commands correctly in scripts and Dockerfiles

Why This Matters in Production

A server where software is installed by hand is a liability. Nobody knows what is on it, it cannot be rebuilt the same way, and vulnerable packages go unpatched because nobody knew they existed. Package managers give you a record of every installed package and a reliable path to patch it.

Teams that provision with Ansible or Terraform, such as those at Atlassian and Shopify, declare every package and version in Git. If a server's nginx version differs from the playbook, that is configuration drift to fix, not acceptable variation.

Core Principles

A package manager works from a local copy of the repository's index. It resolves dependencies against that index, verifies signatures, and only then unpacks files. This is why you refresh the index before installing.

flowchart TD A[(Repository
packages + signatures)] --> B[Local index
apt update / dnf makecache] B --> C{Resolve
dependencies} C --> D[Download packages] D --> E{Verify GPG
signature} E -->|Valid| F[Install files
register services] E -->|Invalid| G([Abort]) classDef repo fill:#475569,color:#ffffff classDef step fill:#326CE5,color:#ffffff classDef decide fill:#DBEAFE,color:#1E293B classDef good fill:#10B981,color:#ffffff classDef bad fill:#EF4444,color:#ffffff class A repo class B,D step class C,E decide class F good class G bad
Operation apt (Debian, Ubuntu) dnf (RHEL, Amazon Linux)
Refresh index apt update dnf makecache
Install apt install nginx dnf install nginx
Remove apt remove nginx dnf remove nginx
Remove plus config apt purge nginx dnf remove nginx (edited configs saved as .rpmsave)
Upgrade all apt upgrade dnf upgrade
Search apt search nginx dnf search nginx
Package details apt show nginx dnf info nginx
List installed dpkg -l rpm -qa
Files in a package dpkg -L nginx rpm -ql nginx
Package owning a file dpkg -S /usr/sbin/nginx rpm -qf /usr/sbin/nginx
Remember

On current RHEL-family systems yum is a symlink to dnf, so the commands behave the same. apt is meant for interactive use. In scripts, use apt-get, whose output and behavior are stable.

Repository configuration lives in these places:

Family Repository files Signing keys
Debian, Ubuntu /etc/apt/sources.list.d/ /usr/share/keyrings/
RHEL, Amazon Linux /etc/yum.repos.d/ /etc/pki/rpm-gpg/

Detailed Step-by-Step Practical Lab

Milestone 1: How to Update the Package Index and Upgrade Packages

apt update only refreshes the list of available versions. apt upgrade is what installs newer versions.

Bash
sudo apt update
apt list --upgradable # preview what would change
sudo apt upgrade # upgrade without removing packages
sudo apt full-upgrade # allow dependency changes and removals
# RHEL, Rocky, Amazon Linux
sudo dnf check-update # exit code 100 means updates are available
sudo dnf upgrade
Milestone 2: How to Install, Search, and Remove Packages
Bash
# Install one or several packages
sudo apt install nginx
sudo apt install curl jq git
# Install a specific version (list versions first)
apt-cache madison nginx
sudo apt install nginx=1.24.0-1ubuntu1
# Inspect before you install
apt search web server
apt show nginx
# Remove, remove with config, and clean up orphans
sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
# RHEL family equivalents
sudo dnf install nginx
dnf list --showduplicates nginx
sudo dnf install nginx-1.24.0
dnf info nginx
sudo dnf remove nginx
Common Mistake

apt remove keeps configuration files in /etc. A later reinstall silently reuses old settings. Use apt purge when you want a clean slate.

Milestone 3: How to Inspect Installed Packages
Bash
# Debian and Ubuntu
dpkg -l | grep nginx
dpkg -L nginx # files installed by the package
dpkg -S /usr/sbin/nginx # which package owns this file
sudo apt install debsums
sudo debsums -c nginx # report files that differ from the package
# RHEL family
rpm -qa | grep nginx
rpm -ql nginx
rpm -qf /usr/sbin/nginx
rpm -V nginx # no output means files are unchanged

Modified config files show up in these checks by design, so expect /etc entries to differ.

Milestone 4: How to Pin or Hold Package Versions

Pinning stops an unexpected major upgrade of a service like Kubernetes, PostgreSQL, or nginx from changing behavior on the next patch run. A hold is the simplest tool.

Bash
# Hold a package at its current version
sudo apt-mark hold nginx
apt-mark showhold
sudo apt-mark unhold nginx
# Pin a version range through apt preferences
sudo tee /etc/apt/preferences.d/nginx-pin > /dev/null << 'EOF'
Package: nginx
Pin: version 1.24.*
Pin-Priority: 1001
EOF
# Confirm the pin
apt-cache policy nginx

On the RHEL family, use the versionlock plugin:

Bash
sudo dnf install python3-dnf-plugin-versionlock
sudo dnf versionlock add nginx
dnf versionlock list
sudo dnf versionlock delete nginx
Tip

A priority above 1000 lets apt downgrade a package to match the pin. If you only want to stop upgrades, prefer apt-mark hold.

Milestone 5: How to Add a Third-Party Repository with a Verified Key

Each third-party repository needs its own signing key. Store the key in /usr/share/keyrings/ and bind it to one repository with signed-by, so that key cannot vouch for packages from any other source. Do not use the deprecated apt-key.

Bash
# 1. Download the vendor key and convert it to a keyring
curl -fsSL https://nginx.org/keys/nginx_signing.key | \
sudo gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg
# 2. Check the fingerprint against the one the vendor publishes
gpg --show-keys --with-fingerprint /usr/share/keyrings/nginx-archive-keyring.gpg
# 3. Add the repository, bound to that key
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | \
sudo tee /etc/apt/sources.list.d/nginx.list
# 4. Refresh and confirm where the package will come from
sudo apt update
apt-cache policy nginx
# 5. Install
sudo apt install nginx

For the RHEL family, create a repo file with gpgcheck=1:

INI
[nginx-stable]
name=nginx stable repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

Save it as /etc/yum.repos.d/nginx.repo, then run sudo dnf makecache and sudo dnf install nginx.

Security

A malicious repository can replace system binaries or install backdoors. Verify the key fingerprint against the vendor's official site, never set gpgcheck=0, and never pipe a repository script from an unknown source into a root shell.

Milestone 6: How to Configure Automatic Security Updates

Manual patching is unreliable. Automatic security updates close known holes without waiting for a person to remember. Note that apt configuration files use // for comments, not #.

Bash
sudo apt install unattended-upgrades
sudo tee /etc/apt/apt.conf.d/52unattended-upgrades-local > /dev/null << 'EOF'
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Mail "ops@devops.in";
Unattended-Upgrade::MailReport "on-change";
Unattended-Upgrade::Automatic-Reboot "false";
EOF
sudo tee /etc/apt/apt.conf.d/20auto-upgrades > /dev/null << 'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "7";
EOF
# Test without installing anything
sudo unattended-upgrades --dry-run --debug

For the RHEL family:

Bash
sudo dnf install dnf-automatic
sudo sed -i 's/^upgrade_type.*/upgrade_type = security/; s/^apply_updates.*/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer
systemctl list-timers dnf-automatic.timer

Kernel updates need a reboot to take effect. Schedule reboots through your orchestration or a tool such as needrestart rather than rebooting production hosts automatically.

Milestone 7: How to Use Package Commands in Scripts and Dockerfiles

Non-interactive installs need -y and a noninteractive frontend. In container images, combine update and install in one layer and remove the cache so the layer stays small.

Bash
# In a provisioning script
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get install -y --no-install-recommends nginx=1.24.0-1ubuntu1
Dockerfile
FROM ubuntu:24.04
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*

Production Best Practices & Common Pitfalls

Make package state declarative and repeatable: what is installed should be decided in Git, not on a server.

  1. Define every package and version in Ansible, Terraform user data, or a Dockerfile.
  2. Pin or hold packages for services where a major upgrade changes behavior.
  3. Keep each third-party repository bound to its own key with signed-by.
  4. Enable automatic security updates and review the reports.
  5. Remove unused packages with autoremove and purge to reduce attack surface.
Mistake Problem Fix
Installing without apt update Stale index, old versions Run apt update first
No pin on critical services Surprise major upgrade apt-mark hold or dnf versionlock
Adding repos without checking the key Risk of tampered packages Verify the fingerprint, use signed-by
Using apt remove for cleanup Old config persists Use apt purge
# comments in apt.conf.d Syntax error, config ignored Use // comments
Using apt in scripts Output may change between versions Use apt-get

Quick Reference & Troubleshooting Commands

Task Command
Refresh and upgrade (apt) sudo apt update && sudo apt upgrade
Show where a package comes from apt-cache policy PACKAGE
List available versions apt-cache madison PACKAGE
Hold a package sudo apt-mark hold PACKAGE
Owner of a file dpkg -S FILE or rpm -qf FILE
Lock a version (dnf) sudo dnf versionlock add PACKAGE
Fix broken dependencies sudo apt install -f
Symptom First command What to look for
Package not found sudo apt update Index was stale or repo is missing
Wrong version installs apt-cache policy PACKAGE Candidate version and its source
Dependency conflict sudo apt install -f Broken packages being repaired
GPG error on update ls /usr/share/keyrings/ Missing or expired key, wrong signed-by path
Disk full during install df -h /var Free space, then sudo apt clean
Lock error ps aux | grep -E 'apt|dpkg' Another package process still running
Tip

apt-cache policy PACKAGE is the most useful diagnostic in package management. It shows the installed version, the candidate version, and every repository offering the package with its priority, so an unexpected version is explained at once.

Remember

apt update refreshes the index and installs nothing. Many servers carry stale package versions because someone ran apt install without it.

Resources

Explore More in Linux Fundamentals and File System

All 4 Topics

Frequently Asked Questions

Is Managing Linux Packages with apt, yum, and dnf free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Managing Linux Packages with apt, yum, and dnf topic cover?

Manage Linux packages with apt, yum, and dnf. Install, pin versions, add signed repositories, and automate security updates on production servers.

Found a mistake, an outdated step or a broken link?