How to RPM Query for Packages on Linux

We will explain the options that can be added to the rpm command (Redhat Package Manager) in order to query packages in Linux. The following elements of the article will be explained:

  1. How to query the whole RPM database?
  2. How to identify the specific package by using grep?
  3. How to query a file?
  4. How to query various files of this package?
  5. How to list only the configuration files?
  6. How to list only documentation files?
  7. How to restore configuration file from RPM package?

1) How to query the whole RPM database

To query the whole RPM database, we will use -qa option (query all) to displays 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) How to identify the specific package by using grep

We will use grep to shows other packages related to ssh but we have to still identify that which package is actually installing SSH.

$ rpm -qa | grep ssh
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

3) How to query a file

We installed 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 output is the RPM package that installed from.

$ rpm -qf /etc/ssh/sshd_config
output
openssh-server-7.4p1-16.el7.x86_64

4) How to query various files of this 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/[email protected]
/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

5) How to list only the configuration files

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

6) How to list only documentation files

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

7) 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.

  1. 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
  2. Download the Openssh rpm from it's source
  3. Copy openssh-server-7.4p1-16.el7.x86_64 package file to /tmp directory or any other directory of your choice.
  4. Use rpm2cpio | cpio -idmv to extract the package.

The commands we used in the above steps created a few sub directories in /tmp. We can now copy it to its original location.

Read Also:

Deep 4:08 am

About Deep

Network and System Administrator with experience in design, installation, configuration and management of enterprise network and server infrastructure. Expertise in Linux Server Configuration, Amazon Web Services and Web Server optimisation running Wordpress or Magento.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.