当我使用任何带有 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.
https://stackoverflow.com/questions/8633461/