12

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?

0

3 Answers 3

18

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.

4
  • 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, 2013 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, 2013 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? Mar 14, 2013 at 22:49
  • @drjimbob, i was not aware of this, thanks. But i kept a backup of the default for safety.
    – Ankit
    Mar 15, 2013 at 16:49
18

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
6
  • +1, i encountered this situation yesterday but was not sure of what could have caused it. Thanks :)
    – Ankit
    Mar 14, 2013 at 16:10
  • 1
    The commands vipw and vigr also exist for the same reason.
    – MikeyB
    Mar 14, 2013 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. Mar 15, 2013 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. Mar 15, 2013 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 Mar 15, 2013 at 12:28
8

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.

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