Blog / Linux/ Complete Guide to Modifying and Repackaging .deb Files on Debian/Ubuntu

Complete Guide to Modifying and Repackaging .deb Files on Debian/Ubuntu

Debian/Ubuntu 系统下修改与重新打包 .deb 安装文件的完整指南

How to Modify and Repackage .deb Files on Debian/Ubuntu

In Debian or Ubuntu systems, you may need to modify an existing .deb package to update configuration files, fix dependencies, or apply patches. This requires unpacking, modifying, and repackaging the .deb file. Below is the standard workflow.

1. Prerequisites

First, ensure the necessary tools are installed:

sudo apt update
sudo apt install dpkg-dev

2. Unpack and Modify

Assume the package file is named package.deb.

# 1. Create directory structure
mkdir -p extract/DEBIAN

# 2. Extract package data (program files, configs, etc.)
dpkg-deb -x package.deb extract/

# 3. Extract control information (control, postinst, etc.)
dpkg-deb -e package.deb extract/DEBIAN

The directory structure will look like this:

extract/
├── DEBIAN/
│   ├── control          # Package metadata (name, version, dependencies)
│   ├── postinst         # Post-installation script (optional)
│   └── ...              # Other control scripts
└── usr/                 # Extracted files (simulated root)
    ├── bin/
    ├── lib/
    └── ...

Now you can modify any files:

  • Edit extract/DEBIAN/control to update version, dependencies, etc.
  • Modify program or configuration files under extract/usr/.
  • Add or modify control scripts (e.g., postinst, prerm).

3. Repackage

# 4. Create output directory (optional)
mkdir build

# 5. Rebuild the .deb package
dpkg-deb -b extract/ build/

The new package will be in build/, named based on the package name and version in the control file.

4. Verify and Install

Check the new package:

# View package info
dpkg-deb -I build/package_new.deb

# List package contents
dpkg-deb -c build/package_new.deb

Install for testing:

sudo dpkg -i build/package_new.deb

Important Notes

  • When modifying a package, increment the version number in the control file (e.g., add a +custom1 suffix) to avoid conflicts with the original.
  • Ensure file permissions are correct, especially for executable scripts.
  • If dependencies are changed, verify the target system meets the new requirements.
  • For complex modifications, consider rebuilding from source using dpkg-source for better compatibility.

Following these steps allows you to customize Debian/Ubuntu packages for specific deployment or debugging needs.

Post a Comment

Your email will not be published. Required fields are marked with *.