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.

This question already has an answer here:

Why it is recommended to edit /etc/sudoers file with the visudo command? Here is a sample of the file:

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.

Are there any special reasons for it?

share|improve this question

marked as duplicate by Ulrich Dangel, Renan, vonbrand, MaxMackie, rahmu Mar 17 '13 at 0:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

3 Answers 3

up vote 17 down vote accepted

Because visudo checks the syntax and make sure it is valid configuration file; otherwise you may edit the file, make an error and sudo won't be useable anymore just because of your syntax error.

share|improve this answer
1  
+1, got your point; I edited the file with vi command made the error purportedly saved the file. I was not able to open the file afterwards. –  Ankit Mar 14 '13 at 16:08
    
@Ankit - You are aware, you can reboot into single-user mode and fix up /etc/sudoers and then reboot, right? –  dr jimbob Mar 14 '13 at 20:59
    
Hmm. I was unable to get visudo to complain on OS X 10.7.5, even when entering outlandish syntax and saving. Feature disabled? –  Konrad Rudolph Mar 14 '13 at 22:49
    
@drjimbob, i was not aware of this, thanks. But i kept a backup of the default for safety. –  Ankit Mar 15 '13 at 16:49
add comment

In addition to the syntax checks previously mentioned, visudo also locks /etc/sudoers so that it can't be edited by two users simultaneously. A concurrent attempt to edit will give

visudo: /etc/sudoers busy, try again later
share|improve this answer
    
+1, i encountered this situation yesterday but was not sure of what could have caused it. Thanks :) –  Ankit Mar 14 '13 at 16:10
1  
The commands vipw and vigr also exist for the same reason. –  MikeyB Mar 14 '13 at 22:48
    
nitpicking: It can't be edited by two users using visudo (or vipw or vigr for /etc/passwd//etc/shadow and /etc/group//etc/gshadow), but it does not prevent another user to edit the file directly using any editor, as file locking in linux is just advisory. –  Carlos Campderrós Mar 15 '13 at 8:49
    
@CarlosCampderrós: File locking may be advisory, but the files do not have the write bit set, and that is not advisory. –  Dietrich Epp Mar 15 '13 at 12:16
    
@DietrichEpp the sudoers file does not have write permission, but that won't prevent an unknowledgeable user to force-write it if he/she doesn't know about sudo –  Carlos Campderrós Mar 15 '13 at 12:28
show 1 more comment

The reason for this is that visudo will verify the file does not have syntax errors prior to overwriting the current sudoers. This prevents you from accidently locking yourself out of root.

share|improve this answer
add comment

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