6

I have just made a huge mistake by changing the owner of my /usr/bin from root to an ordinary user. Whenever i try to execute $sudo chown root /usr/bin this gives me :

chown: changing ownership of ‘/usr/bin/’: Operation not permitted

I have read many topics talking about this issue, which most of them give a solution in case you have already a backup image of your OS. Unfortunately I don't have any backup. Is there any solution then other than reinstalling Ubuntu from scratch.

user-x220
  • 151
  • 1
  • 3
  • 9
  • 1
    Did you change ownership recursively? If so you have most likely changed the owner of the sudo binary, so it can't gain root privileges any more. In that case you need to use plain "su" to become root and then change it back, this only works if the root account has a password (that you know). Otherwise you need to reboot the system into a root shell, e.g. by editing the boot command line to include "init=/bin/sh" and change it back from that shell. – ThomasH May 08 '16 at 17:11
  • Yes that's the big problem, i have executed a chown -R on /usr/bin – user-x220 May 08 '16 at 17:12
  • See above. I added to the comment after accidentally submitting it by pressing return :) – ThomasH May 08 '16 at 17:13
  • Plain "su" lives in /bin so it wouldn't have been affected by your change. If you can get root, "chown -R root /usr/bin" should undo most of the damage done. – ThomasH May 08 '16 at 17:14
  • Thanks a lot, i have actually tried the su trick but it asks me always to give some password that I haven't set the first time while i did install the OS on my machine, what should i do in this case please? – user-x220 May 08 '16 at 17:15
  • sudo su, then passwd? If the old pass is random, you might be able to google a solution (mounting the disk rw from grub etc) – Jonas G. Drange May 08 '16 at 17:17
  • sudo command is not working anymore, all i get is this message "sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set" – user-x220 May 08 '16 at 17:18

2 Answers2

15

Solution:- Get in to Ubuntu Recovery Console Start your computer and press and hold SHIFT key while booting. It will take you to the grub loader page as shown in image – 1.

Image 1

Image 1

Select and enter Advanced options for Ubuntu and from there select the kernel named as recovery mode as shown in image – 2.

Image 2

Image 2

select root – drop to root shell prompt as shown in image – 3

Image 3

Image 3

Now the file system is read only to Remount to Read Write run below command

# mount -o remount,rw /

mount –all

then need to change the ownership for sudo

# chown root:root /usr/bin/sudo

give permisson for sudo

# chmod 4755 /usr/bin/sudo

it’s done … let’s see by restarting the machine

# shutdown -r now

You should have your Sudo back by now....

Ibrahim
  • 301
  • 2
  • 10
4

If you can't gain root with plain "su" because you don't know the password or none has been set, then you have to reboot into a root shell. When you see the GRUB boot menu, press "e" to edit the kernel command lines, and append "init=/bin/sh" - then it will dump you into a single-user root shell instead of the normal boot process. Here you may have to remount the root file system read/write:

# mount / -n -w -o remount

Then you need to undo the damage from earlier:

# chown -R root /usr/bin

Then finally remount the file system read-only, sync and reboot:

# mount / -n -r -o remount
# sync
# reboot -f
ThomasH
  • 1,085
  • 10
  • 12
  • Thanks a lot @ThomasH for your detailed solution. I will try it and give you a feed-back. – user-x220 May 08 '16 at 17:21
  • The trick with booting into single user root shell is shown in this video: https://www.youtube.com/watch?v=PlA1vmEn3UY It may be helpful if you're having problems getting it to work. Good luck :) – ThomasH May 08 '16 at 17:23
  • Thanks you so much that helped me a lot, i really appreciate your help. I could finally fix the problem using also this useful topic https://stackoverflow.com/questions/16682297/getting-message-sudo-must-be-setuid-root-but-sudo-is-already-owned-by-root – user-x220 May 08 '16 at 18:23
  • Great, remember to accept the answer if your problem got solved! – ThomasH May 08 '16 at 22:24