10 RPM Command Usages with Practical Examples

Yellowdog Updater, ModifiedThe RPM (Short for Red Hat Package Manager) has established itself as one of the most popular distribution formats for linux software today. A first time user may feel overwhelmed by a vast number of options available, and this article aims to help you to get familiar with the command line tool RPM.

The traditional way to install a package was to retrieval an archive and extract its files into the system-wide directories. This works fine but system administrators may face lots of problems like to update the packages without breaking former settings, to uninstall the package smoothly with all its files, and to deal with dependency issues, etc.

RPM is a useful tool for managing system-wide files and troubleshooting related problems. The best way to learn this package management system is to learn from examples. Here we’d show 11 practical examples of RPM Commands.

  • Install and deinstall a package

To install a package, “rpm -ivh package.rpm” is widely used. Please check the output of command “man rpm” or “rpm –help” for details. To deinstall a package, “rpm -e package” works well. Keep note that the package’s name is changed here, and the suffix is not needed.

Also you can install a file from the network, both http and ftp are supported, enter:

 rpm -ivh ftp://joseph@ftp.admon.org/rpm/sysstat-9.0.4-1.i586.rpm

You will be prompted for the password after which the file will be downloaded and installed.

  • Verify the entire system

Perhaps you have deleted some system files by accident, but you are not sure which ones you deleted. To verify your entire system and see what might be missing, you could try the following command:  rpm -Va

If there are files that are missing or appear to be corrupted, you should probably either re-install related package or uninstall and then re-install the package.

  • Verify a file to find out which package owns it

Enter: 

rpm -qf /usr/bin/ssh

The output would look like the following:

openssh-clients-5.0p1-1
  • Verify the package which owns the program

We can combine the above two examples, and verify the package that owns that program, but you do not know which package owns it. Enter the following command:

rpm -Vf /usr/bin/ssh

and now the appropriate package is verified. If nothing outputs, the package is in good state.

  • Find out more information about a specified program

If you want to find out detailed information about a program, try the following command to locate the documentation which came with the package:

rpm -qdf /usr/bin/ssh

The output would be similar to the following:

/usr/share/man/man1/sftp.1.gz
/usr/share/man/man1/slogin.1.gz
...
  • Listing all the Installed Packages

To list all the packages installed via RPM mechanisms, enter:

rpm -qa

This command prints out the installed package names stored in the RPM database, hence you either have to have the default database location specified in the $HOME/.rpmmacros file, or to append –dbpath $HOME/myrpmdb (or whatever is your private database location) to the directive.

  • Query information about a specified RPM

You may find a new RPM, but you do not know what it does. To find information about it, use the following command:

rpm -qpi sysstat-9.0.4-1.i586.rpm

The output would be similar to the following:

Name  : sysstat    Relocations: (not relocateable)
Version     : 9.0.4     Vendor: (none)
Release     : 1     Build Date: Sat Jul 18 21:51:44 2009
Install Date: (not installed)   Build Host: localhost
Group : Applications/System     Source RPM: sysstat-9.0.4-1.src.rpm
Size  : 665516   License: GPL
Signature   : (none)
Packager    : Damien Faure <damien-jn.faure@bull.net>
URL   : http://pagesperso-orange.fr/sebastien.godard/
Summary     : SAR, SADF, MPSTAT, IOSTAT and PIDSTAT for Linux
Description :
The sysstat package contains the sar, sadf, mpstat, iostat, pidstat
and sa tools for Linux.
The sar command collects and reports system activity information.
The information collected by sar can be saved in a file in a binary
format for future inspection. The statistics reported by sar concern
I/O transfer rates, paging activity, process-related activities,
interrupts, network activity, memory and swap space utilization, CPU
utilization, kernel activities and TTY statistics, among others. Both
UP and SMP machines are fully supported.
The sadf command may  be used to display data collected by sar in
various formats (CSV, XML, etc.).
The iostat command reports CPU utilization and I/O statistics for disks.
The mpstat command reports global and per-processor statistics.
The pidstat command reports statistics for Linux tasks (processes).
  • Check what files A specified RPM installs

You would enter the following:

rpm -qpl sysstat-9.0.4-1.i586.rpm

The output is similar to the following:

/etc/cron.d/sysstat
/etc/sysconfig/sysstat
/etc/sysconfig/sysstat.ioconf
/usr/bin/iostat
/usr/bin/mpstat
....
  • Simple Unpacking of an RPM Archive

In many cases you would only need to extract files from an RPM archive, without performing complex operations with an RPM database. There is a simple way to do it:

rpm2cpio sysstat-9.0.4-1.i586.rpm | cpio -idm

This method makes use of the generic GNU CPIO utility. The rpm2cpio command converts from RPM to CPIO format, and then the output of this conversion is redirected to the cpio command.

  • Creating a Private RPM Database

To achieve a higher flexibility and to get access to more RPM functionality, you may want to make a step forward and create your own RPM database. You only have to do it once, by issuing the following instructions:

mkdir $HOME/myrpmdb
rpmdb --initdb --dbpath $HOME/myrpmdb

or, for older versions of RPM:

mkdir $HOME/myrpmdb
rpm --initdb --dbpath $HOME/myrpmdb

Mentioned in this example directory $HOME/myrpmdb can of course be substituted with any other location which suits you best. The only requirement is that you must have write permissions in this location.

Having such a personal RPM database allows you to perform more complex operations with RPM packages, without resorting to simple unpacking and manual installation. To simplify your life even further, do the following:

echo '%_dbpath /home/joseph/myrpmdb' | cat >> $HOME/.rpmmacros

This will create a file .rpmmacros in your $HOME directory, and add there the line specifying the database location. In this example, /home/yourname/myrpmdb should be substituted with your RPM database path. This will instruct RPM to use always your personal database.

It may be useful to copy the system-wide database into your own one. RPM does not provide any tool for database synchronization, and you have to do it in a very normal way:

cp -prf /var/lib/rpm/* $HOME/myrpmdb/
  • Relocating RPM Packages

An RPM package may contain information on relocatable directories. Such directories are listed as Relocations, meaning that their contents can be relocated during the installation in another directory. To perform such an installation, do the following:

rpm -ivh package.rpm --relocate /oldlocation=/newlocation

Here /oldlocation is one of the relocatable directories of the package, and /newlocation is your preferred destination, specified as an absolute path starting with “/”.

As of RPM version 4.0.4, multiple relocations are possible. To use this option, specify several relocation pairs on the command line like this “–relocate /old=/new –relocate /old/subdir=/new/subdir

Order of such pairs in the line is important, as RPM will attempt to create new directories and can not skip a level. If the package information contains several relocations, you must specify a –relocate pair for all of them. As in the case of installation, you either have to have the default database location specified in the $HOME/.rpmmacros file, or to append “–dbpath $HOME/myrpmdb” to the directive.

These are just a few examples, which would cover your daily usage. To find out more fancy feature of RPM, please check its manual page.

One thought on “10 RPM Command Usages with Practical Examples

  1. Cheers for the fantastic page – I enjoyed reading it! I always love reading this blog. 🙂

Leave a comment

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