Is there any way to specify that sudo
should preserve certain environment variables for specified commands only? For some purposes I'd like my $HOME
env. variable preserved when I run certain commands. For other purposes and other commands, I want it reset. Can this be done with /etc/sudoers
?
Edit:
Thank you for the answers. I wonder if I might ask a follow-up question, which is "Why, then, does this not work?"
In the example I'm trying to get working, I want sudo nano
to read my $HOME/.nanorc
. If I use this:
Defaults:simon env_keep=HOME
it works perfectly. If I use this:
Defaults!/bin/nano env_keep=HOME
or this:
Cmnd_Alias NANO = /usr/bin/nano,/bin/nano,/bin/rnano
Defaults!NANO env_keep=HOME
it's not working at all. Any suggestions as to why? (I'm on Debian testing, btw.)
(Note: I don't think it's nano
specific, btw -- I can reproduce the behaviour with a one-line bash script that simply echo
s $HOME
).
sudoedit
instead.sudoedit
will run copy the contents of a file to a tmp file and run the editor of your choice as your user, when the editor exits it will check if you changed anything and if so replace the original with the edited one. You can select which editor you want it to run by setting the$EDITOR
or $VISUAL` env vars. – Arrowmaster May 17 '11 at 17:31sudo
with a function of my own, I've simply added aif [ "$1" == "$EDITOR" ]; then
block and am now callingsudoedit
instead :) – simon May 18 '11 at 02:21$HOME/.local/lib/python/site-packages
For a shell script, pass-f
and-p
(protected for ksh/zsh). – okapi Dec 17 '21 at 15:10