RHCE Study Guide

This tutorial explains RPM Command in Linux with examples including rpm file naming convention and how to build a basic RPM package step by step.

RHEL/Centos Linux uses RPM (Red Hat Package Manger) package file to manage the software. An RPM package file includes binary files, script files, configuration files, documentation files and installation instruction for a particular software application. RPM package files have an .rpm extension.

RPM File Naming Convention

RPM package file name provides some important information about the package. Understanding this information is the essential part of learning Linux package management. Typically a package name contains information about version, release and the hardware architecture for it is built. Following figure illustrates a package name with details.

rpm file name explain

vsftpd This is the name of actual package.
3.0.2 This is the corresponding version of package.
21 This is the subversion of package.
el7 This is the Red Hat version for which the packet was created.
x86_64 Tis is hardware architecture for which the packet was created.
rpm This the package file extension.

Before installing a package we should always check Red-Hat version and hardware architecture.

linux uname command

A package should be installed only if both information matches. To install a package we can use two commands; rpm or yum.

Difference between rpm and yum

The biggest difference between yum command and rpm command is the way they deal with dependency hell.

Usually Linux applications require a collection of packages. For a specific functionality, application may depend on other package. We have to install that package first. Sometime that package may depend on some other package. In that case we have to install that package before this package. To get the desire functionality we have to install the whole chain of dependencies.

RPM command works directly with packages, there is no easy way to deal with dependencies. We have to install all dependent package manually. While Yum command works with repositories which store all necessary information about packages including dependencies. Yum command automatically resolves dependency and installs all dependent packages with main package.

Even with this disadvantage, the rpm command is still good choice in several situations. For example, if repository is not configured or system is not connected with any working repository, the yum command will not work. In that case rpm command is the good choice to install the packages. Beside this rpm command is much more flexible while selecting the source of packages. Unlike yum command which depends on repository for packages, the rpm command does not depend of any particular source of packages. Virtually we can install package from any source with rpm command.

Where should we use which command?

If system is connected with working repository and desired package is available in repository, we should always use yum command.

If system is not connected with working repository or desired package is available outside the working repository, we should use rpm command.

We have already explained Yum command in detail with examples including how to create a local repository in previous tutorial.

How to configure yum repository in RHEL Linux

In this tutorial we will focus on RPM command.

RPM Command Query Operation

Basic syntax for query operation is following

#rpm [option] [package name]

To get a list of all available options with description we can use --help option.

rpm command help option

Most frequently used query options

Option Description
-q Perform query operation
-a List all installed packages in system
-c List all configuration files from package
-d List all documentation files from package
-R List all dependent packages
-i Provide information about package
-l List all files from package
--scripts List all scripts from package
-f Find the package which belong to specified file
-p Perform query in individual package instead of RPM database

RPM Query command examples

Let’s take few examples to understand query operation in more details. To list all installed packages in system, we can use following command.

#rpm -qa

rpm qa option

To figure out whether a particular package is installed in system or not we can use following command

#rpm -q [Package Name]

We can also use grep command with wild card to find out the packages

#rpm -qa  | grep [Query String]

Following figure illustrates the use of above commands

rpm command grep option

To get more detailed information about a package such as version, release, architecture, group, size, build date, install date, license and vendor we can use following command.

#rpm -qi [Package Name]

For example to get more information about httpd package we will use following command.

#rpm -qi httpd
rpm qi httpd

As we know rpm command is not strict about the source of rpm. Virtually we can install package from any source with rpm command. This flexibility creates a serious security risk. A hacker may hide malware in package. Since install operation is performed under the root privilege, malware will also be installed in system with this package. For this reason yum command installs packages only from trusted repositories. By following same recommendation, we can also limit security risk with rpm command. We should always install packages only from well-known sources and before installing the package we should always make sure that it does not contain any rogue scripts.

The -p option can help us to scan a package without installing it in system. By default all query operation are performed on installed packages. The -p option allows us to perform same query operation on a package which is not installed in the system. We can use -p option with --scripts option to list all scripts which a package includes.

Let’s understand this with an example. Suppose we want to install the vsftpd package which provides FTP server. We have downloaded this package from internet. If there is no internet connection, we can use RHEL installation disk to simulate the download. To be sure that this package doesn’t contain any malicious scripts we will use following command

#rpm -qpi  --scripts vsftpd-3.0.2-21.el7.x86_64.rpm
rpm qui

Usually Linux applications have associated configuration files. These files are used to control the behavior of applications. Remembering the path of each configuration file is next to impossible. RPM query operation can help us in finding right files associated with an application. Let’s understand it with an example. Suppose we don’t know which files are associated with ssh command. To find out the answer we first have to find out the package which provides ssh command. For this we can use -f option. The -f option needs at least one file name as argument. As we know each command in Linux has an associated script file which executes when we run that command at shell prompt. We can use this file as the argument in rpm command.

Let’s understand it step by step.

First we will execute which ssh command. This command will print the script file name (with absolute path) which executes when ssh command is run at shell prompt.

#which ssh
/usr/bin/ssh

Now we have one file name with absolute path which is associated with ssh command. We can use this file name as argument to find out the package name.

#rpm –qf /usr/bin/ssh
openssh-clients-6.6.1p1-31.el7.x86_64

We can also combine both commands in single command with shell scripting.

#rpm -qf  $(which ssh)

In above command shell will first process the command which is inside the bracket and then it processes the command which is outside the bracket. If you don’t like the shell scripting, you can use both commands individually.

Once we know the package name we can find all associated files with it. To know to main configuration file we can use -qc option. To know to the all documentation files we can use -qd option.

Following figure illustrates the use of above commands and options

rpm qf command

To list all associate files with package including configuration files and documentation files we can use -ql option.

rpm ql

RPM Command Install / Update / Replace / Remove Operation

Basic syntax to perform these operations is following

#rpm [Operation] [package]

Here

Operation is the options which decide what action rpm command should take on specified package.

Source of package is the specified package. (Absolute path is required for install/update/replace operation.)

Following table lists some common options used for these operations

Option Description
-i Install the package
-F Upgrade the existing package.
-U Upgrade the package. If package is not installed then install the package.
-h Show real time progress
-v Display detailed information about running operation
--replacepkgs Replace the existing package
--force Forcefully perform the action.
--nodeps Do not check dependencies
-e Delete the specified package

Let’s take few examples to understand these options in detail.

Mount RHEL installation disk at default location and change directory to Packages folder of disk.

mount rhel dvd

RPM command Install Options

To install a package we use -ivh options. For example following command will install the vsftpd package in system.

rpm ivh vsftpd

RPM command Delete Options

To remove a package we use –evh options. For example following command will delete the vsftpd package from system.

rpm e vsftpd

Although -vh options are not compulsory in install and remove operation but including these options provide us a real time update about the operation. Following figure illustrates both operations with these options and without these options.

rpm ivh vsfptd

RPM command installing a package forcefully

As we know rpm command doesn’t resolve dependency. So whenever it encounters a package which depends on other package during the installation, it fails the installation with error message related to required dependencies. We must have to install those dependent packages before this package.

Following figure illustrates this scenario

rpm failed installation

We have two possible solutions to deal with this situation. Either we install all dependent packages before this package or forcefully ignore the dependency error.

To install a package without dependency we have to use --nodeps and --force options.

rpm nodeps force option

So far in this tutorial we have learned rpm command and its basic operation. In final section of this tutorial we will learn how to build a simple rpm.

Building a basic RPM package

Two packages are required to build a simple RPM; rpm-build and rpmdevtools.

The rpm-build package provides rpmbuild command which is used to build the RPM package from source code.

The rpmdevtools package provides rpmdev-setuptree and rpmdev-newspec command which are used to build necessary directory structure and configuration files.

Let’s install both rpm form installation disk.

rpm ivh rpmdevtools

In real world environment all production related tasks are performed under the normal user account. The root user account is only used in system management related task. Let’s simulate the real world scenario and build this rpm under the normal user account.

Create a normal user account and switch to its home directory.

create normal account

A standard RPM includes following essential directories in a special directory structure.

rpmbuild This directory is the parent directory of all necessary directories.
BUILD This directory provides scratch space which is used to compile the rpm
RPMS This directory stores the binary RPM
SOURCES This directory contains the source code for RPM
SPECS This directory holds the spec file
SRPMS This directory contains source RPM built during the rpm building process
tmp This directory is used to store temporary files. [This is an optional directory.]

There is no need to create above directory tree manually. The rpmdev-setuptree command will do this job in appropriate manner.

rpmdev setup tree

We have prepared the base of RPM. Let’s add some source code in it. Make a directory outside the rpmbuild directory and create a sample file in it.

create sample file

Source codes are stored in archive format under the SOURCES directory. Prepare the archive file from source codes and move it to SOURCES directory.

rpm tar

Finally we have to create a specification file which holds all essential information about the package. Move in SPECS directory and run rpmdev-newspec command. This command will generate a template file.

rpmdev-newspec

Open the generated template file

specification file template

Update this file as illustrated in following figure and save the file.

rpm secification file

Let’s understand above configuration values

Directives Description
Name This directive sets the name of RPM.
Version This directive sets the version of RPM. Default is set to 1.
Release This directive sets the sub version and the RHEL version. Default is set to 1.el7
Summary Small description of package contents.
Group This directive is used to assign a group to the package. Used in group installation.
License License information under which we want to release this package.
URL This directive allows us to set the URL of our site.
Source0 Name of archived source code file.
BuildRoot Path of parent directory which contains all necessary directories and source code of RPM
%description A brief description of package functionality
%prep Commands which prepare the source code
%setup Move source code in BUILD directory and decompress the archived file
%install Path where this RPM will be compiled and any additional commands which should executes after this process.
%files Allow us to include additional files.
%clean Clean all temporary files generated during this process.

That’s all we need to bake this basic RPM cake. Run following command to build a RPM with this specification

$rpmbuild –v --bb newpackage.spec
rpmbuild command

In few seconds our basic rpm will be ready in RPMS/x86_64/ directory.

sample rpm file

We can build RPM from the normal user account but we cannot install RPM from normal user account. Only root account can install or remove RPM from system. Let’s switch back to root account and install this RPM.

rpm ivh sample package

This package installs a file sample_file in /opt/sample_file directory. Let’s verify the package and installation process.

rpm ivh vsftd

Great Job! We have successfully built, installed and tested the custom package. Now it’s time to cleanup the system. Remove this package.

rpm e vsftpd

That’s all for this tutorial. In next tutorial we will learn another Linux topic in detail with example.