6

I use apt behind a firewall, so I specify a proxy in the http_proxy environment variable.

What I have done is added the following line to both .profile and .bashrc (for good measure) of the root user:

export http_proxy=http://proxyserver:8080

When I do the following, it prints the value of the proxy correctly:

sudo echo $http_proxy

However, when I call sudo apt-get update, it does not seem to see the http_proxy variable as the root user.

Only when I su to root, apt-get works as expected, through the proxy, so it obviously sees the variable.

So my question is, as above, why does sudo apt-get not see the variable?

UPDATE I have just learned that when I call sudo echo, it echoes the value of the variable set in the current user's profile. But even so, why does sudo apt-get not use that variable value?

5
  • Because sudo clears any and all environment variables not explicitly whitelisted. Basic security. Commented Apr 5, 2013 at 8:02
  • Why then does sudo echo work but not sudo apt-get? Commented Apr 5, 2013 at 9:24
  • 1
    @mydoghasworms because $http_proxy gets evaluated by the shell and you now run basically sudo echo http://proxyserver:8080 Commented Apr 5, 2013 at 9:44
  • Thanks @UlrichDangel, perhaps you could post this as an answer? Commented Apr 5, 2013 at 9:50
  • To properly check the value of http_proxy, you should do sudo env | grep http_proxy or sudo echo '$http_proxy' instead of sudo echo $http_proxy
    – wisbucky
    Commented May 18, 2015 at 16:43

5 Answers 5

7

It doesn't "not seem to work" in the case of sudo apt-get, it seems to work when you do sudo echo $http_proxy.

When you enter a shell command, it globs and expands all variables and aliases before it executes.

So sudo echo $http_proxy becomes sudo echo http://proxyserver:8080 which goes through fine.

The reason $http_proxy isn't seen by apt-get is because sudo clears all environment variables except the ones explicitly whitelisted in /etc/sudoers with the env_keep directive.

More information on how to manipulate the way sudo handles environment variables (or rather, how it usually doesn't) can be found in man sudoers.

1
  • This is the most accurate explanation. To properly check the value of http_proxy, you should do sudo env | grep http_proxy or sudo echo '$http_proxy' instead of sudo echo $http_proxy
    – wisbucky
    Commented May 18, 2015 at 16:44
4

Just to let you know, because this is what I settled on, being specific to this case and therefore a better solution, is to modify /etc/apt/apt.conf to add a line for the proxy:

Acquire::http::Proxy "http://yourproxyaddress:proxyport";

As this system resides pretty much permanently behind the proxy, this is a better solution than using the http_proxy environment variable in the long run.

3

Ah, I have just seen something on this page:

https://help.ubuntu.com/community/AptGet/Howto#Setting_up_apt-get_to_use_a_http-proxy

If you normally use sudo to run apt-get you will need to login as root first for this to work unless you also add some explicit environment settings to /etc/sudoers, e.g.

Defaults env_keep = "http_proxy https_proxy ftp_proxy"

The reason you need to do is is because sudo deliberately clears any and all environment variables nod so whitelisted as a basic security precaution.

2
  • Hello Shadur, I have rolled back your edit. Can I ask that you please add what you said after the last paragraph, as what I said is still valid for me and I would first want to hear your explanation to my comment above. Thanks. Commented Apr 5, 2013 at 9:32
  • On second thought, Shadur, I believe your addition actually belongs in its own answer. Commented Apr 5, 2013 at 9:48
0

As already pointed out, sudo clears custom environment variables set by the user.

As a workaround you could try:

export http_proxy='http://proxyserver:8080'
sudo sh -c 'export "${1}"; echo "$http_proxy"; apt-get update' _ "http_proxy=${http_proxy}"
0

I think that you just need to use the '-E' option for sudo, e.g.

sudo -E echo $http_proxy
1
  • 2
    No, the http_proxy variable gets extended before the sudo run, and the shell then runs sudo -E echo ADDRESS_OF_PROXY Commented Apr 5, 2013 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .