Is there a key difference between
sudo -e and
sudo vim. I have set up the
sudoers file so that vim is my default editor. Is there a key difference between the two?
Plus, should I switch from
rvim? I tried it but I had some problems with my config file
The big difference is who is editing what file.
sudo vim (assuming successful authentication), the root user invokes vim and edits the file in place (with root's environment and vim swap files parallel to the file being edited).
sudo -e or
sudoedit the user who invoked sudo edits a temporary copy of the file owned by themselves with their own environment (including things like ~/.vimrc). Once the user saves the output, the content of the temporary file is copied back into the original file that the user didn't have the permissions to edit. This method also has a couple checks that prevent editing under a few circumstances:
- the user is trying to edit a symbolic link
- the user is trying to edit a file using a path containing a symbolic link
- the user has write permissions on the directory containing the file
Why those specific rules are strictly enforced, I do not know (some sort of security issues I'd assume).
P.S. Users are also disallowed with sudo's edit mode from editing files that are device special files (block devices, serial devices, etc.).
EDIT: Another consequence of not running vim as root, is that the user cannot use vim's shell capabilities this way to run arbitrary commands as root. This allows giving the user access to edit certain files via sudoers rules, while not handing over the keys to the kingdom.
There’s one key difference: with
sudo -e, the editor runs as your user, not as root; with
sudo vim, the editor runs as root. This has a number of consequences; one of them is that with
sudo -e, you’ll get your own editor setup; another is that with
sudo -e, you won’t risk making other changes to root-owned files.
sudo -e also prevents users from running anything else as root (since most editors can start shells).
sudo -e, you have a small set of operations done as root (with a number of checks to avoid root escapes), and everything the user interacts with to actually edit the file runs as the user. With
sudo vim, everything happens as root, and the user can effectively do anything they want as root.