The rpm commands are used rpm based distros like CentOS, Fedora, Opensuse and RHEL. Modern Linux distros use yum and DNF package manager which has more features.
This tutorial explains the options that can be added to the rpm command (Redhat Package Manager) to list packages in Linux.
1) List all installed packages
To query the whole RPM database, we will use
-qa option (query all) to list all the installed packages in Linux.
# sudo rpm -qa
Output grub2-tools-extra-2.02-0.65.el7.centos.2.x86_64 ncurses-base-5.9-14.20130511.el7_4.noarch libpipeline-1.2.3-3.el7.x86_64 kbd-misc-1.15.5-13.el7.noarch tuned-2.9.0-1.el7.noarch bash-4.2.46-30.el7.x86_64 info-5.1-5.el7.x86_64 ...
2) List installed specific package
You can filter the
rpm -qa results using grep command to list specific package.
The following command will list specific packages related to 'ssh'.
# rpm -qa | grep ssh
sample output openssh-clients-7.4p1-16.el7.x86_64 openssh-7.4p1-16.el7.x86_64 openssh-server-7.4p1-16.el7.x86_64 libssh2-1.4.3-10.el7_2.1.x86_64
Use the following command to find the installation date for a specific package
# rpm -q --last openssh openssh-5.3p1-118.1.el6_8.x86_64 Wed 01 Mar 2017 08:30:12 PM UTC
3) List Detailed information about a package
To List detailed information about the package like version, release, install date size and signature, use qi option. For example, let's check the detailed information of 'openssh' package
# rpm -qi openssh
4) List Installed package by querying a file
ssh from the
openssh-server-5.3p1-84.1.el6.x86_64 package. We can use
-qf option (query file) with rpm command on a configuration file.
The following example will list the package name related to the file ('/etc/ssh/sshd_config') mentioned the command:
# rpm -qf /etc/ssh/sshd_config
5) Display all files installed by a package
We will use
-ql option with rpm command to show all the files of this installed package on the system.
# rpm -ql openssh-server-7.4p1-16.el7.x86_64
output /etc/pam.d/sshd /etc/ssh/sshd_config /etc/sysconfig/sshd /usr/lib/systemd/system/sshd-keygen.service /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd.socket /usr/lib/systemd/system/sshd@.service /usr/lib64/fipscheck/sshd.hmac /usr/libexec/openssh/sftp-server /usr/sbin/sshd /usr/sbin/sshd-keygen /usr/share/man/man5/moduli.5.gz /usr/share/man/man5/sshd_config.5.gz /usr/share/man/man8/sftp-server.8.gz /usr/share/man/man8/sshd.8.gz /var/empty/sshd
6) List only configuration files of the package
We will use
-qc option with
rpm command to displays configuration file related to package.
# rpm -qc openssh-server-7.4p1-16.el7.x86_64
output /etc/pam.d/sshd /etc/ssh/sshd_config /etc/sysconfig/sshd
7) List only documentation files of the package
We will use
-qd option (query documentation) with
rpm command to list only documentation files.
# rpm -qd openssh-server-7.4p1-16.el7.x86_64
output /usr/share/man/man5/moduli.5.gz /usr/share/man/man5/sshd_config.5.gz /usr/share/man/man8/sftp-server.8.gz /usr/share/man/man8/sshd.8.gz
8) Count total number of installed package
In Linux we can use wc command to count so we can pipe it rpm query command to get total count of installed packages.
# rpm -qa | wc -l 222
If you are using yum or dnf package manager then use the following command:
# yum list installed | wc -l # dnf list installed | wc -l # repoquery -a --installed | wc -l
9) List all installed package using repoquery command
In new version of CentOS, RHEL and Fedora it support using repoquery command to list all installed packages. If you are using dnf package manager then install ' dnf-utils' and for yum package manager install 'yum-utils' in-order to use repoquery.
# dnf install dnf-utils
# yum install yum-utils
Run the following command to list all installed package using repoquery command:
# repoquery -a --installed
10) List installed packages from specific repository
You can list installed packages from a specific repository like EPEL repo on RHEL or CentOS 7 or CentOS 8 distros.
The following command list all packages installed from 'EPEL" repository:
# yum list installed | grep @epel
Yumdb is another command available to list installed packages from specific repo. Yumdb comes with 'yum-utils' package, so make sure to install it.
The following yumdb command will display all installed packages from 'EPEL' repo:
# yumdb search from_repo epel
11) How to restore configuration file from RPM package
If for some reason a file has been damaged or got deleted from the system, we can start with the
rpm -qf query option to find out from what package the file originated. Next use
rpm2cpio | cpio -idmv to extract the files from the package. Consider the ssh example:
Assuming that the
/etc/ssh/sshd_config file has been deleted and we may not want to reinstall ssh, so we will restore the file using the steps below.
- The below command shows that the file comes from the openssh-server-7.4p1-16.el7.x86_64 Package.
# rpm -qf /etc/init.d/sshd
- Download the Openssh rpm from its source
openssh-server-7.4p1-16.el7.x86_64package file to
/tmpdirectory or any other directory of your choice.
rpm2cpio | cpio -idmvto extract the package.
The commands we used in the above steps created a few subdirectories in
/tmp. We can now copy it to its original location.
In this tutorial we learned different ways to list installed rpm packages in Linux. I hope you enjoyed reading and please leave your suggestions in the below comment section.