使用sudo时如何保留环境变量 [英] How to keep environment variables when using sudo

查看:26
本文介绍了使用sudo时如何保留环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将任何命令与 sudo 一起使用时,环境变量都不存在.例如,在设置 HTTP_PROXY 之后,命令 wget 可以在没有 sudo 的情况下正常工作.但是,如果我输入 sudo wget 它说它不能绕过代理设置.

When I use any command with sudo the environment variables are not there. For example after setting HTTP_PROXY the command wget works fine without sudo. However if I type sudo wget it says it can't bypass the proxy setting.

推荐答案

诀窍是通过 sudo visudo 命令将环境变量添加到 sudoers 文件并添加以下几行:

The trick is to add environment variables to sudoers file via sudo visudo command and add these lines:

Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"

取自 ArchLinux wiki.

对于 Ubuntu 14,您需要在单独的行中指定,因为它返回多变量行的错误:

For Ubuntu 14, you need to specify in separate lines as it returns the errors for multi-variable lines:

Defaults  env_keep += "http_proxy"
Defaults  env_keep += "https_proxy"
Defaults  env_keep += "HTTP_PROXY"
Defaults  env_keep += "HTTPS_PROXY"

这篇关于使用sudo时如何保留环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆