How can I properly set sudo/visudo's editor?
I am using Ubuntu 10.04 Server and trying to set up sudoers to respect a user's EDITOR choice (within limits)
In my sudoers I have:
Defaults editor=/usr/bin/nano:/usr/bin/vim
Defaults env_resetAnd in the user .bashrc:
export EDITOR=/usr/bin/vim$EDITOR is set:
$ echo $EDITOR
/usr/bin/vimAccording to man sudoers this should be enough for $EDITOR to be set to vim:
editor A colon (':') separated list of editors allowed to be used with visudo. visudo will choose the editor that matches the user's EDITOR environment variable if possible, or the first editor in the list that exists and is executable. The default is the path to vi on your system.However nano is still being used for this user. A quick check of env:
$ sudo -- env | grep EDITORReturns nothing.
$ sudo -E -- env | grep EDITORReturns EDITOR=/usr/bin/vim
I am aware that I could do the following things to make EDITOR work:
- Set
env_editor,env_keep+=EDITORor any other option that keeps the EDITOR variable in sudoers: I don't want to do this as it could allow arbitrary execution of anything (e.g.export EDITOR=~/bad_program_to_run_as_root) - Use
sudo -Eor evenalias sudo='sudo -E': Defeats the point of havingenv_resetand users without SETENV (not something I want to give out: see previous point) getsudo: sorry, you are not allowed to preserve the environment - Set
editor=/usr/bin/vim: But there are other users who don't know vim - Use
sudo select-editor: Close, butsudo visudostill opens innano - Just use sudoedit or vim directly: But then you lose the safety of tools like
visudo,vipw,crontab -e. - Just deal with it: Probably, but if I'm missing some insight I would love to know
I've also tried setting the VISUAL and SUDO_EDITOR variables (in desperation)
Is there something I have missed that will make sudo visudo open in the users editor of choice, without making the compromises above?
EDIT:
I think I understand why this isn't working as I expect. I'm putting it down here in case anyone else has the same misconception.
In the sudoers file
Defaults editor=/usr/bin/nano:/usr/bin/vim- Only refers to the list of editors that are allowed when running
visudo(not any other program) editorchecks $EDITOR, but if runningsudo visudo,sudodoes not set $EDITOR, so whenvisudoruns it will be empty- Therefore the first editor is used, in this case
nano
Can anyone confirm that this is correct?
I expected therefore that a safe solution would be to add:
Defaults!/usr/sbin/visudo env_keep+=EDITORi.e. keep EDITOR if and only if running visudo. This would then be checked against
Defaults editor=/usr/bin/nano:/usr/bin/vimAnd if it didn't match either would use nano
Weirdly though, this doesn't seem to be the case:
$ sudo su - root
# export EDITOR=/bin/echo
# visudo
/etc/sudoers.tmp
visudo: /etc/sudoers.tmp unchanged/bin/echo is used as the editor. Bug? Or another misconception?
Thanks
4 Answers
You are right that setting the EDITOR variable should change the editor used for sudo. However, there are two other variables with precedence over the EDITOR: SUDO_EDITOR and VISUAL. Make sure none of them point to some other editor like nano.
There's another solution as described here:
sudo update-alternatives --config editorBut it's not so friendly on a multi-user system as it only updates a symlink in /usr/bin/:
$ ls -l `which editor`
lrwxrwxrwx 1 root root 24 lip 4 19:37 /usr/bin/editor -> /etc/alternatives/editor
$ ls -l /etc/alternatives/editor
lrwxrwxrwx 1 root root 18 Jul 5 01:39 /etc/alternatives/editor -> /usr/bin/vim.basicWhat happened to select-editor anyway? When I run it, it creates a file:
$ ls -l .selected_editor
-rw-r--r-- 1 rld rld 75 Jul 5 01:54 .selected_editor
$ cat .selected_editor
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/vim.basic"But sudo visudo keeps using nano.
In Debian 7, setting EDITOR in the environment didn't work.
To use Nano, I ended up adding the following line to /etc/sudoers
Defaults editor="/usr/bin/nano" 2 env_reset does not keep a user from setting variables on the command line:
$ sudo EDITOR=vim -- env |grep EDIT
EDITOR=vimI find your findings about the editor option mildly shocking but unfortunately I don't know the answers to your secondary questions. One would think that the Ubuntu camp would have plenty of docs and configuration examples on this issue, perhaps we ought to look harder.