10

I now know that I should never try to edit the etc/sudoers file with a regular text editor. However, minutes before I learned this, I saved a new user name in the file with Sublime Text. Now when I run sudo cat sudoers for example, I get the following error:

>>> /etc/sudoers: syntax error near line 1  <<<
sudo: parse error in /etc/sudoers near line 1
sudo: no valid sudoers sources found, quitting

How can I get out of this quandary?

3
  • Presumably, your root account doesn't have a password that you know of? In that case you could try su. Otherwise, rebooting single-user or off a live CD should be the second easiest way to revert your file; I'll let someone post a proper answer if su doesn't do the trick.
    – dhag
    Apr 13, 2015 at 2:52
  • I used su - root and now I get permission denied. I also tried to reboot as single user and edit the file, but got the error that I was in a read-only filesystem.
    – steel
    Apr 13, 2015 at 3:11
  • 1
    If you booted from CD etc then the root is the root of the CD and readonly. see instructions on mounting your root bellow. If you booted in single user mode using grub, then it may be the correct root, but readonly. If so then you need to remount rw from what I can remember mount -o remount,rw / Apr 13, 2015 at 11:16

4 Answers 4

10

I see you've tagged your question osx so if you've done this on a mac, make use of the GUI.

  1. Open any Finder window and press cmd shift G
  2. Type /etc/sudoers and press return to go to the file
  3. Press cmd i with the file highlighted
  4. Scroll to the bottom of that info window to 'Sharing & Permissions' and click the lock icon in the bottom right
  5. Type an admin username and password
  6. Now add yourself in that window with the + button and select 'Read and Write' privileges
  7. Open the file in any editor and fix what you screwed up the first time!
3
  • Brilliant. And all through the GUI! Thank you.
    – steel
    Apr 16, 2015 at 16:11
  • 1
    Adding permission to write to /etc is also required to change the sudoers file.
    – tsafrir
    Oct 21, 2015 at 19:00
  • I have same problem with my ubuntu. Can you please give some idea to fix in ubuntu ? May 24, 2018 at 5:38
0

I was unable to edit the sudoers file by adding my username in the "sharing and permissions " and changing the permission to "read and write".

I was unsuccessful in creating a new file and changing its permission to root:wheel and 0440.

Finally I had to go to recovery mode (restart -> cmd+R)

  1. Mount Macintosh HD through disk utility
  2. go to Terminal through utilities
  3. go to cd Macintosh HD/private/etc
  4. edit sudoers file to correct the mistakes.
  5. save and change permissions if needed. sudoers file needs root: wheel. If its not, sudo won't be run. chown root:wheel sudoers
  6. I had also changed the permission to 0440 since initially i removed sudoers and created a fresh copy when i was logged in.
0

Thanks @toxefa, it is working for me by modify permission by info GUI and edit sudoer file.

2020-09-10-macOS-fix-sudoer.png

0

This is what worked for me (assuming admin privileges):

1- Enable root user

  • System Preferences -> click padlock icon to unlock (enter user password)
  • click Login Options
  • Network Account Server, click "Join..." -> "Open Directory Utility..."
  • click padlock to unlock
  • click Edit in top menu -> Enable Root User, enter PW for root

2- Logout of osx

3- Login to osx using other user- name: root, PW: whatever you entered in step 1

4- Open terminal, "sudo visudo" and change what you need to change

5- Logout of root user and log into your account

6- Disable root user, using same method from System Preferences (step 1)

Documentation from Apple: https://support.apple.com/en-us/HT204012

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .