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!

  • Jose G.

    You save my day, thanks a lot =)

    • Mitch

      Nice Jose! ūüôā

  • PGiZ

    Thank you. This post save my life. ūüėÄ

  • Gustavo Adolfo Zuluaga C.

    Thanks a lot! Save my life :p

    • Mitch

      As intended ūüôā My work here is done! Regards, Mitch

      • Pandy Pandiyan

        hi mitch..thnkz..but still now cannot access the dir

        • Mitch

          Hi Pandy! What do you have in /mnt/packages directory? Why do you want to access it? The /mnt/packages is not mentioned anywhere in this post. By default /mnt dir is used to mount media to, so it is not usual to keep other files and folders in /mnt. Since you are looking for packages folder in /mnt i presume you should mount CentOS 6 ISO to /mnt? Regards, Mitch

  • Pandy Pandiyan

    hi friend ..i can’t get into the /mnt/packages. when i entered this it will show me the no such file or dir…pls give some tips body

    • Mitch

      Hi Pandy, i am not sure why you would need to get into the /mnt/packages? If you want to reset file permissions of all of the installed RPM packages on the system run “for package in $(rpm -qa); do rpm –setperms $package; done” and to set the UIDs and GIDs run “for package in $(rpm -qa); do rpm –setugids $package; done”. Regards, Mitch

  • PowerLeads, Inc.

    Holy smokes lifesaver!

  • Havoc NME

    Hi Mitch,
    I gave everyone access to my personal folder and want to remove/reset access, how do I do that without causing havoc.
    example: /home/username

    • Mitch

      Hi Havoc, only you should have access to your home folder, so this is easily changed by running “chmod 700 /home/username” as user “username”. This way no other user will be able to access files in /home/username files. Regards, Mitch

  • Shuhail Alam

    While trying to set permission, I entered chmod -R 0777 / by mistake. thats why all permission changed. My master user name also lost the sudoer power.

    After following your instruction, i just got operation not permitted & no such file in the directory error. and nothing else.

    still same problem. effective uid is not 0, is sudo installed setuid root? Please help me solve the issue.

    • Mitch

      Hi Shuhail, you should boot your system in single mode therefore getting root access over the system and resetting all file permissions. You can stop the system from booting and force single user mode – read more here https://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-rescuemode-booting-single.html. Hope this helps.
      Regards, Mitch

      • Shuhail Alam

        Thanks for the reply. But I am using cloud server from godaddy. and I don’t have any access to single user boot. is there any other way? and one more thing, if I create another server from my earlier backup, how much changes do I need to do?

        • Mitch

          Are you using VPS? If so u can still go to single user mode. If you restore from backup it depends on when you’ve backed the server up.