Ahmed Aswani Ahmed Aswani - 10 months ago 84
Linux Question

How to keep Environment Variables when Using SUDO

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.

Answer Source

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"

taken from ArchLinux wiki.

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"