RPM is RedHat Package Manager, used to install/remove/update and query the packages in Red Hat based linux. RHEL and the systems based on it uses rpm command to do that. The following example demonstrates the use of rpm query feature and shows different ways you can query rpm database and restore configuration file.
I have included the SSH package to in the example commands.
Query RPM Database and Packages
1. To query the whole RPM database, use the following command.
# rpm -qa
2. You can identify the package from which SSH is installed by using grep on the above example.
# rpm -qa |grep ssh
The output shows other packages related to ssh but you have to still identify that which package is actually installing SSH. To further break it down see the next example.
3. Check the installed package of SSH a) from sshd daemon b) from it's configuration file.
# rpm -qf /etc/init.d/sshd
# rpm -qf /etc/ssh/sshd_config
As you can see the ssh is installed from the openssh-server-5.3p1-84.1.el6.x86_64 package. You can use rpm -qf command both on daemon and a configuration file. Both will output the package it is installed from.
4. Now that you have the package name, you may want to explore more on it and want to know what are the various files this package contains. For that use rpm -ql command.
# rpm -ql openssh-server-5.3p1-84.1.el6.x86_64
The above output is showing all the files that this package installed on the system. Now let's even break it down and we only want to see the configuration files and document files supplied with this package.
5. To list only the configuration files use the rpm -qc command.
# rpm -qc openssh-server-5.3p1-84.1.el6.x86_64
6. To list only documentation files use rpm -qd command
# rpm -qd openssh-server-5.3p1-84.1.el6.x86_64
Consider a situation in which you want to configure a service, but you don't know where to find the configuration files. As an example, Consider the above example: Use rpm -qf rpm -qf /etc/init.d/sshd to find out from what package the /etc/ssh/sshd_config file originated. It should show you the openssh-server-5.3p1-84.1.el6.x86_64 package. Use rpm -ql openssh-server-5.3p1-84.1.el6.x86_64 to show a list of all the files in this package. As you can see, the names of many files are displayed, but the output is not very useful.
Now use rpm -qc openssh-server-5.3p1-84.1.el6.x86_64 to show just the configuration files used by this package. This shows a list of four files only and gives you the absolute path of /etc/ssh/sshd_config file to start configuring the service.
7. Restore configuration file from RPM Package, without reinstalling a package.
If for some reason a file has been damaged or got deleted from system, you 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 you may not want to reinstall ssh, Restore the file using the steps below.
* Use rpm -qf /etc/init.d/sshd This command shows that the file comes from the openssh-server-5.3p1-84.1.el6.x86_64 Package.
* Download the Openssh rpm from it's source
* Copy openssh-server-5.3p1-84.1.el6.x86_64 package file to /tmp directory or any other directory of your choice.
* Use rpm2cpio |cpio -idmv to extract the package.
The command you used in the above step created a few subdirectories in /tmp. You can now copy it to its original location.