Debian Packages

1. DEB Packaging Basics

Debian-based systems use the Debian Package Manager (DPKG) to handle software distribution, ensuring consistency, ease of installation, and maintenance.

Key Components of a DEB Package

1. Metadata:

Information about the package (e.g., name, version, architecture, dependencies).

2. Compiled Binaries:

Executables and libraries required for the software to function.

3. Configuration Files:

Default settings, typically stored in /etc.

4. Scripts:

Pre- and post-install/uninstall scripts for setup and cleanup tasks (preinst, postinst, prerm, postrm).


2. The Control File

The control file, located in DEBIAN/control, defines metadata and dependencies for the package. It is a crucial component of a DEB package.

Key Fields of a Control File

1. Package:

Specifies the name of the package.

2. Version:

Defines the version number of the package.

3. Architecture:

Indicates the target architecture (e.g., amd64, i386, all).

4. Description:

Provides a brief and detailed description of the package.

5. Maintainer:

Lists the package maintainer's name and email.

6. Dependencies:

Specifies required packages using Depends, Recommends, or Suggests.


3. Basic DPKG Commands

Managing DEB Packages

Install a Package

sudo dpkg -i package.deb

Remove a Package

sudo dpkg -r package-name

Query Installed Packages

dpkg -l | grep package-name

Verify Package Integrity

debsums -c package-name

Reconfigure a Package

sudo dpkg-reconfigure package-name

4. APT Package Manager

APT (Advanced Package Tool) is a higher-level interface for DPKG, providing dependency resolution and easier package management.

Install a Package

sudo apt install package-name

Remove a Package

sudo apt remove package-name

Update Package Index

sudo apt update

Upgrade All Packages

sudo apt upgrade

Search for a Package

apt search package-name

List Installed Packages

apt list --installed

5. Building DEB Packages

1. Create the Directory Structure

Organize files in the directory structure matching the target filesystem (e.g., /usr/bin, /etc, etc.).

2. Create the Control File

Define metadata in DEBIAN/control.

3. Build the Package

Use dpkg-deb to create the package:

dpkg-deb --build package-directory

4. Verify the Package

Inspect the contents of the package:

dpkg -c package.deb

6. Patching DEB Packages

Patching involves modifying a package to fix vulnerabilities or add customizations.

1. Extract the DEB Package

dpkg-deb -R package.deb extracted-package

2. Apply Changes

Modify files or scripts as needed in the extracted directory.

3. Rebuild the Package

dpkg-deb --build extracted-package

4. Test the Patched Package

Install and verify the functionality:

sudo dpkg -i package.deb

7. Golden Images

A golden image is a pre-configured template system image used for deploying systems quickly and consistently.

Golden Image Best Practices

  1. Start with a Minimal Base.
  2. Apply Customizations.
  3. Include Patches.
  4. Regular Updates.
  5. Validation.