How do I extract the contents of an rpm?

Learn how do i extract the contents of an rpm? with practical examples, diagrams, and best practices. Covers linux, package, rpm development techniques with visual explanations.

How to Extract Contents from an RPM Package

Hero image for How do I extract the contents of an rpm?

Learn various methods to inspect and extract files from RPM packages without full installation, essential for system administration and troubleshooting.

RPM (Red Hat Package Manager) packages are the standard distribution format for software on Red Hat-based Linux distributions like CentOS, Fedora, and RHEL. While typically installed using rpm -i or yum install, there are many scenarios where you might need to inspect or extract the contents of an RPM file without actually installing it on your system. This can be crucial for security audits, troubleshooting, dependency analysis, or simply examining configuration files before deployment. This article will guide you through several methods to achieve this, from simple command-line tools to more advanced techniques.

Understanding RPM Structure

Before diving into extraction methods, it's helpful to understand the basic structure of an RPM package. An RPM file is essentially an archive containing metadata (like package name, version, dependencies, and scripts) and the actual payload (the files to be installed). The payload is typically compressed, often using cpio or xz.

flowchart TD
    A[RPM Package (.rpm)] --> B{Header (Metadata)}
    A --> C{Payload (Files)}
    B --> D[Name, Version, Release]
    B --> E[Dependencies, Scripts]
    C --> F[Compressed Archive (e.g., CPIO, XZ)]
    F --> G[Actual Files]

Basic structure of an RPM package

Method 1: Using rpm2cpio and cpio

The rpm2cpio utility is the most common and straightforward way to extract the contents of an RPM package. It converts the RPM package into a cpio archive, which can then be extracted using the cpio command. This method is available on virtually all Linux systems that support RPM.

rpm2cpio your_package.rpm | cpio -idmv

Extracting an RPM package using rpm2cpio and cpio

Let's break down the command:

  • rpm2cpio your_package.rpm: This command takes the RPM file as input and outputs a cpio archive to standard output.
  • |: This is a pipe, which sends the standard output of rpm2cpio as standard input to the cpio command.
  • cpio -idmv: This command extracts the cpio archive.
    • -i: Instructs cpio to extract files.
    • -d: Creates leading directories where needed.
    • -m: Retains previous file modification times.
    • -v: Verbose mode, listing the files as they are extracted.

Method 2: Using rpm with --queryformat (for metadata)

While not for extracting files, the rpm command itself can be used to query extensive metadata about an RPM package without installing it. This is useful for understanding what the package contains, its dependencies, and where files would be installed.

rpm -qp --queryformat '[%{FILENAMES}\n]' your_package.rpm

Listing all files contained within an RPM package

This command will list all files that would be installed by your_package.rpm. You can also query other information, such as dependencies, scripts, and more, by changing the --queryformat string. For example, to see dependencies:

rpm -qp --queryformat '[%{REQUIRES}\n]' your_package.rpm

Listing package dependencies

Method 3: Using ar and tar (for Debian-based systems with alien)

Although RPMs are primarily for Red Hat-based systems, you might encounter them on Debian/Ubuntu systems. The alien tool can convert RPMs to DEB packages, but it can also be used to extract them. Under the hood, alien often uses rpm2cpio. If alien isn't an option, or you're dealing with a converted package, you might encounter ar and tar archives.

# Install alien (if not already installed)
sudo apt-get install alien

# Extract the RPM using alien
alien -g your_package.rpm

# This will create a directory like your_package-version/ containing the extracted files
# You can then navigate into it and find the extracted contents.

Extracting an RPM using the alien tool on Debian/Ubuntu