How To Restore Default Permissions Of All Files Under / (ROOT)

by admin on November 20, 2014

Have you ever made a mistake and changed permissions of the folder and all it’s subfolders? And how to change back permissions of files/folders to default state ?

Yesterday , I accidentally changed / (ROOT) permission to 777 with following command

# chmod 777 -R /

After that, All files and folders on my server changed to 777 permissions. And I try to reboot my server, I can’t login into my server. It’s shown error:

Failed to start SSH server : Starting sshd:WARNING: UNPROTECTED PRIVATE KEY FILE! Permissions 0775 for '/etc/ssh/ssh_host_key' are too open. It is recommended that your private key files are NOT accessible by others. This private key will be ignored. bad permissions: ignore key: /etc/ssh/ssh_host_key Could not load host key: /etc/ssh/ssh_host_key

How do I fix it ?

If you can boot your system into Single User Mode, then type 2 commands

# 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.

If you can’t boot into Single User Mode, Let boot from CentOS installation CD into Recuse Mode, make sure that your system mounted into /mnt/sysimage, And type the following command

# chmod 7777 -R /mnt/sysimage/

Note: Permission is 7777 not 777
To change all files and folder to “7777” permission. And reboot your system. After that, You can boot into Single User Mode and type above commands.

Reset default permissions of all files and folders under /home/

Type the following commands

# cd /home/
# for p in $(ll); do chown $p.$p -R /home/$p; done
# find . -type d -print0 | xargs -0 chmod 755
# find . -type f -print0 | xargs -0 chmod 644
# chmod 700 *

Reset default permissions of all files and folders of MySQL (/var/lib/mysql)

Type the following commands

# chown mysql.mysql -R /var/lib/mysql
# cd /var/lib/mysql
# find . -type d -print0 | xargs -0 chmod 700
# find . -type f -print0 | xargs -0 chmod 660
# chmod 777 *.sock

Finally, restart MySQL with following command

# /etc/init.d/mysqld restart

Related Posts:

Leave a Comment

Previous post:

Next post: