Cheat Sheets

dpkg Cheat Sheet: dpkg, dpkg-query and apt-cache Commands

Seeing a package in dpkg -l doesn’t mean it’s installed. The first two letters of each line are the actual answer, and an rc line means the package is gone and only its configuration files remain on disk. This dpkg cheat sheet reads those letters, then works through the commands you’ll reach for when a Debian or Ubuntu box misbehaves: dpkg, dpkg-query, dpkg-reconfigure and apt-cache.

Original content from computingforgeeks.com - post 187374

The tables up top are the quick reference you can scan or print. The sections under them cover what a table cannot show you, such as which package owns a file on a merged-/usr system and why one failed postinst blocks every later apt run. They also cover how a dpkg hold turns up in apt, what dpkg -V does not check, and how to read apt-cache policy and rdepends. For install, upgrade and remove workflows, use the APT cheat sheet, which covers dpkg only briefly. Everything below was run on Ubuntu 26.04.1 LTS (dpkg 1.23.7) and Debian 13 (dpkg 1.22.22) in November 2026.

dpkg Cheat Sheet (Quick Reference)

Every command form in these tables ran on both lab machines. Queries work as a regular user, and anything that changes the package database needs sudo. Package names such as nginx and redis-tools are the ones used in the lab, so swap in your own.

List and inspect installed packages

CommandWhat it does
dpkg -lList every package dpkg knows, with its state code
dpkg -l "nginx*"List packages matching a glob, including never-installed ones
dpkg -l | grep -v "^ii"Show only packages that aren’t fully installed
dpkg -l | grep -c "^ii"Count installed packages
dpkg -s nginxStatus, version, size and dependencies of one package
dpkg -L nginxFiles a package installed
dpkg-query --control-list nginx-commonMaintainer scripts and control files of a package
dpkg-query -W -f='${Status}\n' nginxStatus as text, for scripts

Find which package owns a file

CommandWhat it does
dpkg -S /usr/sbin/nginxPackage that owns an exact path
dpkg -S "*bin/ls"Glob search, survives the /bin to /usr/bin merge
dpkg -S "$(readlink -f /bin/ls)"Owner of the real file behind a symlink
dpkg -S sshd_configSubstring search across all owned paths

Inspect a .deb before installing

CommandWhat it does
dpkg-deb -I pkg.debControl fields and maintainer scripts in the archive
dpkg-deb -c pkg.debFile list with owners and permissions
dpkg-deb -x pkg.deb ~/xExtract files without installing
apt-get download redis-toolsFetch a .deb from the repo into the current directory

Install, remove and purge with dpkg

CommandWhat it does
sudo dpkg -i pkg.debUnpack and configure a local package (no dependency resolution)
sudo dpkg --unpack pkg.debUnpack only, leaves it in state iU
sudo dpkg --configure probe-agentRun the configure step for one unpacked package
sudo dpkg -r nginx nginx-commonRemove packages; one with conffiles or a postrm stays as rc
sudo dpkg -P nginx-commonPurge a package and its conffiles
dpkg -l | awk '/^rc/{print $2}' | xargs -r sudo dpkg --purgePurge every leftover rc package

Holds and selections

CommandWhat it does
echo "nginx hold" | sudo dpkg --set-selectionsHold a package with dpkg
echo "nginx install" | sudo dpkg --set-selectionsRelease a dpkg hold
sudo apt-mark hold nginxSame hold flag, set through apt
apt-mark showholdList held packages
dpkg --get-selections | grep -w holdList holds from dpkg’s side
dpkg --get-selections > pkgs.txtSave the selection list of every package

Fix broken installs

CommandWhat it does
sudo dpkg --auditReport half-configured or unpacked packages (same as -C)
sudo dpkg --configure -aFinish configuring every pending package
sudo apt-get checkReport unmet dependencies
sudo apt-get --fix-broken installInstall missing dependencies of unpacked packages
sudo dpkg -V openssh-serverVerify installed files against package checksums
grep " install " /var/log/dpkg.logWhen each package was installed

dpkg-reconfigure and debconf

CommandWhat it does
sudo dpkg-reconfigure tzdataRerun a package’s setup questions
sudo dpkg-reconfigure -f noninteractive tzdataReconfigure from current system state, no prompts
sudo dpkg-reconfigure openssh-serverRegenerate missing SSH host keys
sudo debconf-show tzdataShow the stored debconf answers

apt-cache queries

CommandWhat it does
apt-cache policy nginxInstalled and candidate version, with repo priorities
apt-cache policyPriority of every configured repository
apt-cache madison nginxEvery available version, one per line
apt-cache depends nginxWhat a package needs
apt-cache rdepends nginx-commonWhat needs a package (reverse dependencies)
apt-cache rdepends --installed liblzf1Reverse dependencies that are installed
apt-cache showpkg liblzf1Versions, index files and reverse dependencies
apt-cache show redis-toolsFull index record for each available version
apt-cache search --names-only "^redis"Search package names with a regex
apt-cache statsSize of the package cache

Architectures and versions

CommandWhat it does
dpkg --print-architectureNative architecture
sudo dpkg --add-architecture i386Enable a foreign architecture (multiarch)
dpkg --print-foreign-architecturesList enabled foreign architectures
dpkg --compare-versions 2.0~rc1 lt 2.0Compare two versions the way dpkg does

Those are the commands. The rest of the page covers what they print and where readers usually misread the output, starting with the two letters at the left of dpkg -l.

Two Letters Decide the Package State

The first column of dpkg -l is what you asked for, and the second is what actually happened. They are two separate fields, so ii means “you wanted it installed and it is”, while rc means “you asked for removal and only config files are left”. A third column only appears when something is wrong, and an uppercase letter in either of the last two marks a package that needs your attention.

The capture below comes from the Ubuntu box after a few deliberate accidents. The nginx* glob also lists names dpkg has only seen in dependency fields, which is why un lines turn up for packages that were never installed. You can ignore those, because un only means dpkg has heard the name.

dpkg -l output on Ubuntu 26.04 showing ii, un, iF, rc and hi package state codes

Read the codes left to right. The table below expands every letter the header line abbreviates into the combinations you will actually meet on a server, with what each one asks you to do next.

CodeDesiredStatusWhat to do
iiinstallinstalledNothing, it’s healthy
rcremoveconfig-filesPurge it if you don’t need the old config
hiholdinstalledHeld, upgrades skip it
ununknownnot-installedNothing, dpkg only knows the name
iUinstallunpackedFiles are on disk but not configured, usually a missing dependency
iFinstallhalf-configuredThe configure script failed, fix it and run dpkg --configure -a
iHinstallhalf-installedUnpack was interrupted, reinstall the package
iW / itinstalltriggers awaited / pendingRun dpkg --configure -a
iHRinstallhalf-installed, reinst-requiredReinstall the package

Why does a removed package sometimes leave no line at all? An rc line only exists when the package shipped conffiles or a postrm script. Running sudo dpkg -r nginx nginx-common in the lab left rc nginx-common behind, while nginx itself has neither and vanished from dpkg -l entirely. Two more details catch people when they move between releases:

  • Ubuntu 26.04 (dpkg 1.23) prints +--, |+- and ||+ header marks
  • Debian 13 (dpkg 1.22) still prints Desired= and ||/
  • the state letters mean the same thing on both

A script that parses those header lines breaks on one release or the other. Use dpkg-query -W for scripts instead, covered further down.

To list every package that isn’t plainly ii, drop the five header lines and filter. On a healthy server this prints nothing:

dpkg -l | grep -v "^ii" | tail -n +6

Anything this filter prints is a package to look at before your next upgrade, because apt refuses to carry on cleanly past an iF or iU line.

dpkg -S Matches Paths, Not Commands

dpkg -S searches the file lists dpkg recorded at install time. It doesn’t resolve symlinks or follow your PATH, so on Debian 13 and Ubuntu 26.04 dpkg -S /bin/ls fails, even though /bin/ls runs fine. /bin is a symlink to usr/bin, and packages on these releases record the /usr/bin path. Debian 12 is merged too, but its coreutils still lists /bin/ls, so there the old path is the one that matches.

Ubuntu 26.04 adds a second twist. There, ls comes from the Rust uutils coreutils, so the path you query decides which package you get back, and the capture below shows all three answers you can get for the same command.

dpkg -S failing on /bin/ls on merged-usr Ubuntu 26.04 and finding coreutils-from-uutils with a glob

The glob "*bin/ls" finds coreutils-from-uutils as the owner of the /usr/bin/ls name, while readlink -f follows the link to /usr/lib/cargo/bin/coreutils/ls, which belongs to rust-coreutils. On Debian 13 the same three queries all answer coreutils. The uutils coreutils guide for Ubuntu 26.04 explains how those two packages fit together. Pick the query by what you’re after:

  • exact path known: dpkg -S /usr/sbin/nginx
  • only a command name: dpkg -S "*bin/ls"
  • the binary that really runs: dpkg -S "$(readlink -f /bin/ls)"
  • a fragment of a file name: dpkg -S sshd_config

A “no path found” answer does not always mean a stray file. /etc/hostname returned nothing on both releases, because files written by maintainer scripts or by the installer are never recorded in a package’s file list. The dpkg-query manual says so plainly. Treat a miss as “not shipped in a .deb” and check where the file was generated.

dpkg-query Formats What dpkg -l Can’t

dpkg -l fits its columns to the terminal width, so it is made for reading on screen, not for scripts. dpkg-query -W takes a format string with any control field. That gives you stable output for scripts, plus a few reports dpkg -l cannot produce at all. The db:Status-Abbrev field returns the same two letters you just learned:

dpkg-query -W -f='${Package}\t${Version}\t${db:Status-Abbrev}\n' "nginx*"

Each line is tab-separated, and packages that were never installed come back with an empty version and un:

nginx	1.28.3-2ubuntu1.12	ii
nginx-abi-1.28.3-1		un
nginx-common	1.28.3-2ubuntu1.12	ii
nginx-core		un
nginx-doc		un
nginx-extras		un
nginx-light		un 

Sorting by Installed-Size answers “what is eating my root disk” faster than du, because dpkg already knows each package’s size in KiB:

dpkg-query -W -f='${Installed-Size}\t${Package}\n' | sort -n | tail -8

On the Ubuntu 26.04 GCE image the kernel modules package leads at about 159 MiB, with LLVM and snapd behind it. The first column is KiB, so you can add it up without converting anything.

51314	google-guest-agent
59388	libclang-cpp21
67728	google-osconfig-agent
93064	linux-gcp-headers-7.0.0-1017
108192	python3-botocore
126060	snapd
136601	libllvm21
162971	linux-modules-7.0.0-1017-gcp

Three other fields earn a place in your scripts, and each one ran on both lab boxes:

  • ${Conffiles}: protected config files with their shipped checksums
  • ${binary:Package}: the name, plus :amd64 when it would otherwise be ambiguous (Multi-Arch: same or foreign-arch packages)
  • ${Status}: the state spelled out, such as install ok installed

The conffile list matters in the dpkg -V section below, because it decides which edits dpkg can detect.

A Failed postinst Leaves the Package Half-Configured

dpkg installs in two phases: it unpacks the files, then runs the package’s postinst script to configure it. If that script exits non-zero, the files stay on disk and the package sits at iF. Every later apt run then retries the same script and fails again, so a single broken package makes every later apt run exit with an error, even when the new package itself installs.

To show this, the lab used a small test package, probe-agent, whose postinst refuses to finish without a token file. Install it, audit, fix the cause and resume:

sudo dpkg -i ~/probe-agent_1.0-1_all.deb
sudo dpkg --audit

The script’s own message names the missing file, dpkg reports the exit status, and --audit lists the package as half configured. Once the token exists, dpkg --configure -a reruns only the pending configure step:

dpkg postinst failure leaving a package half-configured, dpkg --audit report and dpkg --configure -a fix

Read the line just above dpkg: error processing first, because that is the script talking and it usually names the real cause. With a real package the cause is rarely a token. The usual suspects are:

  • a port the service needs is already taken
  • a syntax error in a config file you edited
  • a service that will not start under systemd
  • a full disk or a read-only /var

Fix whichever one the script named, then resume the pending configure step:

sudo dpkg --configure -a

While the package stayed broken, an unrelated sudo apt-get install -y tree still installed tree, then retried probe-agent, failed and exited with code 100 and E: Sub-process /usr/bin/dpkg returned an error code (1). A broken package makes every apt run end in an error until you fix it.

Error: “post-installation script subprocess returned error exit status 1”

This is the Debian 13 wording (dpkg 1.22). Ubuntu 26.04 with dpkg 1.23 reports the same failure as old probe-agent package postinst maintainer script subprocess failed with exit status 1, so search logs for both strings. The fix is the same: read the script output above the error, correct the cause, run sudo dpkg --configure -a. When the script itself is buggy, dpkg-query --control-list <package> lists it and dpkg-query --control-show <package> postinst prints it, so you can see what it checks.

Error: “unable to check lock file for dpkg database directory /var/lib/dpkg: Permission denied”

Running dpkg --audit as a regular user stopped with this on both releases while the half-configured package was present. It’s a permissions problem, not a locked database, so rerun the command with sudo.

Unmet Dependencies Leave It Unpacked

dpkg -i never downloads anything. Give it a .deb whose dependencies aren’t installed and it unpacks the files, refuses to configure, and leaves the package at iU. That’s the most common way readers end up with a broken install: a vendor .deb or one fetched with apt-get download, installed with dpkg instead of apt.

The lab reproduced it with redis-tools, which needs liblzf1. Neither GCE image ships that library. apt-get download saves into the current directory, so run both lines from your home directory:

apt-get download redis-tools
sudo dpkg -i ~/redis-tools_*.deb

dpkg names the missing dependency and the version it needs. apt-get check confirms the damage and suggests the fix, and --fix-broken pulls in exactly that one library before finishing the configure step:

dpkg -i leaving redis-tools unpacked with unmet liblzf1 dependency, then apt-get --fix-broken install

Run the fix with no package names, just as the error message says:

sudo apt-get --fix-broken install

It resolves dependencies for packages already unpacked, so liblzf1 came in and redis-tools moved from iU to ii in one run. What if it proposes to remove the package instead? Then the dependency cannot be met from your repos, so read the proposal before you answer yes. Three habits keep you out of this state:

  • install local files with sudo apt install ./redis-tools_*.deb
  • keep the ./, or apt searches the repos for that name
  • clear every iU and iF line before a release upgrade

The last one matters most. An upgrade from Ubuntu 24.04 to 26.04 started with a broken package halfway through dpkg is far harder to recover than the package on its own.

Error: “dependency problems prevent configuration of redis-tools”

The full message is redis-tools depends on liblzf1 (>= 1.5); however: Package liblzf1 is not installed., followed by dependency problems - leaving unconfigured. The wording was identical on Ubuntu 26.04 and Debian 13. Run sudo apt-get --fix-broken install to install the missing library and finish the configure step.

A Hold Is One Flag, Shared by dpkg and apt

You might assume apt-mark hold and a dpkg hold are separate mechanisms that can disagree. They are not. Both write the same “desired” field in dpkg’s database, which is the h in hi, so a hold set on one side shows on the other. The lab set it with dpkg and read it back through apt:

dpkg --set-selections hold shown by apt-mark showhold and dpkg -l as hi, then released

Going the other way, sudo apt-mark hold probe-agent printed probe-agent set on hold. and dpkg --get-selections showed it as hold. Use whichever tool fits the job:

  • one or two packages: apt-mark hold and apt-mark unhold
  • a saved list across servers: dpkg --set-selections
  • auditing a host: apt-mark showhold

A hold doesn’t stop dpkg -i from installing a newer .deb by hand. It only keeps apt from upgrading the package. If you need to hold one package and upgrade the rest, the guide on upgrading a single package with apt covers the opposite case as well.

dpkg -V Only Checks What dpkg Tracks

dpkg -V compares installed files against the MD5 sums recorded when the package was unpacked, and prints nothing when everything matches. It’s a quick corruption check, not a security tool: anyone with root can rewrite the stored sums too. It is not a full audit either, though, because many files you care about were never in dpkg’s list.

The lab made three changes on each box: one line appended to /etc/ssh/moduli, one appended to /etc/ssh/sshd_config, and one documentation file deleted. Then it verified both SSH packages:

sudo dpkg -V openssh-server
sudo dpkg -V openssh-client

Two of the three changes showed up. The sshd_config edit did not, and the conffile list printed in the same capture explains why.

dpkg -V flagging a changed /etc/ssh/moduli conffile and a missing doc file, with the openssh-server conffiles list

The 5 in ??5?????? means the checksum differs, and c marks a conffile. The question marks are checks dpkg cannot run, because it stores no size, mode or owner. So why was the sshd_config edit missed? That file is not in the conffile list at all. openssh-server ships a template under /usr/share/openssh/ and generates the live file in its postinst, so dpkg has no checksum to compare. The same is true of any config file a maintainer script creates. To catch changes in those, diff against the template or track /etc in git.

dpkg-reconfigure Reruns the Package’s Questions

dpkg-reconfigure runs a package’s debconf questions and configure script again, as if it had just been installed. It is the supported way to change a setting that a package owns, rather than editing files and hoping the next upgrade does not put them back. The packages you will reconfigure most on a server:

  • tzdata for the time zone
  • openssh-server for missing host keys
  • locales for generated locales and the default
  • unattended-upgrades for automatic updates on or off

All four reconfigured cleanly on Ubuntu 26.04, openssh-server through debconf’s Noninteractive fallback and the rest with -f noninteractive. The Debian 13 cloud image does not ship locales, so there the command stopped with /usr/sbin/dpkg-reconfigure: locales is not installed. Install the package first if you need it.

Interactively, sudo dpkg-reconfigure tzdata opens the area and city menus. On a server you’ll usually script it: set the system state first, then let the package pick it up with -f noninteractive. This run on Debian 13 shows debconf’s stored answer before and after:

dpkg-reconfigure -f noninteractive tzdata on Debian 13 switching the time zone to Africa/Nairobi

The noninteractive frontend didn’t ask anything. It read the new /etc/localtime link and wrote the matching answer back into debconf, so the next tzdata upgrade keeps Africa/Nairobi. Ubuntu 26.04 printed the same three lines. The same command pattern handles a cloned VM that’s missing its SSH host keys:

sudo rm /etc/ssh/ssh_host_ed25519_key*
sudo dpkg-reconfigure openssh-server

Only the missing key type is created, and the existing ECDSA and RSA keys stay as they were. The comment shows the box’s hostname, trimmed here to probe.

Creating SSH2 ED25519 key; this may take some time ...
256 SHA256:5I2h0uLz5I+biV/lT2h3xuJjTt2+k/xzOCsRrUa2PWs root@probe (ED25519)

Expect a “REMOTE HOST IDENTIFICATION HAS CHANGED” warning from every SSH client that knew the old key. Remove the old entry with ssh-keygen -R on each client. The post-install checklist for Ubuntu 26.04 Server sets the time zone and other settings the same way.

apt-cache Reads the Index, Not the System

dpkg knows what’s installed. apt-cache knows what the repositories offer, taken from the index files apt update downloaded. Most confusing apt-cache output comes from expecting it to describe the machine. apt-cache rdepends, for example, lists every package in the archive that depends on yours, whether it’s installed or not.

Run on Ubuntu 26.04 after nginx was purged, apt-cache policy shows an empty installed line, the candidate apt would pick, and the priority of each source:

apt-cache policy nginx and apt-cache rdepends on Ubuntu 26.04 with amd64v3 package indexes

Read policy from the top, in this order:

  1. Installed: the version dpkg has, or (none)
  2. Candidate: the version apt install would pick
  3. the version table, each version with its source priorities

The candidate is the highest version from the highest-priority source, and 500 is the default for every enabled repo. Backports sat at 100 in the lab, so apt will not pick a backport unless you ask for it with -t. Duplicated names in rdepends output are not a bug either: each available version of the depending package adds a line, so redis-tools shows twice on Ubuntu because two builds of it are available, 5:8.0.5-1 and 5:8.0.5-1ubuntu0.1.

The capture also shows amd64v3 in the index paths. Ubuntu 26.04 on this GCE image had APT::Architecture-Variants "amd64v3" set, so apt fetched x86-64-v3 builds while dpkg --print-architecture still answered amd64, and dpkg -s nginx listed Architecture-Variant: amd64v3. Debian 13 had no variant set. If you mirror packages for an older CPU fleet, check this setting before copying a 26.04 cache.

apt-cache madison is the compact form of the version table, one line per version and source:

apt-cache madison nginx

On Debian 13 it also lists the Sources entries, and the GCE image reads its mirrors through mirror+file: lists, so you see a file path where you might expect a URL.

     nginx | 1.26.3-3+deb13u10 | mirror+file:/etc/apt/mirrors/debian-security.list trixie-security/main amd64 Packages
     nginx | 1.26.3-3+deb13u7 | mirror+file:/etc/apt/mirrors/debian.list trixie/main amd64 Packages
     nginx | 1.26.3-3+deb13u7 | mirror+file:/etc/apt/mirrors/debian.list trixie/main Sources
     nginx | 1.26.3-3+deb13u10 | mirror+file:/etc/apt/mirrors/debian-security.list trixie-security/main Sources

showpkg is the low-level view. It lists the index file each version came from and every reverse dependency with the minimum version it needs. For liblzf1 on Ubuntu 26.04 that came to 15 reverse-dependency entries, from redis-tools to virtualbox (Debian 13 lists 13):

apt-cache showpkg liblzf1

Before removing a library, add --installed to rdepends so that only packages on this machine are listed. apt-cache rdepends --installed liblzf1 returned just redis-tools, which tells you exactly what a removal would take with it. Use the plain form when you want to know what could ever pull the library in.

Versions sort by dpkg’s rules, not by string order

An epoch beats everything after it, and a tilde sorts before the release it precedes. The lab confirmed three comparisons that trip up hand-written version checks:

dpkg --compare-versions 1.28.3-2ubuntu1.12 gt 1.28.3-2ubuntu1 && echo newer
dpkg --compare-versions 2.0~rc1 lt 2.0 && echo "tilde sorts first"
dpkg --compare-versions 5:8.0.2 gt 8.1.0 && echo "epoch wins"

All three printed their message on both releases. That’s why redis-tools carries a 5: epoch in its version: it keeps the package ordered above older builds no matter what comes after the colon.

Ubuntu 26.04 and Debian 13 Differ in Five Places

Every command on this page behaved the same on both releases, but the output didn’t. These are the differences that matter if you parse output or follow a guide written for the other distro:

ItemUbuntu 26.04.1 LTSDebian 13
dpkg / apt versions1.23.7 / 3.2.11.22.22 / 3.0.4
dpkg -l header+-- corner marksDesired= and ||/
Failed postinst messagepostinst maintainer script subprocess failed with exit status 1post-installation script subprocess returned error exit status 1
Owner of /usr/bin/lscoreutils-from-uutils (target in rust-coreutils)coreutils
APT architecture variantamd64v3 indexes on this imagenone
ii packages on the GCE image with nginx added624352

The header and the error wording are what break scripts. A monitoring check that greps for “post-installation script” stays silent on Ubuntu 26.04, so match on exit status or check for iF with dpkg-query. On a fresh Debian 13 box, the Debian 13 post-install guide is the next stop once the package database is clean.

One habit covers most of this page: when apt starts failing, run dpkg -l | grep -v "^ii" | tail -n +6 before anything else. The two letters it prints tell you whether you need --configure -a, --fix-broken install or a purge.

Keep reading

Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) Ubuntu Upgrade Ubuntu 24.04 to Ubuntu 26.04 LTS (Step by Step) UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04 Security UFW Firewall Commands with Examples on Ubuntu 26.04 / 24.04 Claude Code Cheat Sheet: Commands, Shortcuts and Flags (PDF) AI Claude Code Cheat Sheet: Commands, Shortcuts and Flags (PDF) Deploy Collabora Online Office on Ubuntu with Let’s Encrypt SSL Cloud Deploy Collabora Online Office on Ubuntu with Let’s Encrypt SSL Install CyberPanel on Ubuntu 26.04 / 24.04 with Let’s Encrypt Ubuntu Install CyberPanel on Ubuntu 26.04 / 24.04 with Let’s Encrypt Linux and DevOps Cheat Sheets (Free PDF Pack) Cheat Sheets Linux and DevOps Cheat Sheets (Free PDF Pack)

Leave a Comment

Press ESC to close