Skip to content

Some Useful RPM Package Manager Commands

RPM is an acronym for RPM Package Manager. Just like with GNU (GNU is not Unix), it has a recursive name. RPM is also known as the Red Hat Package Manager. RPM is mainly used for package management in Red Hat Enterprise Linux. Since RPM is a free software, distributed under GPL license, some popular distros like CentOS (Community Enterprise Operating System) is also using RPM as a package manager.

First, we'll download an RPM package to work on. Let's get started with some examples. I'll be using wget package for practice. You can download wget RPM package from here

Install, Upgrade, Remove and Freshen an RPM Package

To install an RPM package, you can use -i or --install flag, followed by the package file:

install package with rpm
root@localhost ~]# ls
anaconda-ks.cfg  wget-1.14-18.el7_6.1.x86_64.rpm
root@localhost ~]# rpm -i wget-1.14-18.el7_6.1.x86_64.rpm 
root@localhost ~]# echo $?

Ummm... Is this thing on? Well, yes. We haven't got any errors and our echo $? returned 0, we can say that our package has been installed successfully.

To remove an RPM package, simply use -e or --erase. Notice that this command requires the package name, not the package file itself:

remove package with rpm
root@localhost ~]# rpm -e wget
root@localhost ~]# wget
-bash: wget: command not found

Freshen is some kind of update. But it only works if an earlier version has already been installed. If the given package is newer than the installed one, RPM freshens the package:

freshen package with rpm
rpm -F wget-1.14-18.el7_6.1.x86_64.rpm 
root@localhost ~]# wget
-bash: wget: command not found

Since we've already removed the wget package, we couldn't freshen it. Nothing has changed. Bottom line, RPM freshen means "update an already installed package".

RPM upgrade works different than freshen. This command will check if the given RPM file has already been installed. If not, it'll install it. If the package has already installed but given package file contains a newer version, rpm -U will upgrade the package:

upgrade package with rpm
root@localhost ~]# wget
-bash: wget: command not found
root@localhost ~]# rpm -U wget-1.14-18.el7_6.1.x86_64.rpm 
root@localhost ~]# wget
wget: missing URL

Since we didn't have the wget package, rpm -U installed it.

If you want to see the progress of installation, you can use -h or --hash. This will display hashes during installation to inform you about progress:

rpm installation progress
root@localhost ~]# rpm -e wget
root@localhost ~]# rpm -ih wget-1.14-18.el7_6.1.x86_64.rpm 
Updating / installing...
################################# 100%]

List All Installed RPM Packages

To list all RPM packages installed on a system, we can combine RPM query (-q) with all (-a) flag:

red hat list all installed packages
root@localhost ~]# rpm -qa

Further, you can pipe this output to wc -l or nl to see how many RPM packages installed on a system:

red hat list number of packages
root@localhost ~]# rpm -qa | wc -l

What Package Did This File Come From?

Let's assume you've bumped into a configuration file, yet have no idea what package brought this file. You can use RPM to query this information. -q for query and -f for file:

red hat query a file
root@localhost ~]# ls /etc/crontab 
root@localhost ~]# rpm -qf /etc/crontab

As you can see, the /etc/crontab file came with the crontabs package.

Find Dependencies of a Package

If you need to find out dependencies of an RPM package, you can use -R (requires) flag in your queries. Note that this query requires the package name, not the package file:

red hat list dependencies
root@localhost ~]# rpm -qR wget-1.14-18.el7_6.1.x86_64.rpm 
package wget-1.14-18.el7_6.1.x86_64.rpm is not installed
root@localhost ~]# rpm -qR wget
config(wget) = 1.14-18.el7_6.1
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(PayloadIsXz) <= 5.2-1

List All The Files Came With a Package

You've installed a package and you need to know the files came with this package. Like a configuration file, binaries or documentation. You can combine -l or --list with your query:

red hat list package files
root@localhost ~]# rpm -ql crontabs

Above, you're looking at the list of files came with the crontabs package.

How to Verify an RPM Package?

Sometimes you might want to check if your package's information is correct. rpm -V compares the installed files with the information on RPM database. Since we've already know which files came with crontabs, let's change one of them:

red hat verify package
root@localhost ~]# rpm -V crontabs
root@localhost ~]# ls -l /etc/crontab 
-rw-r--r--. 1 root root 451 Haz 10  2014 /etc/crontab
root@localhost ~]# chmod 600 /etc/crontab 
root@localhost ~]# ls -l /etc/crontab 
-rw-------. 1 root root 451 Haz 10  2014 /etc/crontab
root@localhost ~]# rpm -V crontabs
.M.......  c /etc/crontab

What's going on here? First, we've verified the crontabs package. It returned nothing, thus we can say package is verified. And we've checked the permissions of /etc/crontab file which belongs to crontabs package and changed it's permissions. After that, package verification has failed. But what that output means?

See the last letter, "c". That means this file is a configuration file. Some other file types are:

  • "d" for documentation file
  • "l" for license file
  • "r" for readme file

And look at the "M". That means mode has changed like permissions or file type. Some other attributes are:

  • S for file size
  • 5 for digest (came from MD5)
  • U for user ownership
  • G for group ownership
  • T for modify time

Let's play along with this values and see if we can change anything:

red hat package verification
root@localhost ~]# ls -l /etc/crontab 
-rw-------. 1 root root 451 Haz 10  2014 /etc/crontab
root@localhost ~]# chown nobody:nobody /etc/crontab
root@localhost ~]# echo "append this" >> /etc/crontab
root@localhost ~]# rpm -V crontabs
SM5..UGT.  c /etc/crontab

We've change the user ownership, group ownership, file contents itself (therefore file size, modification time and digest) and we've got verification errors with file size, ownerships, digest, mTime and mode (already changed at previous example).

How to Fix an Unverified RPM Package?

We can use a few things to fix out package. First, we can revert every change we made in that configuration file. This will change our digest and file size back. But not the modification time. To fix permissions and ownership, we can use --setperms and --setugids:

red hat fix unverified package
root@localhost ~]# rpm --setperms crontabs
root@localhost ~]# rpm --setugids crontabs
root@localhost ~]# rpm -V crontabs
S.5....T.  c /etc/crontab
root@localhost ~]# vi /etc/crontab 
root@localhost ~]# rpm -V crontabs
.......T.  c /etc/crontab

If you're uncomfortable with this situation, you can always reinstall your package. How about not break it in the first place?