当我使用带有 sudo 的任何命令时,环境变量不存在。例如,在设置 HTTP_PROXY 后,命令 wget
可以在没有 sudo
的情况下正常工作。但是,如果我输入 sudo wget
,它会说它无法绕过代理设置。
最佳答案
首先您需要export HTTP_PROXY
。其次,您需要阅读 man sudo
,并查看 -E
标志。这有效:
$ export HTTP_PROXY=foof
$ sudo -E bash -c 'echo $HTTP_PROXY'
这是手册页中的引述:
-E, --preserve-env
Indicates to the security policy that the user wishes to preserve their
existing environment variables. The security policy may return an error
if the user does not have permission to preserve the environment.
关于linux - 使用sudo时如何保持环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34030137/