如何在使用SUDO时保持环境变量?

时间:2022-02-26 23:12:23

When I use any command with sudo the enviroment 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的任何命令时,环境变量不在那里。例如,在设置HTTP_PROXY之后,命令wget在没有sudo的情况下运行良好。但是,如果我键入sudo wget,它说它不能绕过代理设置。

4 个解决方案

#1


327  

First you need to export HTTP_PROXY. Second, you need to read man sudo carefully, and pay attention to the -E flag. This works:

首先需要导出HTTP_PROXY。第二,你需要仔细阅读男士sudo,注意-E标志。如此:

$ export HTTP_PROXY=foof
$ sudo -E bash -c 'echo $HTTP_PROXY'

Here is the quote from the man page:

这是男人页上的一句话:

-E, --preserve-env
             Indicates to the security policy that the user wishes to reserve their
             existing environment variables.  The security policy may eturn an error
             if the user does not have permission to preserve the environment.

#2


246  

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

诀窍是通过sudo visudo命令将环境变量添加到sudoers文件中,并添加这些行:

Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"

taken from ArchLinux wiki.

取自ArchLinux wiki。

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

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

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

#3


34  

For individual variables you want to make available on a one off basis you can make it part of the command.

对于单个变量,您希望在一个基础上提供可用的变量,您可以将其作为命令的一部分。

sudo http_proxy=$http_proxy wget "http://*.com"

#4


18  

You can also combine the two env_keep statements in Ahmed Aswani's answer into a single statement like this:

您还可以将Ahmed Aswani的两个env_keep语句组合成一个这样的语句:

Defaults env_keep += "http_proxy https_proxy"

默认的env_keep += "http_proxy https_proxy"

You should also consider specifying env_keep for only a single command like this:

您还应该考虑为单个命令指定env_keep:

Defaults!/bin/[your_command] env_keep += "http_proxy https_proxy"

违约!/bin/[your_command] env_keep += "http_proxy https_proxy"

#1


327  

First you need to export HTTP_PROXY. Second, you need to read man sudo carefully, and pay attention to the -E flag. This works:

首先需要导出HTTP_PROXY。第二,你需要仔细阅读男士sudo,注意-E标志。如此:

$ export HTTP_PROXY=foof
$ sudo -E bash -c 'echo $HTTP_PROXY'

Here is the quote from the man page:

这是男人页上的一句话:

-E, --preserve-env
             Indicates to the security policy that the user wishes to reserve their
             existing environment variables.  The security policy may eturn an error
             if the user does not have permission to preserve the environment.

#2


246  

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

诀窍是通过sudo visudo命令将环境变量添加到sudoers文件中,并添加这些行:

Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"

taken from ArchLinux wiki.

取自ArchLinux wiki。

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

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

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

#3


34  

For individual variables you want to make available on a one off basis you can make it part of the command.

对于单个变量,您希望在一个基础上提供可用的变量,您可以将其作为命令的一部分。

sudo http_proxy=$http_proxy wget "http://*.com"

#4


18  

You can also combine the two env_keep statements in Ahmed Aswani's answer into a single statement like this:

您还可以将Ahmed Aswani的两个env_keep语句组合成一个这样的语句:

Defaults env_keep += "http_proxy https_proxy"

默认的env_keep += "http_proxy https_proxy"

You should also consider specifying env_keep for only a single command like this:

您还应该考虑为单个命令指定env_keep:

Defaults!/bin/[your_command] env_keep += "http_proxy https_proxy"

违约!/bin/[your_command] env_keep += "http_proxy https_proxy"