Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using Bash on Debian Squeeze. I just read this topic: change default text editor for crontab to vim

I have exported both variables VISUAL and EDITOR to vim for normal user and for root. And this works – when I type crontab -e as normal user or root, crontab runs vim for editing.

But running sudo crontab -e opens nano.

Running sudo echo $EDITOR (or $VISUAL) gave vim int output.

share|improve this question
3  
You'll find an explanation of why sudo echo $EDITOR doesn't do what you think it does here (and things to try to make your crontab work) – Mat Dec 23 '12 at 12:20
1  
@mat: Your comment could probably be a regular answer. – Heptite Dec 23 '12 at 13:51
    
@mat: thanks for that answer. – Coolmax Dec 23 '12 at 14:30
    
Did you run 'update-alternatives --config editor' while you are root? – tumiki Dec 25 '12 at 5:58
    
@tumiki: I just checked, it's work, but I'm searching solution for other (non-debian) distros too. – Coolmax Dec 26 '12 at 11:59

By default and for security purposes, sudo does not preserve the user environment.

There are multiple ways to deal with this; for your specific case, I recommend adding VISUAL=/usr/bin/vim (and EDITOR=/usr/bin/vim for programs that use the wrong one) to /etc/environment.

Second choice, if you have a permissive sudo configuration (perhaps because it's just you, or all sudoers have real root access anyway) is to run visudo and add Defaults env_keep="VISUAL EDITOR" to allow all sudoers to specify their own (i.e. preserve that environment variable).

Your existing sudoers file may well already have (or include) one or more env_keep statement(s), e.g. this (from the pastebin example):

Defaults    env_reset
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"

The first of those env_keep lines will overwrite whatever edits you make above it (assignment), the other lines extend the list (+=). To keep everything working the way you're used to and unless you have a reason to change the set, I recommend adding a line immediately after those:

Defaults    env_keep += "VISUAL EDITOR"

(There are other options to use your preferred editor, but they're either needlessly complicated or overly permissive and dangerous, so you'll have to read man sudoers, figure them out yourself, and understand what the associated problems might be.)

share|improve this answer
    
Love this... except that visudo will launch vi... the thing we're trying to avoid like the plague. sudo env EDITOR=nano visudo is the way to go. Or maybe cat, which is a bit more dangerous. Also, Defaults env_keep="VISUAL EDITOR" only works after env reset, so putting it at the top will not work... the bottom is safest. – Ray Foss May 13 '15 at 12:49
    
I am baffled by why placing env_keep="VISUAL EDITOR" under env_reset does not work... Ive only tried on top, under env_reset and at the bottom. only at the bottom works. – Ray Foss May 19 '15 at 15:47
    
Without any other information, I'll just guess that there already is a line setting env_keep in the file somewhere, which overrides whatever you put in above and which you override by moving your directive to the end. If you paste your sudoers file to pastebin.com or something and add a link here, I can extract the relevant info into the answer. – Gabe May 19 '15 at 16:11
    
it is the default centos sudoers that gives me troubles. I should probably script that. pastebin.com/4s3CiVYJ – Ray Foss May 19 '15 at 18:56

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.