1

I was trying to run chmod on some files and accidentally specified the wrong directory. Now, sudo doesn't work, and I rebooted. I can log in, but I can't do anything as root. How can I fix this?

This is different than Set myself as owner of /etc with chown command now getting all kinds of errors as this describes what to do if you can't boot your system. It also addresses problems that may be encountered if you ran chmod on another directory or just accidentally specified /, like I did. It also explains how to fix sudo when/if it gets broken.

  • It would help if you divide it into a question and an answer. This isn't a forum. –  Jan 17 '18 at 13:13
  • 2
    it would be better to post the fix as an answer to your question then accept it. That way people searching will see that it has an answer. – ravery Jan 17 '18 at 13:14
  • 1
    The duplicate itself is closed as a duplicate, which you should also see. You should also see take some time to read terdon's excellent answer before blindly stating your question is different. – muru Jan 17 '18 at 14:10

1 Answers1

0

Here's how to fix the problem WITHOUT reinstalling.

  1. Find Your Operating System Version

    If You CAN Boot Your System And Log In

    Open up a Terminal (Ctrl+Alt+T) and type lsb_release -a. Look at the 'Description' field. Write down what is in this field.

    If You CANNOT Boot Your System Or Log In

    Power off your computer if it's not already off. Power it back on, and press and hold the 'Shift' key until you see a full screen menu.

    Use the arrow keys to highlight the Advanced Options for Ubuntu option. Press Enter. You will see a list of options. Pick the first one that has (recovery mode) at the end of it. Press Enter. Wait until a menu comes up.

    Use the arrow keys to highlight the root option. Press Enter twice. Type lsb_release -a.

    Notice: You may see 2 or 3 lines of text pop up on the screen mentioning something about a dependency. Just press Enter.

    Note the OS version in the description field. You will need this later.

    Press Ctrl+D. The menu with the root option will reappear. Select the resume option with your arrow keys and press Enter. At this point, power the system off.

  2. Fix sudo

    Boot into recovery mode and drop to a root prompt. Enter the following commands:

    mount -o remount /

    This remounts your boot drive in read-write mode. Without this, the next commands will fail.

    chown root:root /usr/bin/sudo && chmod 4755 /usr/bin/sudo

    chown root:root /usr/lib/sudo && chmod 4755 /usr/lib/sudo

    chown root:root /usr/lib/sudo/sudoers.so && chmod 4755 /usr/lib/sudo/sudoers.so

    chown root:root /etc/sudoers && chmod 4755 /etc/sudoers

    chown root:root /etc/sudoers.d && chmod 4755 /etc/sudoers.d

    chown root:root /etc/sudoers.d/README && chmod 4755 /etc/sudoers.d/README

    chown root:root /var/lib/sudo && chmod 4755 /var/lib/sudo

    Press Ctrl+D. Select the resume option with the arrow keys and press Enter. Power the system off.

  3. Download The OS And Put It On A DVD/Flash Drive

    Next, download the version of Ubuntu listed in that field. Using another version can result in this process failing. Put the OS on a DVD or a flash drive.

  4. Boot The Affected Computer Off Of The USB Drive/DVD

    Insert the disc or plug the flash drive into the chmod affected computer. Power the computer up and tell it to boot off of the USB drive or DVD. You may have to consult your computer manufacturer on how to do this. Let the computer boot. This may take a while, depending on the speed of your computer.

    When you see a menu pop up with 2 options, Try Ubuntu and Install Ubuntu, click the Try Ubuntu option. You should see a desktop. Open 2 file explorer windows. One should be at the root of the computer's boot drive, and the other should be at /home/<your_username>, on your computer's boot drive.

  5. Get The Permissions Of The DVD/USB Drive's OS

    Open up a Terminal (Ctrl+Alt+T). Type sudo su. Next, type the following commands.

    find /bin -depth -printf 'chmod %m %p\n' > bin

    find /boot -depth -printf 'chmod %m %p\n' > boot*

    find /dev -depth -printf 'chmod %m %p\n' > dev

    find /etc -depth -printf 'chmod %m %p\n' > etc

    find /lib -depth -printf 'chmod %m %p\n' > lib

    find /proc -depth -printf 'chmod %m %p\n' > proc

    You may see some errors here. You can safely ignore these.

    find /run -depth -printf 'chmod %m %p\n' > run

    You may see an error here. You can safely ignore it.

    find /sbin -depth -printf 'chmod %m %p\n' > sbin

    find /usr -depth -printf 'chmod %m %p\n' > usr

    find /var -depth -printf 'chmod %m %p\n' > var

    *Only if you cannot boot your system.

    Now, close the terminal. Open another file explorer window and navigate to /home/ubuntu. Xubuntu will use /home/xubuntu. Kubuntu will use /home/kubuntu. Follow this naming convention accordingly.

    You will see 9 or 10 files. They are named bin, boot*, dev, etc, lib, proc, run, sbin, usr, and var. Copy these to YOUR home folder.

  6. Applying The New Permissions

    If You CANNOT Boot The System And Log In

    Open another terminal. Type sudo su. Next, type fdisk -l. Look for your boot drive or your drive with /home mounted to it. It is usually the largest drive with the largest partition. In my case, it is /dev/sda1. Note this down.

    Next, type cd /. After that, type cd /mount. Then type ls. You should see one and only one directory. Type cd /<directory>. Next, type mkdir mount1. After that, type mount -t ext4 <disk here> /mount/<directory>/mount1, replacing <disk here> with the drive name you wrote down.

    Now, type cd /mount/<directory>/mount1. You are now in the root directory of the damaged boot drive. Type cd /home/<username>, replacing <username> with YOUR username. Type gedit boot. If it says that gedit is not installed, type mousepad boot. If it says that mousepad is not installed, replace mousepad with the name of your distribution's default text editor in ALL LOWERCASE. A window will pop up. Use the text editor's find and replace feature to replace /boot with /mount/<directory>/mount1/boot. Tell it to replace exact matches. Once you have done that, go to the very top of the file, press Enter at the beginning of the first line, go to the new blank line, and type #!/bin/sh. Click File, then Save As, and save it as boot.sh. Close the file and go back to the terminal.

    Type chmod +x boot.sh. No messages should pop up. Finally, type ./boot.sh. You may see some errors, you can safely ignore these.

    Follow the instructions in the next section.

    If You CAN Boot The System And Log In

    Reboot the computer. When it says to remove the installation media, do so. Let the computer reboot. You may see copious quantities of errors during the boot process. Ignore these. Open your /home directory and a terminal. Type sudo su. Type your password. Type the following commands:

    chmod 777 bin

    chmod 777 dev

    chmod 777 etc

    chmod 777 lib

    chmod 777 proc

    chmod 777 run

    chmod 777 sbin

    chmod 777 usr

    chmod 777 var

    BE CAREFUL when you run these commands. Make sure the terminal prompt says root@computername:/home/<username># when there's nothing typed in.

    Now, type the following commands:

    ./bin

    ./dev

    ./etc

    ./lib

    ./proc

    ./run

    ./sbin

    ./usr

    ^

    This is the longest one. In my case, the text file was 10.4 megabytes in size.

    ./var

    When running these commands, you will get OODLES of errors. Don't be worried by this. In fact, they may go by too fast for you to read. Wait for a prompt to return before issuing the next command.

    Your system will appear to have been completely trashed after this. Shortcuts will stop working and applications will stop opening. Exit the Terminal, save your work, and close all applications. Force your computer to power off. Power it back on. Let it boot back up and log in. Also, sudo will break again.

    Follow the next section to fix sudo.

  7. Fix sudo (Again)

    Boot back into recovery mode and drop to a root prompt. Type the following commands:

    mount -o remount /

    This remounts your boot drive in read-write mode. Without this, the next commands will fail.

    chown root:root /usr/bin/sudo && chmod 4755 /usr/bin/sudo

    chown root:root /usr/lib/sudo && chmod 4755 /usr/lib/sudo

    chown root:root /usr/lib/sudo/sudoers.so && chmod 4755 /usr/lib/sudo/sudoers.so

    chown root:root /etc/sudoers && chmod 4755 /etc/sudoers

    chown root:root /etc/sudoers.d && chmod 4755 /etc/sudoers.d

    chown root:root /etc/sudoers.d/README && chmod 4755 /etc/sudoers.d/README

    chown root:root /var/lib/sudo && chmod 4755 /var/lib/sudo

    Press Ctrl+D. Use your arrow keys to highlight the resume option. Press Enter. Let your system boot up. Log in and open a Terminal. Type sudo and press Enter. You should see a list of arguments, but no errors or a /etc/sudoers is world writable message.

Congratulations, you've fixed your Ubuntu install!

This should only be done as a LAST RESORT. There are probably uncorrected security errors that are affecting system security. Plan on backing up your data and reinstalling the system from scratch. I highly suggest formatting your hard disk. If you have /home on a separate drive, back up all of your personal files in there and format that disk, too.

The goal of this is to get your system reasonably secure once again after a CHMOD disaster, allowing you to safely get your files off and prepare for a reinstall.

I hope this helped. :)

  • 1
    /proc and /run (and likely /dev) are ephemeral (RAM-based) filesystems - AFAIK there is no point trying to "restore" their permissions – steeldriver Jan 17 '18 at 14:02
  • 1
    chmod 777 is dangerous. Do not do this. – muru Jan 17 '18 at 14:05
  • You should post this answer here - https://askubuntu.com/questions/43621/what-if-i-accidentally-run-command-chmod-r-on-system-directories-etc . I can see you put a lot of work into this, but there is an easier more reliable method, IMO, use apt-get to reinstall all packages with proper permissions, see my answer here https://askubuntu.com/questions/43621/what-if-i-accidentally-run-command-chmod-r-on-system-directories-etc – Panther Jan 17 '18 at 15:44
  • I tried the 'reinstall all packages' thing, but it seemed to fail to fix the problem for me. – TheComputerGeek010101001 Jan 17 '18 at 16:26