0

A colleague of mine told me that after he executed the below command, he's not able to login to the system.

  • No user can login via ssh.

  • Only normal users can login over physical console of the system, but no administrative commands are allowed, not even

    # sudo su -
    

And the culprit command which did the damage was

# chmod -R 777 /

My question, of course is, how to deal with such a system? Is there a way to restore the default permissions on a Linux system and what's best way to handle such situations?

Sollosa
  • 1,929
  • 4
  • 20
  • 38
  • 1
    "what's best way to handle such situations?" Captain's Mast and a yard-arm. Fixes that problem every single time. – Warren Young Apr 29 '16 at 04:16
  • 1
    short answer: unfixable. but here's a dirty hack that can partially do the job on debian systems http://unix.stackexchange.com/a/49978/7696 - if you use a different unix or distro you may be able to adapt some of the ideas. or it's probably easier/better to just backup your data & config files and re-install from scratch. – cas Apr 29 '16 at 05:09
  • 1
    BTW, ssh is disallowed because the chmod -R 777 messed up the perms of all .ssh directories (including root's) and the files in them. ssh is very fussy about perms. If you want to login via ssh in order to attempt fixing this, the first thing you need to do is fix the home dir perms (/home/user/ and /root/) and the ~/.ssh/ perms. Also the perms for / and /home. – cas Apr 29 '16 at 05:13
  • Can you restore from backups? – Mark Plotnick Apr 29 '16 at 08:42
  • @cas thanks, I really understand the consequences of the command. Not to surprise you, It was new thing for me. I tried it on a machine as well, luckily was virtual, but faced same situation. – Sollosa Apr 29 '16 at 11:12
  • @cas there is are backup files, but I'm not sure which folders to be replaced. Is there a way that we could sync the permissions of one system with the affected one? – Sollosa Apr 29 '16 at 11:14
  • @MarkPlotnick thanks Mark! there are backup files, but I'm not sure which folders to be replaced. Is there a way that we could sync the permissions of one system with the affected one? – Sollosa Apr 29 '16 at 11:15
  • 2
    If you can restore the backups to a new filesystem, and mount both the correct and incorrect filesystems on the same computer, you can use find to walk through the file tree and run a bunch of chmod commands, using the --reference option of chmod to copy the permissions. I'll see if there's an example online. – Mark Plotnick Apr 29 '16 at 11:32
  • @MarkPlotnick that's much better I think, but do I have to replace every folder one by one or can you suggest a command that would hold reference to all files and folders on newlyinstalled system to apply on the oldsystem? I mean it would be nice, and thanks again Mark! – Sollosa Apr 29 '16 at 11:36
  • @Sollosa I once saw a shell script here (can't find it atm) that would scan a file tree (that had the correct permissions) and produce a shell script containing chmod commands to fix up a similar tree that had incorrect permissions. – Mark Plotnick Apr 29 '16 at 14:25
  • @MarkPlotnick that would be helpful though, thanks anyhow – Sollosa Apr 30 '16 at 19:20
  • 1
    Found it on serverfault. The answers there should fix things up: Copy permissions to identical tree on linux / unix – Mark Plotnick Apr 30 '16 at 20:57

1 Answers1

1

If you can't login or su to a root user, the two remaining options are

  1. Boot from rescue media, and repair the damage
  2. Boot from installer, reinstall the system, then restore data from backups
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
Toby Speight
  • 8,678