Search My Blog

Monday, July 12, 2010

How to: Linux reset the permissions of the installed rpm packages with –setperms option

How to: Linux reset the permissions of the installed rpm packages with –setperms option

by Vivek Gite · 9 comments

Sometime by mistakes all file permissions get changed and you need to restore file permission. For example a shell script or some sort of corruption could change the permissions for packages (installed files), it may be necessary to reset them.

For example a long time ago my shell script run chmod and chown commands on /usr and changed the permission. Luckily rpm command can reset package permission. Sun Solaris pkg command and IBM can also reset permissions.

Please note that this troubleshooting tip is about resetting the permission of the installed package files and not about end users files stored in /home directory.

RPM syntax to fix permission

To set permissions of files in a package, enter:

rpm --setperms {packagename}

RPM syntax to fix file ownership

To set user/group ownership of files in a package, enter:

rpm --setugids {packagename}

List installed package

You can list all installed package with rpm -qa command:
rpm -qa
Output:

basesystem-8.0-5.1.1.el5.centos glibc-2.5-12 expat-1.95.8-8.2.1 db4-4.3.29-9.fc6 cyrus-sasl-lib-2.1.22-4 libusb-0.1.12-5.1 libgcrypt-1.2.3-1 perl-5.8.8-10 gmp-4.1.4-10.el5 perl-DBI-1.52-1.fc6 perl-URI-1.35-3 wireless-tools-28-2.el5 libXdmcp-1.0.1-2.1 perl-IO-Zlib-1.04-4.2.1 perl-String-CRC32-1.4-2.fc6 perl-HTML-Tagset-3.10-2.1.1 libattr-devel-2.4.32-1.1 zip-2.31-1.2.2 ..... .. ... 

List individual package file permission

You can list individual installed package file permission using following shell for loop (for example list file permission for zip package):
for f in $(rpm -ql zip); do ls -l $f; done
Output:

-rwxr-xr-x 1 root root 75308 Jan  9  2007 /usr/bin/zip -rwxr-xr-x 1 root root 31264 Jan  9  2007 /usr/bin/zipcloak -rwxr-xr-x 1 root root 28336 Jan  9  2007 /usr/bin/zipnote -rwxr-xr-x 1 root root 30608 Jan  9  2007 /usr/bin/zipsplit total 188 -rw-r--r-- 1 root root  3395 Dec 14  1996 algorith.txt -rw-r--r-- 1 root root   356 Dec 14  1996 BUGS -rw-r--r-- 1 root root 60168 Mar  9  2005 CHANGES -rw-r--r-- 1 root root  2692 Apr 10  2000 LICENSE -rw-r--r-- 1 root root 40079 Feb 28  2005 MANUAL -rw-r--r-- 1 root root  8059 Feb 27  2005 README -rw-r--r-- 1 root root  3149 Feb 21  2005 TODO -rw-r--r-- 1 root root  2000 Mar  9  2005 WHATSNEW -rw-r--r-- 1 root root 19032 Apr 19  2000 WHERE -rw-r--r-- 1 root root 356 Dec 14  1996 /usr/share/doc/zip-2.31/BUGS -rw-r--r-- 1 root root 60168 Mar  9  2005 /usr/share/doc/zip-2.31/CHANGES -rw-r--r-- 1 root root 2692 Apr 10  2000 /usr/share/doc/zip-2.31/LICENSE -rw-r--r-- 1 root root 40079 Feb 28  2005 /usr/share/doc/zip-2.31/MANUAL -rw-r--r-- 1 root root 8059 Feb 27  2005 /usr/share/doc/zip-2.31/README -rw-r--r-- 1 root root 3149 Feb 21  2005 /usr/share/doc/zip-2.31/TODO -rw-r--r-- 1 root root 2000 Mar  9  2005 /usr/share/doc/zip-2.31/WHATSNEW -rw-r--r-- 1 root root 19032 Apr 19  2000 /usr/share/doc/zip-2.31/WHERE -rw-r--r-- 1 root root 3395 Dec 14  1996 /usr/share/doc/zip-2.31/algorith.txt -rw-r--r-- 1 root root 12854 Jan  9  2007 /usr/share/man/man1/zip.1.gz

Reset the permissions of the all installed RPM packages

You need to use combination of rpm and a shell for loop command as follows:
for p in $(rpm -qa); do rpm --setperms $p; done
for p in $(rpm -qa); do rpm --setugids $p; done

Above command combination will reset all the permissions to the default permissions under CentOS / RHEL / Fedora Linux.

A note about Debian / Ubuntu Linux distributions

Only rpm command / Solaris pkg and AIX command supports package file permission reset option. But dpkg / apt-get command doesn't support this option.

Solaris command example

Boot Solaris / OpenSolaris box in single user mode. Mount /usr and other filesystem:
mount / /a
mount /usr /a/usr
mount /var/ /a/var
mount /opt /a/opt

Login as the root, enter:
pkgchk -R /a -f
Please note that he pkgchk command does not restore setuid, setgid, and sticky bits. These must be set manually. Read pkgchk command man page for more information:
man pkgchk

Featured Articles:

4000+ howtos and counting! Want to read more Linux / UNIX howtos, tips and tricks? We request you to sign up for the Daily email newsletter or Weekly newsletter and get intimated about our new howtos / faqs as soon as it is released.

Go there...
http://www.cyberciti.biz/tips/reset-rhel-centos-fedora-package-file-permission.html

Don

No comments: