Thursday, June 26, 2008

Manually modifying a .deb package

From time to time, you might be facing a binary .deb package that you have to tweak for suiting your own needs or for testing purposes. By "a binary .deb package" I mean the final .deb, with a few configuration files and pre/post-inst/rm scripts and not the source files for the .deb themselves.

So, what to do if you only want to change, say, the behavior of a postinst file and then have the .deb prepared for installation?

First of all, the .deb itself is a kind of an archive. This is why you can browse its contents while in mc, for instance ("Midnight Commander").

Step 1. So, in order to operate upon the files contained within your .deb, you need to unpack it. Suppose your .deb package is called your_package.deb. You need to unpack it using the ar command, which is part of binutils.

ar x your_package.deb

This will result in 3 files:

  • debian-binary: ASCII text
  • control.tar.gz: gzip compressed data, from Unix
  • data.tar.gz: gzip compressed data, from Unix

Now, a brief explanation on the two gzip files:

data.tar.gz is the archive that contains the files that actually get installed (in /usr or /opt etc.), while control.tar.gz is an archive containing other important files. Usually, this latter archive has to contain:

  • control: UTF-8 Unicode English text
  • md5sums: ASCII text (a text file that contains the md5 sum for every file contained within the data.tar.gz archive)
  • postinst: Bourne shell script text executable (a script that is to be executed after installing the package)
  • postrm: Bourne shell script text executable (a script that is to be executed after removing the package)
  • preinst: Bourne shell script text executable (a script that is to be executed before installing the package)
  • prerm: Bourne shell script text executable (a script that is to be executed before removing the package)

Just a quick note: it's not necessary to have all of the preinst, postinst, prerm and postrm scripts (sometimes there's no need for some of them and they can be omitted).

Step 2. OK so now that you've gotten this straighted out, it's time to unpack one of the two archives and modify the file or files you need to customize. Suppose your file is one that actually gets 'installed', so it's in data.tar.gz. To unpack this archive in a 'safe' way (to be able to easily re-pack it), execute:

mkdir extras-data
cd extras-data
tar xfz ../data.tar.gz

Step 3. Now go on and modify the file or files you need to change.

Step 4. Remember, since you changed the contents of at least one file in the data.tar.gz archive, its md5sums have changed as well. So it's time to update the md5sums file in control.tar.gz.

mkdir extras-control
cd extras-control
tar xfz ../control.tar.gz

Now, edit the file md5sums, carefully replacing each modified file's md5 sum with the new one.

Step 5. Next, repack control.tar.gz:

tar cfz control.tar.gz *
mv control.tar.gz .. #
Warning: this will overwrite the original control.tar.gz archive

Step 6. Next step is to repack the data.tar.gz archive:

cd ../extras-data
tar cfz data.tar.gz *
mv data.tar.gz ..
# Warning: this will overwrite the original data.tar.gz archive

Step 7. Then clean up:

cd ..
rm -fr extras*

Step 8. The final step is to actually rebuild your .deb package:

ar r your_new_package.deb debian-binary control.tar.gz data.tar.gz

That's it, you're done. You will now have your custom .deb package, called your_new_package.deb.

Good luck.

LATER EDIT: Be careful, when repackaging the ar archive, always do it in the order specified above (i.e. 1) debian-binary, 2) control.tar.gz and 3) data.tar.gz). Otherwise it won't work. I noticed the mistake when a colleague told me the resulted archive is not a valid Debian package if the 'instructions' were followed exactly.


  1. Thank you very much, this post helped me a lot!

  2. Thanks, just what I needed

  3. Thank you very much. It's very helpfull
    Thao NGUYEN