這個問題主要會引發一些軟件環境變量方面的困擾,好比使用了 zsh
,sudo -s
後發現主題消失,或者其它軟件相似的現象,主要緣由是使用了sudo -s
後當前用戶爲root用戶,環境變量改變,致使配置文件失效。shell
sudo visudo sudo vim/etc/sudoers
Defaults:用戶名 !env_reset
注意:以上修改的僅僅是當前用戶的環境變量規則,也就是說,在當前用戶下使用sudo
不會更改環境變量,可是sudo -s
後進入的是root用戶,再提權即root用戶使用sudo
,那麼環境變量依然遵循root用戶的配置。
若是想root後也保留當前環境配置,須要額外設置,參考下文。ubuntu
Using sudo 1.7.4p4 on Solaris 5.10 and sudo 1.6.7p5 on RHEL4 u6 I can't see how to preserve my environment variables, for instance $PYTHONPATH. I've added this line to sudoers, but it doesn't make any difference:vim
Defaults !env_reset
Am I doing something wrong, or is the sudo installation simply not respecting the env_reset flag?bash
Edit: At least on Solaris, we've found that this issue depends on the shell! The standard root shell is Bourne, if we run bash under sudo (sudo bash) on the other hand, !env_preset will preserve the environment (including PATH and LD_LIBRARY_PATH). This is rather confusing behaviour I have to say.this
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:unix
Thank you for the answers. I wonder if I might ask a follow-up question, which is "Why, then, does this not work?"code
In the example I'm trying to get working, I want sudo nano to read my $HOME/.nanorc. If I use this:ip
Defaults:simon env_keep=HOME
it works perfectly. If I use this:ciDefaults!/bin/nano env_keep=HOME
or this:getCmnd_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 echos $HOME).
`/etc/sudoers` — specify `env_keep` for one command only?
How is sudo set to not change $HOME in Ubuntu and how to disable this behavior?