49

Fedora 14 uses vi by default when one runs visudo. Is there a way to change this to something else?

xenoterracide
  • 59,188
  • 74
  • 187
  • 252
tshepang
  • 65,642

6 Answers6

61

Adding Defaults editor=/path/to/editor in the sudoers file will cause visudo to use the specified editor for changes.

Additionally, if your sudo package has been built with --with-env-editor, as is the default on some Linux distributions, you can also set the EDITOR environment variable by executing export EDITOR=/path/to/editor. Performed on the command line this will revert as soon as that shell session is terminated, setting the variable in a ~/.bashrc or /etc/profile will cause the change to persist.

Tok
  • 10,744
  • Could you please provide the command(s) that you executed? – Tok Nov 29 '10 at 17:46
  • @Tshepang - You can also set Defaults editor=/usr/bin/nano in the sudoers file to specify an editor. At this point I am assuming that you wish to use nano as specified in your earlier comment. – Tok Nov 29 '10 at 18:33
  • @Tshepang - Yes, however, this is likely a product of having built the package using --with-env-editor. – Tok Nov 29 '10 at 18:57
  • 1
    @Tok: On what system doesn't visudo honor $VISUAL and $EDITOR? – Gilles 'SO- stop being evil' Nov 29 '10 at 20:01
  • @Gill It is Fedora 14 default install. – tshepang Nov 29 '10 at 20:03
  • @Tshepang: Yes, I was wondering what system is not like this. (Debian, Fedora, Ubuntu, OpenBSD have sudo honoring VISUAL and EDITOR.) – Gilles 'SO- stop being evil' Nov 29 '10 at 20:22
  • 4
    @Gilles - based on my reading Ubuntu has recently changed the build parameters of their sudo package to not include --with-env-editor, recently here meaning within the memory of the internet. Additionally the sudo package changed their default some time ago to use vi as their default editor which I believe coincided with a change to the behavior regarding the acceptance of environmental variables. I do not have a clear timeline of how, when, or if these events relate to each other at this time. – Tok Nov 29 '10 at 20:33
  • 1
    @gilles: On RHEL 5.10, which I use, visudo doesn't honor $VISUAL or $EDITOR. – Bulrush May 09 '16 at 17:11
  • 1
    @Gilles I'm using Arch Linux and sudo does not honor $EDITOR nor $VISUAL env var either. I have to add the variable after sudo to manually override it. – Franklin Yu Aug 15 '18 at 21:11
  • @FranklinYu Note that this is about visudo, not about sudo. If you run sudo visudo, then visudo never gets a chance to honor EDITOR and VISUAL because it doesn't see them. You need to write something like sudo EDITOR=emacs visudo. – Gilles 'SO- stop being evil' Aug 16 '18 at 19:41
  • 4
    sudo EDITOR=/usr/bin/vi visudo is what I used to get this to work. – PatS Feb 11 '21 at 04:33
  • 1
    If you have the $EDITOR variable set in your user's environment you can use sudo -E visudo to get it into your sudo environment (it will share all environment variables and may have security implications). This works for me on Arch. – Paul Mar 05 '21 at 11:41
22

The following

  • works for distros that use alternatives (originally just Debian and its forks, but now most major distros, IIUC).
  • has the advantage that

    1. you won't need to edit sudoers first with its default editor
    2. it works even if package=sudo has not been built with --with-env-editor

From a commandline:

  1. Run sudo update-alternatives --config editor
  2. Choose desired editor from the (text-mode) menu. If you don't see the editor you want, you probably need to install it; cancel, do that, and repeat.

sudo visudo should now open the editor of your choice.

TomRoche
  • 1,285
15

visudo uses the EDITOR variable, for example.

sudo EDITOR=/path/to/editor visudo
Bhavin
  • 157
  • 2
    This is excellent. It gets around the circular problem of using the default editor to change the default edit. – Manngo May 08 '20 at 00:00
  • It's certainly a reasonable solution, but IMHO, it misses the point because you have to use this command each time the sudo file is edited. Other answers here propose changes to default behavior. – Seamus May 13 '20 at 23:04
  • @Seamus, While I agree with your statement, I was stuck with how to do this the very first time (before changing the default in the sudo file). So this answer combined with the accepted answer which included adding Defaults editor=/path/to/editor to the sudoers file. – PatS Feb 11 '21 at 04:38
10

A normal unix program that wants to invoke an editor will run the program whose name is in the EDITOR or VISUAL environment variable, and if the variable is unset, a system-dependent default. Many, but not all, programs check both EDITOR and VISUAL; the distinction is long obsolete (once you would run EDITOR on a teletype and VISUAL on a “glass” terminal…) so you should set both to the same value. There is some disagreement as to whether the contents of the variable should be the full path to an executable, an executable name that's looked up in $PATH, or a shell snippet, so you should stick to a path to an executable not containing any shell metacharacter. The system default when neither variable is set is traditionally vi, though some distributions have changed this to a more newbie-friendly editor such as nano.

visudo checks both VISUAL and EDITOR (this can be compiled out, presumably to dissuade root from using an editor whose security the distribution maker doesn't trust, but even OpenBSD doesn't do this).

6

These steps let me set the editor to vi for visudo on Ubuntu 12.04, they may work for other distributions:

Issue command:

sudo visudo

This opens up the sudoers file in nano. Near the top you will see this line:

Defaults    env_reset

Add the following line beneath the above line:

Defaults    editor=/usr/bin/vi

Save the file and exit. Reopen visudo as your default user:

sudo visudo

The sudoers file now opens up in VI.

Seamus
  • 2,925
1

IMPO the best way to do this is to have each user set their own preferred editor using the EDITOR and VISUAL environment variables in their own .bashrc file (or the equivalent for their preferred shell), then tell sudo to respect those variables.

So you would add this to /etc/sudoer using visudo:

## Respect the user's choice of editors
Defaults env_keep += "EDITOR VISUAL"

Then add this to ~/.bashrc:

# e.g. if you want to use micro
EDITOR=micro
VISUAL=micro

For the distinction between EDITOR and VISUAL and why I think it's best to set both, see this answer.


After this sudo visudo should use the user's preferred editor for all users.

cyqsimon
  • 785
  • 7
  • 22