rpm Command Examples Package Management Linux

On almost all distributions forked or cloned from RHEL (Red hat enterprise Linux), the rpm command Red Hat package manager is used to install, update, build and manage packages on the system. The name rpm is an acronym for Red Hat package manager.

All rpm package files have the .rpm extension and contain pre-compiled binary files and/or source code to build binaries and metadata that describes how rpm should treat the files. Information about installed packages is maintained in an SQLite database named rpmdb. We use the rpm command to interact with packages through the redhat package manager. Rpm is a great tool as long as we are working with single package files but since rpm cannot resolve dependencies, using rpm to install packages with complex dependency trees is tedious. Therefore we generally prefer to use yum when dependency resolution is involved.

In this article, we will demonstrate with examples how to install and remove packages and query information about them.
We’ll be working on a Centos 7 system for all the examples demonstrated in this article.

Installing packages using rpm Command

We can download a rpm package file from the web onto our local system and install it or we could also feed the package URL directly to the rpm command. Demonstrate both methods of installing an rpm package file. Use the rpm command with -i option to install a package and it is generally accompanied by the -v and -h options also. The -v and -h options add verbosity and provide a progress bar to view the of the installation process while the package is being installed. We’ll first install the rpm which that provides the tftp binary. I’ve downloaded the rpm file locally and will use the rpm command with the -ivh options to install it as shown below

[root@sahil-centos7 ~]# rpm -ivh tftp-5.2-13.el7.x86_64.rpm

For our next example, we will install the vsftpd package but this time I’ve not downloaded the rpm on my local machine but instead will use the URL of the site where the rpm is, to install the package directly.

[root@sahil-centos7 ~]# rpm -ivh http://mirror.centos.org/centos/7/os/x86_64/Packages/vsftpd-3.0.2-22.el7.x86_64.rpm

If your server computer has access to the internet then I’d suggest you to use the second method of installing rpm packages on your system as it saves the step of having to download the rpm package file using wget first.

Query information about packages

Now that we have installed a couple of packages, we will now show you how to query information about packages. Note that we can query different aspects of a package using the rpm command. For any query related operation, we add the -q option with the rpm command. Also, we could query information about packages from their rpm package files directly without installing them using the -p option along with the -q option.

To check if a package has been installed, use the rpm command with the -q option followed by the package name.
Let’s verify that the tftp package has been installed on the system.

[root@sahil-centos7 ~]# rpm -q tftp

To list all available packages use the -a option along with the -q option.
Here’s a quick snippet of the output

[root@sahil-centos7 ~]# rpm -qa | head
tftp-5.2-13.el7.x86_64
abrt-console-notification-2.1.11-45.el7.centos.x86_64

You could sort the output provided by the rpm -qa command by date to list out the packages that were recently first by using the –last option.
Here’s a snippet from the output.

[root@sahil-centos7 ~]# rpm -qa --last | head
vsftpd-3.0.2-22.el7.x86_64 Mon 25 Dec 2017 01:49:22 PM IST
tftp-5.2-13.el7.x86_64 Mon 25 Dec 2017 01:48:30 PM IST

Note that the vsftpd and tftp packages that we installed earlier are on top of the list as they were the most recent package installs.

To query information about a package we use the rpm command with the -qi option with i implying information.
Let’s query information about the vsftpd package we installed earlier.

[root@sahil-centos7 ~]# rpm -qi vsftpd

As we mentioned earlier, we could use the -p option with the -q option with the rpm command to query information directly from an rpm package file.
Let’s query the rpm package file for tftp to demonstrate.

[root@sahil-centos7 ~]# rpm -qip tftp-5.2-13.el7.x86_64.rpm

To view the files installed as part of an rpm package installation we use the rpm command with the -ql options with l implying list files.
Let’s list files installed with the vsftpd package.

[root@sahil-centos7 ~]# rpm -ql vsftpd | grep -E 'etc|bin'
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd

The output was lengthy, so I filtered it through grep.

Package Installation and Uninstall Scripts

To view any pre/post install or uninstall actions that would occur for a package, you may use the –scripts option.
Let’s query for install/uninstall scripts with the vsftpd package.

[root@sahil-centos7 ~]# rpm -q vsftpd --scripts
postinstall scriptlet (using /bin/sh):

if [ $1 -eq 1 ] ; then
# Initial installation
systemctl preset vsftpd.service >/dev/null 2>&1 || :
fi
preuninstall scriptlet (using /bin/sh):

if [ $1 -eq 0 ] ; then
# Package removal, not upgrade
systemctl --no-reload disable vsftpd.service > /dev/null 2>&1 || :
systemctl stop vsftpd.service > /dev/null 2>&1 || :
fi

if [ $1 -eq 0 ] ; then
# Package removal, not upgrade
systemctl --no-reload disable vsftpd.target > /dev/null 2>&1 || :
systemctl stop vsftpd.target > /dev/null 2>&1 || :
fi
postuninstall scriptlet (using /bin/sh):

systemctl daemon-reload >/dev/null 2>&1 || :
if [ $1 -ge 1 ] ; then
# Package upgrade, not uninstall
systemctl try-restart vsftpd.service >/dev/null 2>&1 || :
fi

To view information on updates made to the package across versions like additional functionality, bug fixes or vulnerability remediations, we use the –changelog option with the rpm -q command.

Given below is a snippet of the output from the changelog of the vsftpd package.

[root@sahil-centos7 ~]# rpm -q vsftpd --changelog | head

To view which package provides a particular file we use the rpm command with the -qf option with f implying file.
Let’s demonstrate which checking which package provides the ssh binary.

[root@sahil-centos7 ~]# rpm -qf /usr/bin/ssh
openssh-clients-6.6.1p1-31.el7.x86_64

To view dependencies for a package, we use the -R option with the -rpm -q command.
We’ll demonstrate this by looking up dependencies for the vsftpd package.

[root@sahil-centos7 ~]# rpm -qR vsftpd

Removing, upgrading and downgrading packages

To remove a package, we use the -e option for erasing with the rpm command.
We’ll demonstrate this by removing the tftp package we installed earlier.

[root@sahil-centos7 ~]# rpm -e tftp

Generally, we would use yum to update a package but we could also do it with the rpm command using the -U option.
Note that if any dependencies need to be upgraded to allow for a package to be updated then that must be done before attempting to update the package using -U flag with the rpm command because rpm command does not handle dependency resolution.
In the below example we will be upgrading the zsh shell.

[root@sahil-centos7 ~]# rpm -Uvh zsh-5.0.2-28.el7.x86_64.rpm

To downgrade a package you could remove the newer version of the package and install the older version or you could use the -U option with –oldpackage option followed by the name of the rpm file of the older version of the package.

We’ll demonstrate this by downgrading the zsh shell package that we just upgrades in the previous example.

[root@sahil-centos7 ~]# rpm -Uvh --oldpackage zsh-5.0.2-25.el7.x86_64.rpm

Note that for both upgrade and downgrade operations, the package files need to be available locally on the system.

Conclusion

This concludes our discussion of the (RPM Command )Red hat package manager tool where we showed you how to use rpm to install, query, remove, upgrade and downgrade packages.

Related Articles

EPEL Repository configuration

YUM Server configuration to Install Packages

FTP Server Installation and configuration Step by Step guide

software

Thanks for your wonderful Support and Encouragement

Sahil Suri

I am a system administrator who loves to learn and share my knowledge with the community. I've been working in the IT industry since 2011.

Leave a Reply

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