Share, , Google Plus, Pinterest,

Print

Posted in:

Reset File Permissions on CentOS 6

Wondering how you could reset file permissions? Have you ever made a mistake and changed user and group ownership/permissions of the folder and all it’s subfolders? Well … it happens! 🙂 Read this post and learn how to reset file permissions to default. By default, we mean the permissions set, when RPM package was installed – set the default RPM installation permissions of files and folders.

Reset File Permissions
Reset File Permissions

Let’s Reset File Permissions to default!

 

SITUATION:

Let’s say, we made a mistake and chmod 777 whole /var directory. This can cause quite some problems to linux services as we can see below:

[root@foo1 ~]# ll /
total 92
dr-xr-xr-x.  2 root root  4096 Nov  8 10:00 bin
dr-xr-xr-x.  5 root root  3072 Nov  8 10:02 boot
drwxr-xr-x  19 root root  3580 Nov 29 11:07 dev
drwxr-xr-x. 60 root root  4096 Nov 29 11:07 etc
drwxr-xr-x.  2 root root  4096 Sep 23  2011 home
dr-xr-xr-x. 15 root root 12288 Nov  8 10:00 lib
drwx------.  2 root root 16384 May  8  2013 lost+found
drwxr-xr-x.  2 root root  4096 Sep 23  2011 media
drwxr-xr-x.  2 root root  4096 Sep 23  2011 mnt
drwxr-xr-x.  2 root root  4096 Sep 23  2011 opt
dr-xr-xr-x  75 root root     0 Nov 29 11:07 proc
dr-xr-x---.  2 root root  4096 Nov 29 11:18 root
dr-xr-xr-x.  2 root root 12288 Nov  8 10:00 sbin
drwxr-xr-x.  2 root root  4096 May  8  2013 selinux
drwxr-xr-x.  2 root root  4096 Sep 23  2011 srv
drwxr-xr-x  13 root root     0 Nov 29 11:07 sys
drwxrwxrwt.  3 root root  4096 Nov 29 11:07 tmp
drwxr-xr-x. 12 root root  4096 May  8  2013 usr
drwxrwxrwx. 17 root root  4096 May  8  2013 var

[root@foo1 ~]# ll /var/
total 60
drwxrwxrwx.  4 root root 4096 May  8  2013 cache
drwxrwxrwx.  3 root root 4096 May  8  2013 db
drwxrwxrwx.  3 root root 4096 May  8  2013 empty
drwxrwxrwx.  2 root root 4096 Sep 23  2011 games
drwxrwxrwx. 15 root root 4096 May  8  2013 lib
drwxrwxrwx.  2 root root 4096 Sep 23  2011 local
drwxrwxrwx.  5 root lock 4096 May  8  2013 lock
drwxrwxrwx.  3 root root 4096 Nov 29 11:07 log
lrwxrwxrwx.  1 root root   10 May  8  2013 mail -> spool/mail
drwxrwxrwx.  2 root root 4096 Sep 23  2011 nis
drwxrwxrwx.  2 root root 4096 Sep 23  2011 opt
drwxrwxrwx.  2 root root 4096 Sep 23  2011 preserve
drwxrwxrwx. 11 root root 4096 Nov 29 11:07 run
drwxrwxrwx.  8 root root 4096 May  8  2013 spool
drwxrwxrwx.  2 root root 4096 Nov  8 10:02 tmp
drwxrwxrwx.  2 root root 4096 Sep 23  2011 yp

[root@foo1 ~]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd: /var/empty/sshd must be owned by root and not group or world-writable.
                                                           [FAILED]

FIX:

We can use

  • rpm –setperms PACKAGENAME
  • rpm –setugids PACKAGENAME

commands to reset file permissions to it’s default state.

Since we need to reset file permissions on the whole /var directory, we will run the following commands on all of the installed RPM packages:

for package in $(rpm -qa); do rpm --setperms $package; done
for package in $(rpm -qa); do rpm --setugids $package; done

 

  • Let’s see a practical example – Here is how to reset file permissions:
[root@foo2 ~]# for package in $(rpm -qa); do rpm --setperms $package; done
.......
chmod: cannot access `/etc/modprobe.d/local.conf': No such file or directory
chmod: cannot access `/etc/dev.d': No such file or directory
chmod: cannot access `/etc/scsi_id.config': No such file or directory
chmod: cannot access `/etc/udev/devices': No such file or directory
chmod: cannot access `/etc/udev/scripts': No such file or directory
..........
[root@foo2 ~]#

When you run this oneliner you might see a lot of “No such file or directory” errors, ignore it. When it is finished, all file permissions should be set to default!

  • Do you need to reset the file and folder ownerships to default too? Here is how to do it:
[root@foo2 ~]# for package in $(rpm -qa); do rpm --setugids $package; done
.......
chown: cannot access `/mnt/cdrom': No such file or directory
chgrp: cannot access `/mnt/cdrom': No such file or directory
chown: cannot access `/mnt/floppy': No such file or directory
chgrp: cannot access `/mnt/floppy': No such file or directory
.......
[root@foo2 ~]#

Voila! We have succesfully reset file permissions and ownership to default value!

Share, , Google Plus, Pinterest,

9 Comments

Leave a Reply