RPM is RedHat Package Manager, used to install/remove/update and query the packages in Red Hat based linux. The following example demonstrates the use of rpm query feature and shows different ways you can query rpm database and restore configuration file.
1. To query the whole RPM database
# rpm -qa
2. Identify the specific package by using grep
# 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. Querying 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. Query various files this package
# 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. List only the configuration files
# rpm -qc openssh-server-5.3p1-84.1.el6.x86_64
6. To list only documentation files
# 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
If for some reason a file has been damaged or got deleted from the 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.