Why do I have to edit /etc/sudoers with visudo?

I've noticed that the sudoers file and cron config files act in a special way compared to other config files on Linux. They need to be edited with a special wrapper rather than any text editor. Why is this?


Solution 1:

You use visudo mostly to prevent from breaking your system. Visudo runs checks on your changes to make sure you didn't mess anything up. If you did mess something up, you could completely wreck your ability to fix it or do anything requiring privileges without rebooting into a rescue mode.

The man page describes this.

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. If the sudoers file is currently being edited you will receive a message to try again later.

Solution 2:

Zoredache answer is perfect.

One more thing that can be worth to mention. You can use you favorite editor by setting EDITOR or VISUAL:

export EDITOR=whatevertexteditoryouwant
export VISUAL=whatevertexteditoryouwant

Or:

EDITOR=whatevertexteditoryouwant visudo