Laurent Mesguen
Laurent Mesguen

Reputation: 394

How to fix permissions after chmod -r 777 /?

I made a very annoying mistake in a script... A line supposed to change permissions recursively on a folder :

chmod -R 777 $folder/

The thing is the variable was not set, so it ran instead :

chmod -R 777 /

Yes stupid. The problem is now I can't use sudo anymore, I get this message :

sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set
  1. Is there any way to restore the permissions ?
  2. If not, is there any way to backup my Ubuntu 14.04 (running on VirtualBox) ?
  3. If not, how can I save important data ?

Upvotes: 1

Views: 12731

Answers (3)

user3184564
user3184564

Reputation: 81

I did the same mistake by accident and with these steps my problem was solved.

I was logged out from root and I couldn't run commands with root permissions, and I was working on debian 10.

1-Start or re-start your computer and press ESC(in some machines, press and hold Shift key) to show the Grub boot menu.

2-Choose "Advanced Options for..." from menu and boot the kernel entry with(recovery mode)

3-It gives you a root shell if you have your root password, so you can run these commands

chmod -R 755 /bin /boot /dev /etc/ /home /lib /lib64 /media /mnt /opt /run /sbin /srv /usr /var

chmod -R 777 /initrd.img /vmlinuz

chmod -R 1777 /tmp

chmod -R 555 /sys

chmod -R 555 /proc

chmod -R 700 /root

4-If you get an error when you want to log in as root user, you can run chmod +s /bin/su and check the result with ls -lt /bin/su
the result should be like this -rwsr-sr-x instead of -rwxr-sr-x

Upvotes: 0

chukwuka mark
chukwuka mark

Reputation: 164

I had same issue and this is how I fixed it here basically the content from the link is the default file permission for Ubuntu 16.04 so i just created a bash script copied all the content there edited the file like replace user:user to you specific user and also /home/user to the specific user name

cheers!

Upvotes: 1

Laurent Mesguen
Laurent Mesguen

Reputation: 394

I've finally found a solution!

Issue explained

When I changed recursively the permissions on the root folder / I also changed the permissions for /usr/bin/sudo. To see its permissions I typed :

ls -la /usr/bin/sudo

It gives me :

-rwxrwxrwx 1 root root 127668 2016-05-11 12:01 /usr/bin/sudo

Instead of :

-rwsr-xr-x 1 root root 127668 2016-05-11 12:01 /usr/bin/sudo

Here the s of -rwsr-xr-x is important because it gives temporary permissions to a user to run sudo with the permissions of the file owner (i.e root in this case) rather that the user who runs it.

Take a look on this article for further information : http://www.linuxnix.com/suid-set-suid-linuxunix/

A solution would have been to change the permissions on /usr/bin/sudo :

chmod 4755 /usr/bin/sudo

But I need to be root to change the permissions... Well, fortunately I was running Ubuntu on a VM.

Solution

A solution is to create a new virtual machine. Once it's done, on my new virtual machine mount my broken ubuntu .vdi with VirtualBox. Settings --> Storage --> Add a hard drive. Make sure the first hard drive is your new virtual machine so that it boots on the new virtual machine.

Once it's done, you can change sudo permissions on the hard drive mounted (your broken ubuntu) :

sudo chmod 4755 /mnt/XXXXX/usr/bin/sudo 

You can now run your fixed virtual machine with a working sudo...

Upvotes: 3

Related Questions