2

On my Red Hat machine, when I run the command echo $HOME with my normal user account, I get /home/snrub. When I run the command sudo vim foo.txt and inside vim I type echo $HOME I get /root returned. I want it to return /home/snrub instead so that it will parse my cool Vim plugins, even when I'm editing a file with sudo. According to the sudo manpage, the -E or -H options should work, but if I run sudo -E vim foo.txt or sudo -H vim foo.txt or sudo -EH vim foo.txt they all still don't parse my home .vimrc file and return /root when I type echo $HOME.

I have also tried adding the following line to the sudoers file, and saving:

Defaults   env_keep += "HOME"

Still not working. If I run sudo vim foo.txt and then inside Vim type echo $HOME it still says /root. Similarly (and to verify that this is not strictly an issue with Vim), if I type sudo emacs foo.txt and press Enter, then type Alt-X, type getenv and press Enter, then type HOME and press Enter, it will still return /root. How do I make it return /home/snrub?

2
  • I assume you mean inside vim's command mode, you type !echo $HOME?
    – Jeff Schaller
    Dec 20, 2016 at 13:24
  • Yes, that is correct. Inside Vim I type echo $HOME and it returns /root.
    – MrSnrub
    Dec 20, 2016 at 13:25

1 Answer 1

0

As a security precaution, sudo resets a list of environment variables (TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_*) under the env_reset option, which is enabled by default. In order for HOME to carry over from your user account to a sudo environment, you would have to either:

  • use the SETENV tag on the sudo rule along with the -E option, or

  • specifically unset the env_reset option.

Additionally ensure that always_set_home is off (which it is by default).

See the sudoers man page for all the details and interactions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .