Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Recover from syntax errors in the sudoers file UNIX
While messing around trying to share my iTunes library between two user accounts on the same iMac, I accidentally introduced a syntax error into my /etc/sudoers file. The big problem here is you need to invoke sudo to edit the sudoers file; I use pico, but visudo is recommended on this site.

So, to recover from this, I had to enable my root account using NetInfo in Applications » Utilities. Log in as root and edit /etc/sudoers, then once it's fixed and working, disable the root account again.

[robg adds: There's a good reason visudo is the recommended way to edit your sudoers file, and this hint is a perfect demonstration of why: because visudo includes a basic check for syntax errors, as explained in the man page:
visudo edits the sudoers file in a safe fashion, analogous to vipw(8). visudo locks the sudoers file against multiple simultaneous edits, provides basic sanity checks, and checks for parse errors.
So you really should use visudo if you're going to edit the sudoers file. If you don't, however, and introduce errors, you'll need this hint to fix the problems.]
    •    
  • Currently 2.67 / 5
  You rated: 5 / 5 (3 votes cast)
 
[18,736 views]  

Recover from syntax errors in the sudoers file | 9 comments | Create New Account
Click here to return to the 'Recover from syntax errors in the sudoers file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Single User Mode
Authored by: googoo on Feb 15, '07 08:02:05AM

I have never had this problem, but I suspect that it is possible to change the sudoers file so that you could not even enable root login using NetInfo Manager. In this case, you would have to boot into single user mode (hold down command and s at startup). At that point, you are root and can modify the sudoers file (using visudo).

-Mark



[ Reply to This | # ]
Recover from syntax errors in the sudoers file
Authored by: boredzo on Feb 15, '07 08:16:08AM

Also, visudo doesn't necessarily use vi. As explained by the manpage, if you set VISUAL or EDITOR in your environment, it will use that editor instead. This goes for vipw as well.

There are other things that use $EDITOR, so it's worth setting anyway.



[ Reply to This | # ]
Recover from syntax errors in the sudoers file
Authored by: xSmurf on Feb 15, '07 08:19:19AM

See, I like setting my EDITOR variable to 'mate -w' which opens in TextMate and asks it to stay open until I'm done with the file, allowing it to work with visudo or svn commit. Very neat.

---
MacBook Pro 2.16Ghz / 2Gb / 100Gb 7200rpm / CD/DVD±RW
PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf



[ Reply to This | # ]
Recover from syntax errors in the sudoers file
Authored by: DavidRavenMoon on Feb 15, '07 01:07:48PM

This has nothing to do with the sudoers file, but my family (3 users) share an iTunes Library, and we don't do anything special. I just moved the iTunes music location to the /Users/Shared/ directory, and told iTunes were it was for each user.

The only thing it doesn't do is update automatically when new songs are added by another user, but that's easy enough to fix.

---
G4/Digital Audio/1GHz, 1 GB, Mac OS X 10.4.8 • www.david-schwab.com • www.myspace/davidschwab • www.imanicoppola.net



[ Reply to This | # ]
Run visudo with pico
Authored by: dfe on Feb 15, '07 01:21:17PM

Try this:
EDITOR=pico sudo visudo

You could instead export EDITOR=pico in your .bash_profile or .profile if you want to use it for all commands requiring an EDITOR. For example, crontab -e if you still use cron and not launchd.



[ Reply to This | # ]
Run visudo with pico
Authored by: gshenaut on Feb 17, '07 08:22:51AM
or for shells that don't recogize that syntax, env EDITOR=pico sudo visudo

Greg Shenaut

[ Reply to This | # ]

Recover from syntax errors in the sudoers file
Authored by: aubreyapple on Feb 16, '07 09:52:41AM

It is also possible to edit files in single user mode. For me this is much simpler than enabling root user.



[ Reply to This | # ]
Recover from syntax errors in the sudoers file
Authored by: macweb on Aug 25, '08 06:34:25PM

I just use BBEdit to edit sudoers and then check if all is fine by running visudo. I was having problem with syntax error. Problem for me was that examples as stated in manual do not work, at least not in 10.5.4 in my setup. I had to make aliases for every command. Using commands directy was not working.



[ Reply to This | # ]
Recover from syntax errors in the sudoers file
Authored by: macweb on Aug 25, '08 06:39:30PM

Other way would be changing permisiiones on sudoers file and editing it in whatever texteditor you want. That should work without messing with root account, restarting, ...



[ Reply to This | # ]