Ubuntu – Accidental changing of permissions to run sudo, leading to its inability to function

permissionssudo

I ran sudo chmod 777 -R /usr/bin as I had a few programs there I wanted to be able to run with more ease, without continuously entering my root password but now /usr/bin/sudo keeps giving me an error whenever I run sudo. Namely this error:

sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set

and I am stuck for how to fix this.

Best Answer

The following procedure can also be performed from booting to Recovery Mode choosing Root. Then after booting into root, type in the following to mount the drive in Read / Write so changes can be made:

mount -o remount,rw /

I would recommend following the first answer on this site to boot to a Live CD so that you can mount your hard drive and repair your file(s). Stop after hitting step #5 so that your hard drive is mounted as a root user.

How can I repair grub? (How to get Ubuntu back after installing Windows?)


after the drive is mounted as a root user, you should be able to type in from the terminal window:

chmod 4755 /usr/bin/sudo

to set the "suid" back for the sudo file.

The following list is all the files in /usr that should have the "suid" set: (I will leave them as a list that you can copy and paste as need be)

chmod 4755 /usr/bin/pkexec
chmod 4755 /usr/bin/sudo
chmod 4755 /usr/bin/mtr
chmod 4755 /usr/bin/traceroute6.iputils
chmod 4755 /usr/bin/gpasswd
chmod 4755 /usr/bin/lppasswd
chmod 4755 /usr/bin/passwd
chmod 4755 /usr/bin/newgrp
chmod 4755 /usr/bin/chsh
chmod 4755 /usr/bin/chfn

EDIT 2:

The following files have permissions of rwxr-sr-x and can be reset by copy and pasting the following into a terminal after getting the host back up: (some might be different then your system)

sudo chmod 2755 /usr/bin/mail-unlock
sudo chmod 2755 /usr/bin/dotlockfile
sudo chmod 2755 /usr/bin/ssh-agent
sudo chmod 2755 /usr/bin/wall
sudo chmod 2755 /usr/bin/dotlock.mailutils
sudo chmod 2755 /usr/bin/chage
sudo chmod 2755 /usr/bin/bsd-write
sudo chmod 2755 /usr/bin/crontab
sudo chmod 2755 /usr/bin/expiry
sudo chmod 2755 /usr/bin/mlocate
sudo chmod 2755 /usr/bin/mail-touchlock
sudo chmod 2755 /usr/bin/mail-lock

Since all you ran was chmod 777 -R /usr/bin and there are no sub directories in that directory, all the files except for the links and the obvious ones above in the list with the "suid" permission set on them, the rest of the files in that directory have permission settings of -rwxr-xr-x (or 755). You could reset those permissions to those files without touching the "suid" files or the links by typing in:

sudo chmod go-w /usr/bin/*

that will set the group and other permissions to r-x and leave the links alone as lrwxrwxrwx. DO NOT RUN AS chmod 755 as that will mess up the permissions we have fixed!

EDIT: changed to the "suid" as was pointed out to me.

Related Question