Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

According to the man page:

sudoedit /etc/file

creates a copy of the file, opens it as the current user, and when saved overwrites the existing file with the copy.

whereas

sudo vi /etc/file

opens the file as root

The man page states that sudoedit is different from sudo because:

the editor is run with the invoking user's environment unmodified

Is the first method safer, if so why? Are there other reasons for using sudoedit instead of sudo vi?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First of all, sudo vim already explicitly mentions your default editor, which is not necessary if you have it defined in $EDITOR. sudoedit spares you from defining the editor every time you want to edit something—and on a multiuser system it allows everyone to use the editor they personally like.

How so? Consider a system where normal users only get sudo privileges for editing certain files. They are restricted from running sudo with anything else though. You would have to allow them to sudo vi and sudo vim and sudo nano and sudo emacs and sudo pico (et cetera). Instead of having to do that, you could simply allow them to sudoedit the file, with their choice of setting $EDITOR to whatever they like. (Imagine you'd force an Emacs lover to use Vim…)

Another issue is that if your $EDITOR is set to vim, and you have customization settings for it in your user's .vimrc, those settings will not be used if you use sudo vim or sudo $EDITOR. sudoedit however preserves the calling user's environment, and therefore your settings.

See also: What's so great about sudoedit?

share|improve this answer
    
Also the user should be using visudo instead of sudoedit. –  Evan Darwin Jul 19 at 17:01
    
@EvanDarwin No, that is unrelated. visudo is only used to edit the /etc/sudoers file in a safe manner (to prevent you from locking yourself out). sudoedit is a command to edit any file. –  slhck Jul 19 at 17:21
    
I'm aware, for some reason my brain saw a "/etc/sudoers" in his question somewhere. Maybe I need some more coffee. –  Evan Darwin Jul 19 at 19:17

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.