linux - 使用sudo时如何保持环境变量

标签 linux environment-variables sudo

当我使用带有 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/

相关文章:

windows - 在 Powershell 中使用批处理式环境变量解释字符串

linux - shell和环境变量的区别

docker - 无法在 CentOS 上运行的 Docker 容器中使用 yum

linux - Datastage Designer 和顺序文件位置

c++ - 交叉编译 Qt 5

c - getpwnam 的奇怪行为

c - 你怎么能 "avoid"SIGSEGV?

c - 在 shell 中显示环境变量时出现段错误

linux - 在 Windows 10 中模拟启用类似 Linux 的管理员权限

emacs - 如何使用 Emacs trapmp ssh 到远程主机并以另一个用户的身份临时编辑文件?