Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I understand that if you want to modify who can use sudo and what they can do with it that you should use visudo. I know I'm not supposed to directly modify the /etc/sudoers file myself.

What is it that visudo does that directly modifying the file doesn't do? What can go wrong?

share|improve this question

2 Answers 2

up vote 58 down vote accepted

visudo checks the file syntax before actually overwriting the sudoers file.

If you use a plain editor, mess up the syntax, and save... sudo will (probably) stop working, and, since /etc/sudoers is only modifiable by root, you're stuck (unless you have another way of gaining root).

Additionally it ensures that the edits will be one atomic operation. This locking is important if you need to ensure nobody else can mess up your carefully considered config changes. For editing other files as root besides /etc/sudoers there is the sudoedit command which also guard against such editing conflicts.

share|improve this answer
2  
Wow, never knew about sudoedit. It doesn't work on OS X so I assume it's a GNU tool? Anyway, cool info here. I'd always edited manually and never had a problem - I understand now that it was just luck and the tool can help prevent catastrophe. Thanks! –  Harv Dec 26 '11 at 20:01
    
@Harv. There is no GNU sudo and OS X does have GNU tools. As sudo was first created as an open source application, there's probably no reason for their being many implementations. sudo and sudoedit are the same command, sudo behaves as sudo -e when called as sudoedit. I believe it's just that OS X forgot to add the sudoedit -> sudo link, but you should still be able to use sudo -e or call sudo with argv[0] set to sudoedit to get the same behavior. –  Stéphane Chazelas Jan 23 '13 at 22:41
    
Interestingly, visudo uses nano by default. –  Tim Aug 27 at 17:52
    
@Tim: The "real" default is vi, but it can be configured to use something else. So nano could indeed be the default on your distro/setup. See the man page. –  Mat Aug 28 at 7:42
    
@Mat ahh okay thanks :) –  Tim Aug 28 at 7:49

From the visudo man page:

visudo locks the sudoers file against multiple simultaneous edits, provides basic sanity checks, and checks for parse errors. If the sudoers file is currently being edited you will receive a message to try again later.

Also check this answer from serverfault.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.