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


当前回答

一个简单的包装器函数(或内联for循环)

我想出了一个独特的解决方案,因为:

sudo -E "$@"泄漏变量,导致我的命令出现问题 sudo VAR1="$VAR1"…VAR42="$VAR42" "$@"在我的例子中又长又丑

demo.sh

#!/bin/bash

function sudo_exports(){
    eval sudo $(for x in $_EXPORTS; do printf '%q=%q ' "$x" "${!x}"; done;) "$@"
}

# create a test script to call as sudo
echo 'echo Forty-Two is $VAR42' > sudo_test.sh
chmod +x sudo_test.sh

export VAR42="The Answer to the Ultimate Question of Life, The Universe, and Everything."

export _EXPORTS="_EXPORTS VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7 VAR8 VAR9 VAR10 VAR11 VAR12 VAR13 VAR14 VAR15 VAR16 VAR17 VAR18 VAR19 VAR20 VAR21 VAR22 VAR23 VAR24 VAR25 VAR26 VAR27 VAR28 VAR29 VAR30 VAR31 VAR32 VAR33 VAR34 VAR35 VAR36 VAR37 VAR38 VAR39 VAR40 VAR41 VAR42"

# clean function style
sudo_exports ./sudo_test.sh

# or just use the content of the function
eval sudo $(for x in $_EXPORTS; do printf '%q=%q ' "$x" "${!x}"; done;) ./sudo_test.sh

结果

$ ./demo.sh
Forty-Two is The Answer to the Ultimate Question of Life, The Universe, and Everything.
Forty-Two is The Answer to the Ultimate Question of Life, The Universe, and Everything.

How?

这是由bash内置printf的一个特性实现的。%q产生一个带引号的shell字符串。与bash 4.4中的参数展开不同,这适用于bash < 4.0版本

其他回答

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

Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"

摘自ArchLinux wiki。

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

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

对于您希望一次性可用的单个变量,您可以将其作为命令的一部分。

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

你也可以把Ahmed Aswani回答中的两个env_keep语句合并成一个语句,就像这样:

默认值env_keep += "http_proxy "

你也应该考虑只为一个命令指定env_keep,就像这样:

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

首先,您需要导出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.

一个简单的包装器函数(或内联for循环)

我想出了一个独特的解决方案,因为:

sudo -E "$@"泄漏变量,导致我的命令出现问题 sudo VAR1="$VAR1"…VAR42="$VAR42" "$@"在我的例子中又长又丑

demo.sh

#!/bin/bash

function sudo_exports(){
    eval sudo $(for x in $_EXPORTS; do printf '%q=%q ' "$x" "${!x}"; done;) "$@"
}

# create a test script to call as sudo
echo 'echo Forty-Two is $VAR42' > sudo_test.sh
chmod +x sudo_test.sh

export VAR42="The Answer to the Ultimate Question of Life, The Universe, and Everything."

export _EXPORTS="_EXPORTS VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7 VAR8 VAR9 VAR10 VAR11 VAR12 VAR13 VAR14 VAR15 VAR16 VAR17 VAR18 VAR19 VAR20 VAR21 VAR22 VAR23 VAR24 VAR25 VAR26 VAR27 VAR28 VAR29 VAR30 VAR31 VAR32 VAR33 VAR34 VAR35 VAR36 VAR37 VAR38 VAR39 VAR40 VAR41 VAR42"

# clean function style
sudo_exports ./sudo_test.sh

# or just use the content of the function
eval sudo $(for x in $_EXPORTS; do printf '%q=%q ' "$x" "${!x}"; done;) ./sudo_test.sh

结果

$ ./demo.sh
Forty-Two is The Answer to the Ultimate Question of Life, The Universe, and Everything.
Forty-Two is The Answer to the Ultimate Question of Life, The Universe, and Everything.

How?

这是由bash内置printf的一个特性实现的。%q产生一个带引号的shell字符串。与bash 4.4中的参数展开不同,这适用于bash < 4.0版本