Inspecting and extracting Debian package contents

This post covers how to list and extract the contents of a Debian package. There will be examples used to show how to list the contents of debian packages that are installed and not-installed on a system, as well as, how to extract the debian control information and program files.

A debian package is a Unix ar archive that includes two tar archives: one containing the control information and another with the program data to be installed.

View contents of a Debian package using dpkg

The debian package manager dpkg comes with a utility to view the contents of a package. Assuming you have the actual debian package, the following command will list its contents:

For example:

As you can see in the example above, the package will install an executable binary called test into /usr/bin/ and supporting documentation will be dropped into /usr/share/.

Extract files from a Debian package using the ar command

A debian package is just an ar archive. To extract data from a deb package, use the command ar with the -x flag:

The files extracted from the deb package are control.tar.gz data.tar.gz and debian-binary. These are the control files and package data along with the debian-binary file which contains the version string of the package.

Extract files from control.tar.gz and data.tar.gz using tar

Extracting files from tar archives is straightforward, using the -xzf flags to extract to the current working directory:

Extracts the following files:

The program files are located in the data.tar.gz archive. Extracting this archive will effectively pull all the program files into the current working directory, in this case the usr/ directory:

Using dpkg-deb

To extract files from a debian package, use the following command:

For example:

This command extracts the contents of the package (without installing it) into the ./path/to/destination directory. The ./path/to/destination directory will be created if necessary, and the proper permissions given to match the contents of the package. The command can also be written as:

NOTE simply extracting the packages to the root directory will NOT ensure a correct installation. Please use dpkg or apt-get to install packages.

Extract control information from a Debian package using dpkg-deb

To extract the control section from a debian package, use the dpkg command with the -e option. This will extract the control files for a package into the specified directory:

What are preinst, postinst, prerm and postrm files?

The preinst, postinst, prerm, and postrm files are scripts that will automatically execute before or after a package is installed or removed. These scripts are part of the control section of a Debian package.

Using apt-file to view the contents of debian packages on remote repositories

It can be helpful to view the contents of packages that aren’t downloaded or installed on your the system. If you’ve configured an apt repository (for example a packagecloud repo) you can use apt-file to list the contents of a package in that repository without fetching or installing the package.

Make sure apt-file is installed on your system:

Before using apt-file you have to make sure that you’ve updated it with the repositories configured on the system. To update apt-file run the following command:

Example output (using a packagecloud repo):

After the update you can list package contents using the following command:

For example:

Note that the apt-file command takes the name of a package that exists in the repository and not the file path to a debian package. It will search for packages by name from the apt contents metadata.

Understanding how packages interact with the systems they’re installed on can be helpful in day-to-day operations. A Debian package is comprised of an ar archive containing two tar archives, and by knowing this, we can extract data using tools we’re familiar with(ar and tar). We can also use the Debian tools provided to extract and inspect debian package contents without having to manually deconstruct the Debian archive.