6.1. Understanding Debian Packages

Every question this chapter answers - what is installed, what could be installed, which version wins, what broke - is answered from data attached to packages: names, versions, architectures, and the dependency relationships between them. This section reads that metadata with the inspection tools, establishes the split of responsibilities between APT and dpkg, and maps the package states and scripts that the later procedures manipulate.

6.1.1. Package Names, Versions, Architectures, and Dependencies

A package is identified by three things: its name, its version, and its architecture. The architecture of the machine itself is the starting point:

$ dpkg --print-architecture
amd64

Debian calls the native architecture amd64 here; dpkg also knows the foreign architectures enabled for multiarch installs, which Section 6.5.3 covers. What dpkg currently has installed, with versions and architectures, comes from its own database:

$ dpkg-query -W -f='${Package} ${Version} ${Architecture}\n' apt dpkg
apt 3.0.4 amd64
dpkg 1.22.22 amd64

dpkg-query -W walks the installed database and prints one line per package; -f selects the fields through the ${...} placeholders shown. The same tool, with different fields, is the workhorse for scripted checks throughout this chapter:

dpkg-query usage Description
-W -f='${Status}\n' pkg Print the three-part status of one package.
-W -f='${Version}\n' pkg Print only the installed version.
-W -f='${Conffiles}\n' pkg List a package's registered configuration files.
-W pkg… Default listing: name and version, tab-separated.
-W 'pattern*' Restrict output to names matching a glob.

Debian version strings have up to three parts, [epoch:]upstream[-revision]. The upstream version is the software's own numbering; the Debian revision after the hyphen counts Debian packaging changes; the optional leading epoch, separated by a colon, exists only to escape upstream numbering mistakes - 3:1.0-1 is newer than 2:9.9-1 by decree. Two conventions that appear in the version tables of Section 6.2.2 matter for later sections. First, 3.03+dfsg2-8 shows a suffix on the upstream version: packaging may strip non-free files and mark the fact in the version. Second, 3.7c-1~bpo13+1 shows the tilde: ~ sorts below the empty string, so a backport of 3.7c-1 names itself 3.7c-1~bpo13+1 and thereby loses every comparison against the real 3.7c-1. Version comparison is a precise operation with its own tool:

$ dpkg --compare-versions "1.0-1~bpo1" lt "1.0-1" && echo "1.0-1~bpo1 sorts before 1.0-1"
1.0-1~bpo1 sorts before 1.0-1

dpkg --compare-versions a op b performs the comparison and signals the result through its exit status - && runs the echo only when the comparison holds, which is why the line appears; the operators are lt, gt, le, ge, and eq. This is the same comparison apt uses when it ranks versions, so it settles arguments about which version is "really" newer.

The rest of a package's identity lives in its control fields. APT's copy of the fields - gathered from every configured repository - can be shown before anything is installed:

$ apt-cache show apt | sed -n "1,12p"
Package: apt
Version: 3.0.4
Installed-Size: 4481
Maintainer: APT Development Team <deity@lists.debian.org>
Architecture: amd64
Replaces: apt-transport-https (<< 1.5~alpha4~), apt-utils (<< 1.3~exp2~)
Provides: apt-transport-https (= 3.0.4)
Depends: base-passwd (>= 3.6.1) | adduser, sqv (>= 1.3.0), libapt-pkg7.0 (>= 3.0.4), debian-archive-keyring, libc6 (>= 2.38), libgcc-s1 (>= 3.0), libseccomp2 (>= 2.4.2), libssl3t64 (>= 3.0.0), libstdc++6 (>= 13.1), libsystemd0
Suggests: apt-doc, aptitude | synaptic | wajig, dpkg-dev (>= 1.17.2), gnupg | gnupg2 | gnupg1, powermgmt-base
Conflicts: apt-verify, libnettle8 (<< 3.9.1-2.2~)
Breaks: apt-transport-https (<< 1.5~alpha4~), apt-utils (<< 1.3~exp2~), aptitude (<< 0.8.10)
Description-en: commandline package manager

The pipeline is one logical operation: apt-cache show prints the full metadata record and sed -n "1,12p" keeps the first twelve lines of it. Most of the interesting semantics of Debian packages are in the relationship fields this excerpt shows:

Field Meaning Installer behavior
Depends Required for the package to work at all. Unsatisfied Depends block installation; apt pulls the listed packages in.
Recommends Strong default: co-packs most users want. Installed by default; disable per transaction with --no-install-recommends.
Suggests Optional companion packages. Never installed automatically.
Replaces Files this package may overwrite. Permits replacing another package's files.
Provides This package stands in for a (often virtual) name. Other packages may satisfy dependencies on the virtual name.
Conflicts Cannot coexist with the named package. Installing one removes the other.
Breaks The named package is known-broken by this one. Upgrading this one forces removal or upgrade of the broken one.

The Depends line above also demonstrates the syntax details: version constraints in parentheses (libc6 (>= 2.38)), alternatives separated by a vertical bar (base-passwd (>= 3.6.1) | adduser - either satisfies the requirement), and <perl:any>-style architecture qualifiers meaning "any architecture's copy will do". APT resolves all of this into one transaction; dpkg then enforces it. The dependency graph is queryable from either direction, which answers two different questions:

$ apt-cache depends cowsay=3.03+dfsg2-8
cowsay
  Depends: libtext-charwidth-perl
  Depends: <perl:any>
    perl
  Suggests: filters
  Suggests: cowsay-off
$ apt-cache rdepends libtext-charwidth-perl
libtext-charwidth-perl
Reverse Depends:
  cowsay
  libtext-wrapi18n-perl
  debconf-i18n

The first block asks "what does cowsay need?" - the answer names its dependency on the Perl module and its architecture-qualified need for any Perl interpreter, plus two suggestions it will not pull in. The second asks the reverse: "who needs this module?" - three packages depend on libtext-charwidth-perl, so removing it would have to consider all three. Note the =3.03+dfsg2-8 in the first command: apt-cache depends reports the candidate version by default, and an explicit name=version pins the query to one version when several are known.

apt-cache subcommand Description
show pkg[=ver] Print control fields as known from the indexes.
policy pkg Show installed, candidate, and all known versions with priorities.
depends pkg Expand forward dependencies.
rdepends pkg List packages declaring a dependency on pkg.
search regex Search names and descriptions in the indexes.
show -a pkg Show every known version's fields, not just the candidate.

6.1.2. The Roles of APT and dpkg

The package tools are not one program but two layers with strictly divided duties, and most confusing failures come from expecting the wrong layer to know something. Figure 6.1 shows the whole path a package travels.

The division of labor between APT and dpkg

Figure 6.1. Conceptual view of the two layers: APT resolves candidates from signed indexes and downloads archives; dpkg applies them and owns the database every later decision reads.

APT - the apt-get, apt, apt-cache, and apt-mark commands over the libapt-pkg library - knows about repositories: it reads the source configuration, fetches and verifies their signed indexes into /var/lib/apt/lists/, resolves dependencies and candidate versions from those indexes plus the installed state, and downloads .deb archives into /var/cache/apt/archives/. dpkg knows about the machine: it unpacks archives, runs the maintainer scripts inside them, asks configuration-file questions, and records the result in /var/lib/dpkg/status, with per-package detail in /var/lib/dpkg/info/. The bridge between the layers is exactly that status database: APT reads it to know what is installed, and hands dpkg a plan; dpkg reports back what it did. When the two disagree - APT's plan says one thing, dpkg's database says another - the repair procedure of Section 6.7 re-synchronizes them.

The commands divide by audience, and the distinction is contractual:

Command Intended use Stability of output format
apt Interactive administration. Human-oriented; may change between releases.
apt-get, apt-cache Scripts and precise procedures. Stable, documented output.
apt-mark Automatic/manual and hold flags. Stable, one line per action.

This chapter uses apt-get and friends throughout, both because the procedures should be reproducible and because apt prints a standing warning about its own scripting use - Chapter 2 showed that warning when it surveyed the update tools. Every operation the chapter demonstrates has a simulated form: apt-get -s computes and prints the full plan without touching the system, needing no privileges, and Sections 6.3.1, 6.4.1, and 6.5.1 use it before or instead of real changes.

Both layers serialize their work with locks under /var/lib/dpkg/ and /var/cache/apt/; a second apt-get run while one is in progress exits with a lock error rather than interfering, and unattended-upgrades (Section 6.4.2) takes those locks on its own schedule, which is why a manual upgrade can occasionally find the lock held and need a moment's patience.

6.1.3. Package States, Maintainer Scripts, and Configuration Files

dpkg records a three-part status for every package it knows: want (what the administrator last asked for), error flag (problems dpkg must tell someone about), and status (where in dpkg's unpack-and-configure lifecycle the package actually is). The full record for an ordinary, healthy package:

$ dpkg-query -W -f='${Status}\nMaintainer: ${Maintainer}\nDepends: ${Depends}\n' bash
install ok installed
Maintainer: Matthias Klose <doko@debian.org>
Depends: base-files (>= 2.1.12), debianutils (>= 5.6-0.1)

install ok installed reads as: the wanted state is install, no error flag is set, and the package is fully configured. The combinations that matter later in this chapter:

Status line Meaning
install ok installed Unpacked and configured; the normal state.
install ok unpacked Files in place, not yet configured - Section 6.7.1.
install ok half-configured Configuration started and failed - Section 6.7.1.
deinstall ok config-files Removed, but configuration files (and so the record) remain.
unknown ok not-installed dpkg knows the name only through another package's dependency fields; nothing is installed.

The last two rows are the remove/purge distinction of Section 6.3.3 in advance: a removed package that owns configuration files keeps deinstall ok config-files, and a name mentioned in some installed package's fields but never installed here resolves as unknown ok not-installed rather than as an error.

The configuration files in that distinction are a named, protected class. A package declares which of its files are configuration - dpkg calls them conffiles - and promises to leave the administrator's edits alone:

$ dpkg-query -W -f='${Conffiles}\n' bash
 /etc/bash.bashrc 72d453119aaa1e96c7c8a7a8fedcf582
 /etc/skel/.bash_logout 22bfb8c1dd94b5f3813a2b25da67463f
 /etc/skel/.bashrc ee35a240758f374832e809ae0ea4883a
 /etc/skel/.profile f4e81ade7d6f9fb342541152d08e7a97

Each conffile is listed with the checksum dpkg recorded at install time; on later installs dpkg compares that checksum against both the file on disk and the new package's copy, and if the local file was edited it asks before overwriting - the prompt of Section 6.4.3. Everything a package installs that is not a conffile is overwritten on upgrade without question, which is why programs belong under /usr and editable configuration under /etc, never mixed.

Packages carry maintainer scripts as well: shell snippets the maintainer supplies, which dpkg runs at fixed points of the lifecycle. For a shipped Debian package they are kept with dpkg's per-package metadata:

$ ls /var/lib/dpkg/info/bash.*
/var/lib/dpkg/info/bash.conffiles
/var/lib/dpkg/info/bash.list
/var/lib/dpkg/info/bash.md5sums
/var/lib/dpkg/info/bash.postinst
/var/lib/dpkg/info/bash.postrm
/var/lib/dpkg/info/bash.prerm

preinst runs before unpacking, postinst after it and again after reconfiguration, prerm before removal, postrm after - and the same postrm runs once more on purge to delete conffiles. The .list and .md5sums files beside them are the record of what the package put on disk and the checksums it shipped, which is what dpkg -L reports and what integrity checks compare against (Chapter 26 uses them for baseline audits):

$ dpkg -L bash | grep -E "^/usr/(bin/bash|share/doc/bash)$"
/usr/bin/bash
/usr/share/doc/bash

dpkg -L prints every file the package owns; the grep narrows the listing to the two entries worth naming here - the shell itself and its documentation directory - in one pipeline.

Finally, dpkg can audit its own database for packages caught between states, and a healthy machine's answer is silence:

# dpkg --audit

The command prints nothing when every package is fully configured and no dependency is broken, and exits 0. Silence here is the good result; Section 6.7.1 shows the non-silent case and what to do about it. The # marker means the block runs in the root shell - dpkg --audit is harmless as any user, but it belongs to the root-workflow this chapter keeps together; enter the root shell with sudo -i from john's session and leave it with exit.