Debian Packaging: From Source to Repository

Anatomy of a Debian Package

Explore the internal structure of Debian binary packages.

Anatomy of a Debian Package

A Debian package is more than a collection of files compressed into an archive. It is a structured artifact containing software, metadata, and installation-related information.

Understanding this structure is essential when building, inspecting, debugging, or maintaining Debian packages.

In this lesson, we will inspect a real .deb file, examine its internal archives, and understand how Debian uses the information stored inside it.

Learning Objectives

By the end of this lesson, you will be able to:

  • Identify the main components of a Debian binary package.
  • Inspect a .deb file without installing it.
  • Read package metadata and control information.
  • Explore the files included in a package.
  • Understand the difference between package contents and installed system files.

1. A Debian Package Is an Archive

A Debian binary package uses the .deb extension.

Internally, it is an ar archive containing three main entries:

1
2
3
4
package.deb
├── debian-binary
├── control.tar.*
└── data.tar.*

Each entry has a specific responsibility.

Component Purpose
debian-binary Identifies the Debian binary package format version
control.tar.* Contains package metadata and control information
data.tar.* Contains the actual files installed on the system

The * indicates that the archive may use different compression formats, such as gzip, xz, or zstd.

Let’s inspect these components ourselves.

2. Download a Package Without Installing It

We will use the small hello package as our example.

First, download it:

1
2
3
4
mkdir debian-package-lab
cd debian-package-lab

apt download hello

This downloads the package into the current directory without installing it.

Find the downloaded file:

1
ls -lh

You should see a file with a name similar to:

1
hello_2.10-3_amd64.deb

The exact version and architecture may differ depending on your Debian or Ubuntu release.

3. Inspect the Package Archive

The ar utility can list the internal entries of a Debian package.

1
ar t hello_*.deb

A typical result looks like this:

1
2
3
debian-binary
control.tar.xz
data.tar.xz

Your package may use a different compression extension.

Now inspect the package format version:

1
ar p hello_*.deb debian-binary

The output is usually:

1
2.0

This small file identifies the version of the Debian binary package format.

4. Explore the Control Archive

The control archive contains metadata and instructions used by Debian’s package-management tools.

Let’s extract it:

1
2
3
4
mkdir control
cd control

ar p ../hello_*.deb control.tar.xz | tar -xJ

If the archive uses gzip compression, use tar -xz instead of tar -xJ. For zstd, use tar --zstd -xf.

List the extracted files:

1
find . -maxdepth 2 -type f

You will typically find files such as:

1
2
3
4
5
./control
./md5sums
./postinst
./prerm
./postrm

Not every package contains every file. Some packages have no maintainer scripts at all.

The control file

The control file contains the package’s essential metadata.

Display it:

1
cat control

You may see fields similar to:

1
2
3
4
5
6
Package: hello
Version: 2.10-3
Architecture: amd64
Maintainer: Debian QA Group
Description: example package based on GNU hello
Depends: libc6

The exact values depend on the package version.

Here are some important fields:

Field Meaning
Package Package name
Version Package version
Architecture Target architecture
Maintainer Package maintainer
Depends Required packages
Description Human-readable package description
Section Package category
Priority Package priority

We will examine these fields in more detail when we create our own package.

Maintainer scripts

A package can include scripts that run during installation, upgrade, or removal.

Common script names include:

  • preinst: runs before the package is unpacked.
  • postinst: runs after the package is unpacked.
  • prerm: runs before the package is removed.
  • postrm: runs after the package is removed.

These scripts are optional.

They should be used carefully: package installation and removal must remain reliable, predictable, and safe.

5. Explore the Data Archive

The data archive contains the actual files that will be installed on the target system.

Return to the lab directory:

1
cd ..

List the contents of the data archive:

1
ar p hello_*.deb data.tar.xz | tar -tJ

You may see paths such as:

1
2
3
./usr/bin/hello
./usr/share/doc/hello/
./usr/share/man/man1/hello.1.gz

These paths are relative to the filesystem root.

For example:

1
./usr/bin/hello

will normally be installed as:

1
/usr/bin/hello

Extract the data archive

We can inspect the files without installing the package:

1
2
3
mkdir data

ar p hello_*.deb data.tar.xz | tar -xJ -C data

Now explore the extracted directory:

1
find data -type f

This is a useful technique when debugging a package or checking exactly what it will install.

6. Use dpkg-deb Instead of ar

Although ar and tar are useful for understanding the internal structure, Debian provides a dedicated tool for working with .deb files: dpkg-deb.

Let’s inspect the same package using it.

Show package information:

1
dpkg-deb --info hello_*.deb

List its contents:

1
dpkg-deb --contents hello_*.deb

Extract its complete filesystem payload:

1
2
3
mkdir extracted

dpkg-deb --extract hello_*.deb extracted/

Extract its control information:

1
2
3
mkdir extracted-control

dpkg-deb --control hello_*.deb extracted-control/

For most everyday inspection tasks, dpkg-deb is the more convenient option.

7. Package Contents vs. Installed Files

There is an important distinction between the files included in a package and the files currently installed on a system.

A .deb file describes what the package provides. After installation, dpkg tracks the package and its installed files.

For example, after installing hello, you can inspect its installed file list:

1
dpkg -L hello

You can also query its metadata:

1
dpkg-query -W -f='${Package} ${Version} ${Architecture}\n' hello

These commands operate on the installed package database, not on an arbitrary .deb file.

This distinction becomes especially useful when troubleshooting missing files, package upgrades, and installation problems.

8. What We Learned

A Debian binary package has three main components:

  1. debian-binary identifies the package format version.
  2. control.tar.* contains metadata and optional installation scripts.
  3. data.tar.* contains the files installed on the system.

We also learned how to inspect these components using ar, tar, and dpkg-deb, without installing the package.

Exercises

Before moving on, try these exercises:

  1. Download another small package using apt download.
  2. Inspect its internal structure using ar t.
  3. Extract its control archive and read its control file.
  4. List the files included in its data archive.
  5. Compare the output of dpkg-deb --contents with the extracted directory.
  6. Find a package that includes maintainer scripts and inspect them.

You can use apt show to discover package metadata and apt-cache depends to explore dependencies.

What’s Next?

Now that we understand the internal structure of a Debian package, it is time to prepare our development environment.

In the next lesson, we will install the tools required to build Debian packages and configure a clean workspace for our practical project: Syspeek, a small system-information command-line application written in Go.

Next: Preparing the Build Environment