Sunday, July 3, 2011

HTTP proxy and apt

This might be well-known, but I though I'd mention it anyway, as it seems a bit mysterious at first sight.
If you live behind a HTTP proxy you probably are used to the two ways of making your system use the proxy: by http_proxy and no_proxy environment variables and by the GNOME Network Proxy Preferences (gnome-network-properties). But either way it can happen that simple sudo aptitude or sudo apt-get install foo fail because it tries to access the internet directly, ignoring the proxy setting. So, why is that?

Both ways end up setting the http_proxy environment variable (and possibly others). But sudo, at least on Ubuntu 11.04, is set up to reset the environment. You can see it in the /etc/sudoers file:
Defaults env_reset
See sudoers(5) for details.
This option causes the http_proxy environment variable to be dropped from the environment of the command that you start, e.g. try:
$ export http_proxy=test
$ env | grep http_proxy
$ sudo env | grep http_proxy
On the other hand, if you start sudo via sudo -i and are setting the http_proxy variable via /etc/profile.d/ then the proxy setting is correctly propagated to the environment, because the newly launched root shell sources /etc/profile (see sudo(8)).
This means you can work around the non-working sudo aptitude by sudo -i followed by aptitude.
But this workaround doesn't solve everything. It seems that Jockey loses the http_proxy variable in the process of installing packages.
So, what is the correct solution?
It turns out you can explicitly set the http proxy in apt configuration. Create a file in /etc/apt/apt.conf.d with a name like http-proxy (arbitrary) with the following content:
Acquire::http::Proxy "http://proxy:8080";
See apt.conf(5) for more info.
This way, no matter how is apt fired up (sudo, jockey, ...), it always uses the proxy.
Btw. it would be also possible to whitelist the http_proxy variable in /etc/sudoers, but resetting the environment and setting the proxy via apt.conf seems a (little) bit safer.


  1. This comment has been removed by a blog administrator.

  2. sudo -E apt-get install stuff

    also works