Changed sudoers file and can't open it anymore; can't open any file using root

6,862

Solution 1

Assuming you didn't give the root user a password then the easiest way to fix this is with a live cd such as Knoppix. Boot of the CD. Mount the local disc, edit the file, unmount the disc and reboot.

Solution 2

Simple, boot the computer in 'single user mode' by doing the following steps:

  1. Reboot
  2. When Grub starts, press ESC (or escape)
  3. Press 'e' for editing the current line
  4. Add 'single' to end of the line containing 'linux'
  5. Boot (press b)

This will start the computer with only one user, root. From there follow these steps:

  1. /usr/bin/vim /etc/sudoers (or use nano, might be easier for you) to fix the problem
  2. If you're unsure of the problem, try copying the included example of sudoers from "/usr/share/doc/sudo/examples" (varies) and of course add your user there.
  3. Save the file, exit Vim
  4. Reboot, enjoy!

Solution 3

James' idea to use a live cd (assuming the root account is not enabled) is a good one. I would add a few points.

  • First, you might get lucky and a backup of the /etc/sudoers file was automatically saved when you were editing it. Check in /etc for a file that looks like this sudoers~ (you can cd to that directory and run ls -A without root privileges, even if you can't read or edit the files as a regular user).Some editors will create such a backup if at all possible, so you may have one without having created it explicitly.
  • Second, if you have no idea how to edit the file, you might consider posting it (or the area around line 7) here or elsewhere. Although the file itself has to do with security, there aren't passwords in it, so there's no immediate harm in posting it.
  • Last, note that an error message like the one you saw doesn't necessarily mean that the problem itself is literally on line 7. The error might be on line 4, but only got tripped off, so to speak, on line 7.
Share:
6,862

Related videos on Youtube

Karlo
Author by

Karlo

Engineer

Updated on September 17, 2022

Comments

  • Karlo
    Karlo over 1 year

    I changed settings in sudoers file using

    sudo visudo 
    

    and messed it up. Now when I try to do it again or open any file using sudo, I can't open it anymore and I get this error message:

    >>> sudoers file: syntax error, line 7 
    

    What now?

    Is there any way to get out of this problem (to revert the settings), or do I need to install a fresh copy of linux ?

    The reason I got all this is I pressed "Q" while it was telling me there was some error, which was very stupid of me.

    Thanks.

    EDIT

    My sudoers file is no different than normal sudoers file. I just added timestamp_timeout = 0 line in that file and all these problems arised. I did what James suggested and even removed that line (timestamp_timeout = 0). I changed file permission to 0440 which it requested, and then boot normally. Then, I get the same error message

    sudoers file: syntax error, line 7 
    sudo: parse error in /etc/sudoers near line 7
    
    as above.

    What should I do now ??

    EDIT 2 Solved

    This is what I did:

    1. Boot using livecd
    2. Remove the old sudoers file
    3. Made new sudoers file and copied everything to that file i.e. minimum configuration settings, which I luckily had saved in another file
    4. Changed file permission to 0440
    5. Reboot again normally from harddrive

    Hurray !!

    • Andrioid
      Andrioid almost 15 years
      you could paste the contents of your sudoers file to www.pastebin.com and let us take a look, probably the fastest way.
    • Dennis Williamson
      Dennis Williamson almost 15 years
      I would look for missing # on comments, missing colons, equal signs, line-continuation backslashes, etc.
  • Smiter
    Smiter almost 15 years
    Pressing 'e' when you get shown the error, after you finish editing, will drop you back in so you can fix your errors. Unfortunately it's doesn't explicitly mention this to you.
  • James
    James almost 15 years
    I was wondering about when it asks for a password before it gives the single user shell ?
  • Telemachus
    Telemachus almost 15 years
    This method doesn't work for all distros. Since it's a well-known back door, some distros force a login before you get to a root prompt. It sounds like that is the case for you, James.
  • Andrioid
    Andrioid almost 15 years
    Didn't know distros had started doing that, always worked for me in the good old Slackware days
  • Admin
    Admin over 14 years
    You should recommend the use of visudo over vim: en.wikipedia.org/wiki/Visudo
  • Scott - Слава Україні
    Scott - Слава Україні about 5 years
    But how do you get pkexec to do your bidding  if you don’t know the root password?