0

I managed to messed up server permissions on entire server

chown root:root -R /
chmod 777 -R /

How to fix it? I can't login as root using terminal on that machine it show error "Invalid password" and SSH shows error "connection reset by remote server"

Note: I try to reinstall CentOS 7 using USB. but got stuck during Select disk stage as it will not let me select / without formating it. This question explains what my commands did. Why is "chmod -R 777 /" destructive?

Faraz
  • 183
  • 1
  • 2
  • 10
  • 1
    see http://serverfault.com/questions/40025/how-to-restore-remote-access-to-a-rhel-system-from-expanded-permissions-set-over – Federico Sierra May 06 '16 at 15:12
  • 2
    You can still boot from a rescue image and run for instance `rpm -a --setperms` which will restore whatever original permissions were included in your RPM packages, but not everything (not every file/directory that gets installed from a package was included as a file, things get generated in the script sections of RPM's as well). Also everything that wasn't installed from rpm packages, local data, user data, home directories etc. will still be screwed up, so **restoring from back-up is the only answer**. – HBruijn May 06 '16 at 15:15
  • @FedericoSierra I did check that question, My issue is I can't login using Terminal I think i messed the root user permissions as well. – Faraz May 06 '16 at 15:22

2 Answers2

4

Restore from backup or rebuild the server.

Safado
  • 4,786
  • 7
  • 37
  • 54
  • Its my local dev server no backup.. and I don't want to spend hours re-installing it. – Faraz May 06 '16 at 15:03
  • I try to re-install CentOS 7 but I can't select root / in install process as its forcing me to format root to install on it. So i just can't install new CentOS on old disk and get it fix.. – Faraz May 06 '16 at 15:05
  • 1
    Well... ¯\\_(ツ)_/¯ There is no undo. Nor are there any full system permission templates that you could apply. So aside from having an already identical system in which you could pull all the ownshership information from and create a script to reapply that to your own server (assuming you can get logged in again), then starting from scratch is probably your quickest bet – Safado May 06 '16 at 15:07
  • That is ultimate solution, But I want to try my luck till Monday.. – Faraz May 06 '16 at 15:24
  • 1
    yum reinstall might fix permissions for files included in installeed packages – ptman May 06 '16 at 15:32
4

I managed to Solve it, here are the steps i followed.

To fix the issue of root login using terminal.

  1. boot using bootable USB/CD
  2. select recover and mound disk
  3. go to mounted folder and run chown root -R / and chmod 755 -R /
  4. chmod 0700 -R /root/.ssh
  5. reboot normally
  6. Ignore SElinux contexts warnings and let it re-store contexts

After this you will be able to login to machine using root

To fix file permission and owners info use these commands rpm --setperms {packagename} and rpm --setugids {packagename}

To fix you all system just rum them in loop more details here

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

It will fix permissions for most of packages.

If you can't login using SSH follow these steps

  1. systemctl restart sshd It will fail
  2. systemctl status sshd It will show you the file with wrong permissions
  3. Fix the file permission using chmod 0700 -R /root/.ssh OR chmod 0700 your_file_path
  4. repeat this process until SSH service start successfully

Using these steps I managed to restore all essential services on my server.

Finally to restore default permissions for vhosts created using Plesk. I use this command as described on plesk website

# /usr/local/psa/bin/repair --restore-vhosts-permissions
Faraz
  • 183
  • 1
  • 2
  • 10