tl;dr
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.
Related Post
Inspecting and extracting RPM package contents
What is a Debian package?
A debian package is a Unixar archivethat includes two tar archives: one containing the control information and another with the program data to be installed.
View contents of a Debian package usingdpkg
The debian package managerdpkg
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:
$ dpkg -c ./path/to/test.deb
For example:
$ dpkg -c ./test_2.0.0_amd64.debdrwxr-xr-x root/root 0 2015-06-27 19:00 ./drwxr-xr-x root/root 0 2015-06-27 19:00 ./usr/drwxr-xr-x root/root 0 2015-06-27 19:00 ./usr/bin/-rwxr-xr-x root/root 44790352 2015-06-27 19:00 ./usr/bin/testdrwxr-xr-x root/root 0 2015-06-27 19:00 ./usr/share/drwxr-xr-x root/root 0 2015-06-27 19:00 ./usr/share/doc/drwxr-xr-x root/root 0 2015-06-27 19:00 ./usr/share/doc/test/-rw-r--r-- root/root 148 2015-06-27 18:45 ./usr/share/doc/test/changelog.gz-rw-r--r-- root/root 33 2015-06-27 18:44 ./usr/share/doc/test/copyright
As you can see in the example above, the package will install an executable binary calledtest
into/usr/bin/
and supporting documentation will be dropped into/usr/share/
.
Extract files from a Debian package
Using thear
command
A debian package is just anar
archive. To extract data from a deb package, use the commandar
with the-x
flag:
$ ar -x ./test_2.0.0_amd64.deb$ lscontrol.tar.gz data.tar.gz debian-binary test_2.0.0_amd64.deb
The files extracted from the deb package arecontrol.tar.gz
data.tar.gz
anddebian-binary
. These are the control files and package data along with thedebian-binary
file which contains the version string of the package.
Extract files fromcontrol.tar.gz
anddata.tar.gz
usingtar
Extracting files fromtar
archives is straightforward, using the-xzf
flags to extract to the current working directory:
$ tar -xzf control.tar.gz
Extracts the following files:
control md5sums
The program files are located in thedata.tar.gz
archive. Extracting this archive will effectively pull all the program files into the current working directory, in this case theusr/
directory:
$ tar -xzf data.tar.gz$ lscontrol control.tar.gz data.tar.gz debian-binary md5sums test_2.0.0_amd64.deb usr$ ls usr/bintest
Usingdpkg-deb
To extract files from a debian package, use the following command:
$ dpkg-deb -x ./path/to/test.deb ./path/to/destination
For example:
$ dpkg-deb -x ./test_2.0.0_amd64.deb .$ file ./usr/bin/testusr/bin/test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x32b2d15656286b7b0e39ba1768be7767a0e7e9e8, stripped
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:
$ dpkg -x ./test_2.0.0_amd64.deb .
NOTEsimply extracting the packages to the root directory will NOT ensure a correct installation. Please usedpkg
orapt-get
to install packages.
Extractcontrol
information from a Debian package usingdpkg-deb
To extract the control section from a debian package, use thedpkg
command with the-e
option. This will extract the control files for a package into the specified directory:
$ dpkg -e ./test_2.0.0_amd64.deb$ lscontrol md5sums postinst postrm preinst prerm$ cat ./DEBIAN/md5sumsaff2ef681a6f055bb1b3c524520d9542 usr/bin/testc95b234e1d551b6198b5e375a61e2441 usr/share/doc/test/changelog.gz1699fdbd753f1bc26e6fcb312b26b4b7 usr/share/doc/test/copyright
What arepreinst
,postinst
,prerm
andpostrm
files?
Thepreinst
,postinst
,prerm
, andpostrm
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.
$ dpkg -e ./test_2.0.0_amd64.deb$ lscontrol md5sums postinst postrm preinst prerm$ cat ./DEBIAN/postinst#!/bin/sh# This is an example script that does nothing...exit 0
Usingapt-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 apackagecloud repo) you can useapt-file
to list the contents of a package in that repository without fetching or installing the package.
Make sureapt-file
is installed on your system:
$ apt-get install apt-file
Before usingapt-file
you have to make sure that you’ve updated it with the repositories configured on the system. To updateapt-file
run the following command:
$ apt-file update
Example output (using a packagecloud repo):
$ apt-file updateDownloading complete file https://packagecloud.io/armando/test/ubuntu/dists/precise/Contents-amd64.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 90 100 90 0 0 251 0 --:--:-- --:--:-- --:--:-- 251
After the update you can list package contents using the following command:
$ apt-file list <packagename>
For example:
$ apt-file list testtest=2.0.0: /usr/bin/testtest=2.0.0: /usr/share/doc/test/changelog.gztest=2.0.0: /usr/share/doc/test/copyright
Note that theapt-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.
Conclusion
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 anar
archive containing twotar
archives, and by knowing this, we can extract data using tools we’re familiar with(ar
andtar
). We can also use the Debian tools provided to extract and inspect debian package contents without having to manually deconstruct the Debian archive.
Set up your own package repository.
Fast, reliable, and secure software starts here.
Try Packagecloud