Common RPM Tasks[edit | edit source]

Below are the common tasks I do with RPM.

List installed packages[edit | edit source]

# rpm -qa

Format output using --queryformat.

# rpm -qa --queryformat="%{name}\n"

Look for a specific package by name[edit | edit source]

# rpm -qi <package name>

Determine the origin of a file[edit | edit source]

# rpm -qf <path to file>


# rpm -qf /etc/hosts

Extracting a rpm package[edit | edit source]

Go into a directory and extract it using cpio. This will dump the files in the current directory, so make a directory to avoid having your files clobbered.

# mkdir package && cd package
# rpm2cpio ~/custom-package.rpm | cpio -idmv

Viewing / Editing rpm package scripts[edit | edit source]

Use the rpmrebuild utility to generate a .spec file from an existing RPM package. The spec file that gets generated will contain any scripts that may be bundled with rpm which you can modify.

# rpmrebuild -e -p some-package.rpm
## This should open your editor with the .spec file that was generated.
## Save and quit when done to have the option to rebuild the rpm.

Remove a package with a broken uninstall script[edit | edit source]

# rpm -e --noscripts <package>

Building RPM[edit | edit source]

RPMs are built using a .spec specification file.

Some directives to note:

  • Summary defines what the package is for
  • Description describes what the package does.
  • prep section unpacks source and applies patches
  • build section builds any source code. The make command usually uses DESTDIR=%{buildroot}.
  • install section uses the buildroot macro (%{buildroot} to generate a temporary buildroot location.
  • files section defines all files to be included. Files can be marked as 'config', 'doc', or 'license'. Configs and docs labeld as such can be displayed using rpm -qc or rpm -qd respectively. License files are placed in /usr/share/licenses.

The Buildroot directory is like a chroot environment of the target system.

Generating a Spec File[edit | edit source]

A specification file can be generated using the rpmdev-newspec command.

# rpmdev-newspec new-package

Creating 32bit Dependency[edit | edit source]

In your RPM.spec file, have the following in your Requires line:

Requires: nss-pam-ldapd(x86-32)


Requires: nss-pam-ldapd.i686

Checking for Install/Upgrade/Remove in Install Scripts[edit | edit source]

To check whether a .rpm file is being installed, upgraded, or removed in the pre/post install scripts, check $1 against the table below:

$1 = 0 1 2
%pre Install Upgrade
%post Install Upgrade
%preun Uninstall Upgrade
%postun Uninstall Upgrade

Example usage:


if [ $1 -eq 1 ] ; then
  # Initial install

Useful Flags[edit | edit source]

When packaging .jar files, the .spec file should contain:

%define debug_package %{nil}
%define __jar_repack 0

Binary stripping is done by rpmbuild automatically. To prevent this, add:

%define __os_install_post %{nil}

Building[edit | edit source]

Use the rpmbuild command to build a RPM file using an existing .spec file.

The default topdir directory is ~/rpmbuild. This can be changed by passing in --define "_topdir `pwd`".

Packages are then signed using the rpmsign command. For example, rpmsign --addsign something.rpm.

Troubleshooting[edit | edit source]

Warning: RPMDB altered outside of yum.[edit | edit source]

If you ever get the error:

Warning: RPMDB altered outside of yum.

... it probably means that you ran rpm. To fix this warning, clear yum's cache and force a rebuild by running:

# yum clean all
# yum makecache