Linux: Restore Package Permissions

Linux: Restore Package Permissions

Sometimes it can happen to us that using the chmod or chown commands , we configure wrong permissions and owners on directories and files.

If the directories and files affected by the change correspond to a service, the service may stop working correctly.

If we have a distribution based on RPM packages (Red Hat Package Manager), such as: RedHat, CentOS, Fedora, etc …, we can reestablish the permissions and owner of a package automatically.  

Linux: Restore permissions and owner of the files of an RPM package manually:

The manual procedure consists of listing the files inside the package in detailed format and then setting the permissions and owner.

In this case, we will use a distribution based on RPM packages.

Let’s see a step by step example:

1) We list all the installed packages called php:

We use the modifier -q (query) and the -a, (all).

[root @ SYSADMIT1 ~] # rpm -qa php
php-5.3.3-40.el6_6.x86_64

2) We obtain the list of files of the packages:

We use the modifier -q (query) and the -l, (list).

[root @ SYSADMIT1 ~] # rpm -ql php-5.3.3-40.el6_6.x86_64
/etc/httpd/conf.d/php.conf
/usr/lib64/httpd/modules/libphp5.so
/ var / lib / php / session
/var/www/icons/php.gif

3) We obtain the list of package files, in detailed format.

We use the -q (query) modifier, the -l, (list) and the -v (verbose).

[root @ SYSADMIT1 ~] # rpm -qlv php-5.3.3-40.el6_6.x86_64
-rw-r – r– 1 root root 674 Oct 30 2014 /etc/httpd/conf.d/php.conf
– rwxr-xr-x 1 root root 3699056 Oct 30 2014
/usr/lib64/httpd/modules/libphp5.so drwxrwx — 2 root apache 0 Oct 30 2014 / var / lib / php / session
-rw-r – r – 1 root root 2523 Oct 30 2014 /var/www/icons/php.gif

Then we could set the permissions manually with chmod and chown .

Following this example, we would execute:

chown root: root /etc/httpd/conf.d/php.conf
chown root: root /usr/lib64/httpd/modules/libphp5.so
chown root: apache / var / lib / php / session
chown root: root / var /www/icons/php.gif

chmod u = rw, g = r, o = r /etc/httpd/conf.d/php.conf
chmod u = rwx, g = rx, o = rx / usr / lib64 / httpd /modules/libphp5.so
chmod u = rwx, g = rwx, o = / var / lib / php / session
chmod u = rw, g = r, o = r /var/www/icons/php.gif


Linux: Restore permissions and owner of the files of an automatic RPM package:

To restore permissions and owner of the files of an automatic package, we can use the rpm command with the –setperms and –setugids modifiers.

To reset the permissions of a package:

rpm –setperms package-name

To reset the owner (user and group) of a package:

rpm –setugids package -name

To reset the permissions of all packages:

rpm –setperms -a

To reset the owner (user and group) of all packages:

rpm –setugids -a

If –setperms does not allow us the -a parameter, to reset the permissions of all packages:

for x in $ (rpm -qa); do rpm – setperms $ x; done

If –setugids does not allow the -a parameter, to reset the owner (user and group) of all packages:

for x in $ (rpm -qa); do rpm – setugids $ x; done

You may also like...