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
aptanddnf - 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.
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 |
RememberOn current RHEL-family systems
yumis a symlink todnf, so the commands behave the same.aptis meant for interactive use. In scripts, useapt-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.
sudo apt updateapt list --upgradable # preview what would changesudo apt upgrade # upgrade without removing packagessudo apt full-upgrade # allow dependency changes and removals # RHEL, Rocky, Amazon Linuxsudo dnf check-update # exit code 100 means updates are availablesudo dnf upgradeMilestone 2: How to Install, Search, and Remove Packages
# Install one or several packagessudo apt install nginxsudo apt install curl jq git # Install a specific version (list versions first)apt-cache madison nginxsudo apt install nginx=1.24.0-1ubuntu1 # Inspect before you installapt search web serverapt show nginx # Remove, remove with config, and clean up orphanssudo apt remove nginxsudo apt purge nginxsudo apt autoremove # RHEL family equivalentssudo dnf install nginxdnf list --showduplicates nginxsudo dnf install nginx-1.24.0dnf info nginxsudo dnf remove nginxCommon Mistake
apt removekeeps configuration files in/etc. A later reinstall silently reuses old settings. Useapt purgewhen you want a clean slate.
Milestone 3: How to Inspect Installed Packages
# Debian and Ubuntudpkg -l | grep nginxdpkg -L nginx # files installed by the packagedpkg -S /usr/sbin/nginx # which package owns this filesudo apt install debsumssudo debsums -c nginx # report files that differ from the package # RHEL familyrpm -qa | grep nginxrpm -ql nginxrpm -qf /usr/sbin/nginxrpm -V nginx # no output means files are unchangedModified 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.
# Hold a package at its current versionsudo apt-mark hold nginxapt-mark showholdsudo apt-mark unhold nginx # Pin a version range through apt preferencessudo tee /etc/apt/preferences.d/nginx-pin > /dev/null << 'EOF'Package: nginxPin: version 1.24.*Pin-Priority: 1001EOF # Confirm the pinapt-cache policy nginxOn the RHEL family, use the versionlock plugin:
sudo dnf install python3-dnf-plugin-versionlocksudo dnf versionlock add nginxdnf versionlock listsudo dnf versionlock delete nginxTipA 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.
# 1. Download the vendor key and convert it to a keyringcurl -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 publishesgpg --show-keys --with-fingerprint /usr/share/keyrings/nginx-archive-keyring.gpg # 3. Add the repository, bound to that keyecho "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 fromsudo apt updateapt-cache policy nginx # 5. Installsudo apt install nginxFor the RHEL family, create a repo file with gpgcheck=1:
[nginx-stable]name=nginx stable repobaseurl=https://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keySave it as /etc/yum.repos.d/nginx.repo, then run sudo dnf makecache and sudo dnf install nginx.
SecurityA 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 #.
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 anythingsudo unattended-upgrades --dry-run --debugFor the RHEL family:
sudo dnf install dnf-automaticsudo sed -i 's/^upgrade_type.*/upgrade_type = security/; s/^apply_updates.*/apply_updates = yes/' /etc/dnf/automatic.confsudo systemctl enable --now dnf-automatic.timersystemctl list-timers dnf-automatic.timerKernel 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.
# In a provisioning scriptexport DEBIAN_FRONTEND=noninteractivesudo apt-get updatesudo apt-get install -y --no-install-recommends nginx=1.24.0-1ubuntu1FROM ubuntu:24.04RUN 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.
- Define every package and version in Ansible, Terraform user data, or a Dockerfile.
- Pin or hold packages for services where a major upgrade changes behavior.
- Keep each third-party repository bound to its own key with
signed-by. - Enable automatic security updates and review the reports.
- Remove unused packages with
autoremoveandpurgeto 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 PACKAGEis 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 updaterefreshes the index and installs nothing. Many servers carry stale package versions because someone ranapt installwithout it.