Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Because sudo clears any and all environment variables not explicitly whitelisted. Basic security. –  Shadur Apr 5 '13 at 8:02
    
Why then does sudo echo work but not sudo apt-get? –  mydoghasworms Apr 5 '13 at 9:24
1  
@mydoghasworms because $http_proxy gets evaluated by the shell and you now run basically sudo echo http://proxyserver:8080 –  Ulrich Dangel Apr 5 '13 at 9:44
    
Thanks @UlrichDangel, perhaps you could post this as an answer? –  mydoghasworms Apr 5 '13 at 9:50
add comment

5 Answers

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.

share|improve this answer
add comment

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"

Though it does not go into the detail of why (unless I missed it), it does confirm my experience and give me the workaround I need.

share|improve this answer
    
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. –  mydoghasworms Apr 5 '13 at 9:32
    
On second thought, Shadur, I believe your addition actually belongs in its own answer. –  mydoghasworms Apr 5 '13 at 9:48
add comment

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.

share|improve this answer
add comment

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}"
share|improve this answer
add comment

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

sudo -E echo $http_proxy
share|improve this answer
1  
No, the http_proxy variable gets extended before the sudo run, and the shell then runs sudo -E echo ADDRESS_OF_PROXY –  Ulrich Dangel Apr 5 '13 at 14:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.