Table of Contents
- What is RPM?
- RPM Package Structure
- Basic RPM Commands
- Advanced RPM Operations
- RPM vs. YUM/DNF: When to Use Which?
- Best Practices for RPM Usage
- Troubleshooting Common RPM Issues
- Conclusion
- References
What is RPM?
RPM (originally an acronym for Red Hat Package Manager) is a command-line package management system used by Enterprise Linux distributions. Introduced in 1997 by Red Hat, RPM simplifies the process of distributing, installing, and maintaining software by bundling binaries, configuration files, documentation, and metadata into a single, standardized package format (.rpm).
Key Features of RPM:
- Package Format: Standardizes software distribution with metadata (version, dependencies, author, etc.) and a compressed payload (files to install).
- Low-Level Control: Directly interacts with the system to manage packages, making it ideal for granular operations.
- Cross-Distribution Support: Used by RHEL, CentOS, Fedora, Oracle Linux, SUSE Linux Enterprise, and more (though SUSE uses a variant called
rpm5).
RPM is often confused with higher-level package managers like YUM (Yellowdog Updater, Modified) or DNF (Dandified YUM), but it’s important to note: RPM is the underlying engine, while YUM/DNF are frontends that add dependency resolution and repository management.
RPM Package Structure
An RPM package (.rpm file) is a compressed archive containing three main components:
1. Filename Format
RPM filenames follow a strict convention for clarity:
[name]-[version]-[release].[architecture].rpm
Example: httpd-2.4.53-10.el9.x86_64.rpm
name:httpd(the package name, Apache HTTP Server).version:2.4.53(software version).release:10.el9(distribution-specific release number;el9= RHEL 9).architecture:x86_64(CPU architecture; common values:x86_64,noarch(architecture-independent),ppc64le).
2. Internal Structure
When unpacked, an RPM package contains:
- Metadata: Stored in a header, this includes dependencies, installation scripts, file list, and checksums.
- Payload: A compressed archive (usually
cpio+gziporxz) containing the actual files to install (binaries, configs, docs). - Scripts: Optional pre/post-installation/removal scripts (e.g.,
%prefor pre-install checks,%postto start a service after installation).
Basic RPM Commands
RPM uses a consistent syntax: rpm [options] [package]. Below are essential commands for everyday use.
Querying Packages
Use rpm -q (query) to retrieve information about installed packages or .rpm files.
| Command | Purpose |
|---|---|
rpm -q <package-name> | Check if a package is installed. Example: rpm -q httpd |
rpm -qa | List all installed packages. Pipe to grep for filtering: `rpm -qa |
rpm -qi <package-name> | Show detailed info (version, author, description). Example: rpm -qi httpd |
rpm -ql <package-name> | List all files installed by a package. Example: rpm -ql httpd |
rpm -qf <file-path> | Find which package owns a file. Example: rpm -qf /usr/sbin/httpd |
rpm -qp <package.rpm> | Query an uninstalled .rpm file (add -i, -l, etc., for details). Example: rpm -qpi httpd-2.4.53.rpm |
Installing Packages
Use rpm -i (install) to add a package from a local .rpm file.
| Command | Purpose |
|---|---|
rpm -i <package.rpm> | Install a package (basic). |
rpm -iv <package.rpm> | Install with verbose output (recommended for visibility). |
rpm -ivh <package.rpm> | Install with verbose output and hash marks (progress bar). |
Example:
sudo rpm -ivh httpd-2.4.53-10.el9.x86_64.rpm
⚠️ Note: RPM does not resolve dependencies automatically. If you see errors like error: Failed dependencies, you must install missing prerequisites first (e.g., apr, apr-util for httpd).
Updating Packages
Use rpm -U (upgrade) or rpm -F (freshen) to update packages.
| Command | Purpose |
|---|---|
rpm -Uvh <package.rpm> | Upgrade (install if not present, or update to a newer version). |
rpm -Fvh <package.rpm> | Freshen (only update if an older version is already installed). |
Example:
sudo rpm -Uvh httpd-2.4.54-1.el9.x86_64.rpm # Upgrade to newer version
Removing Packages
Use rpm -e (erase) to uninstall a package.
| Command | Purpose |
|---|---|
rpm -e <package-name> | Remove a package (use the package name, not the .rpm file). |
Example:
sudo rpm -e httpd # Removes the httpd package
⚠️ Caution: Use --nodeps to force removal without checking dependencies (e.g., rpm -e --nodeps httpd), but this can break other software依赖 on httpd. Only use as a last resort!
Advanced RPM Operations
Verifying Package Integrity
Over time, system files may be modified accidentally (or maliciously). Use rpm -V (verify) to check if a package’s files match their original state.
| Command | Purpose |
|---|---|
rpm -V <package-name> | Verify all files in a package. |
rpm -Va | Verify all installed packages (time-consuming but thorough). |
Output Explanation:
If files are modified, RPM will output a string like S.5....T. c /etc/httpd/conf/httpd.conf, where each character represents a check:
S: File size differs.M: Mode (permissions/type) differs.5: MD5 checksum differs.T: Modification time differs.c: File is a configuration file (changes here may be intentional).
Example:
rpm -V httpd # Checks if httpd’s files are unmodified
Querying Detailed Package Information
Customize query output with --queryformat to extract specific metadata (e.g., version, install date).
Example: List all installed packages with their versions and install dates:
rpm -qa --queryformat "%{NAME} %{VERSION} %{INSTALLTIME:date}\n" | grep httpd
Common --queryformat tags:
%{NAME}: Package name%{VERSION}: Version%{RELEASE}: Release number%{INSTALLTIME:date}: Human-readable install date
Handling Dependencies
RPM packages often depend on other packages (e.g., httpd requires apr). While RPM doesn’t resolve dependencies automatically, you can:
-
Identify dependencies for an uninstalled package:
rpm -qpR httpd-2.4.53.rpm # Lists all dependencies of httpd.rpm -
Install dependencies manually: Download and install each required package first.
-
Bypass dependencies (not recommended!): Use
--nodepsto ignore missing dependencies, but this may cause the package to fail:sudo rpm -ivh --nodeps httpd.rpm # Risky!
RPM vs. YUM/DNF: When to Use Which?
| RPM | YUM/DNF |
|---|---|
Low-level tool; directly manages .rpm packages. | High-level tool; uses RPM under the hood. |
| No dependency resolution. | Automatically resolves and installs dependencies. |
Ideal for local .rpm files or granular operations (e.g., verify, query). | Ideal for installing from repositories, updating the system, or handling complex dependencies. |
When to use RPM:
- Installing a local
.rpmfile you trust. - Verifying package integrity (
rpm -V). - Querying detailed package metadata.
When to use YUM/DNF:
- Installing packages from repositories (e.g.,
dnf install httpd). - Updating the entire system (
dnf update). - Resolving complex dependencies (e.g., installing a package with 10+ prerequisites).
Best Practices for RPM Usage
- Always Use
sudo: RPM requires root privileges for installation/removal. - Backup Before Changes: Save critical data (e.g.,
/etcconfigs) before major updates. - Prefer Verbose Output: Use
-vor-vhto track progress and catch errors early. - Verify After Installation: Run
rpm -V <package>to ensure files were installed correctly. - Avoid
--nodepsand--force: These flags bypass safety checks and can break your system. - Use YUM/DNF for Dependencies: For most use cases, YUM/DNF is safer and more efficient than RPM alone.
- Keep the RPM Database Healthy: The RPM database (
/var/lib/rpm/) tracks installed packages. Avoid corrupting it by abruptly terminatingrpmcommands.
Troubleshooting Common RPM Issues
1. Corrupted RPM Database
If you see errors like error: rpmdb: BDB0113 Thread/process ... exited and did not leave a note, rebuild the database:
sudo rpm --rebuilddb # Rebuilds the RPM database index
sudo rpm --initdb # Creates a new database (use only if --rebuilddb fails)
2. “Package Already Installed” Error
If rpm -i fails with package <name> is already installed, use rpm -U (upgrade) instead:
sudo rpm -Uvh httpd.rpm # Upgrades if newer, or skips if same version
3. File Conflicts
If you see file /usr/bin/example conflicts between attempted installs of packageA and packageB, resolve by:
- Removing the conflicting package first (
rpm -e packageA). - Using
--forceto overwrite (use with extreme caution!):sudo rpm -ivh --force packageB.rpm # Overwrites conflicting files
4. Missing Dependencies
If rpm -i complains about missing dependencies, use dnf to install them:
sudo dnf install apr apr-util # Installs httpd’s dependencies, then use rpm -i httpd.rpm
Conclusion
RPM is a foundational tool for package management in Enterprise Linux, offering granular control over software installation, querying, and verification. While it lacks automatic dependency resolution, its low-level flexibility makes it indispensable for system administrators.
By mastering RPM commands like rpm -ivh, rpm -V, and rpm -e, and combining them with higher-level tools like DNF for dependency handling, you can efficiently manage software on RHEL, CentOS, and other RPM-based distributions.
Remember: Always prioritize safety—verify packages, avoid risky flags like --nodeps, and back up critical data before making changes.