Wednesday, December 21, 2011

How to reset the permissions of installed RPM packages ?

Situation:  
One of our Developer has changed the permission of all the files under /etc to 777 by inadvertently executing the chmod command with 777 permission. Due to this, the system went into non-usable state.

Resolution: 
The Linux 'rpm' command  comes with an interesting switch known as '--setperms' using which we can restore the file-permissions of either one or more packages. 

Syntax:  
# rpm --setperms [package name]

To reset one specific RPM package:  
# for pkg in $(rpm -qa | grep -w pkgname); do rpm --setperms $pkg; done
Example:  
To reset the 'zip' package.
# rpm -qa | grep zip                           <-- Check to see if the package exists
# for pkg in $(rpm -qa | grep -w zip); do rpm --setperms $pkg; done

To reset the entire packages installed ( applicable in the situation I mentioned above):
# for allpkg in $(rpm -qa); do rpm --setperms $allpkg; done

Note:  To restore the Group permission, we need to use the switch "--setugids".

2 comments:

  1. hi..
    Thx for your command.
    if we give chmod -R 777 /etc then ur command will not change file permission for all the content in /etc/..

    It only change the default permission for /etc/ only.

    ReplyDelete
  2. @Guruprasanna, this command set can be used only to bring the system back to its default permission level, if someone mistakenly executed the chmod or chown commands in such a way that it impacts the functionality of the server. These are kind of System recovery commands.

    ReplyDelete