Following the instructions in Best way to cache apt downloads on a LAN?, I've set up a caching proxy in my local network. Since that machine is not always up, I'd like to be able to refresh the sources list and install packages without using that proxy if unavailable.
I've already read the Acquire group section in the manual page of apt.conf(5)
, but I couldn't find an option like "Silent-Fail".
At the moment, sudo apt-get update
and related commands fail because no connection could be established. So how do I configure the client so that the proxy is ignored if it's not available?
There is an undocumented setting,
Acquire::http::ProxyAutoDetect
. This setting should contains the full path to the binary and cannot have arguments. The command should output the proxy to be used (example:http://10.0.0.1:8000
).Given the above information, a script could be created that tries a proxy before setting it. If no proxy is available, a direct connection should be used.
Below is such a proxy detection script which tries the
http://10.0.0.1:8000/
andhttp://10.0.0.2:8000
proxies.Put the code in
/etc/apt/detect-http-proxy
:Now, APT must be configured to use the above proxy detection script, so put the following code in
/etc/apt/apt.conf.d/30detectproxy
:I've also put the next code to the file to prevent some host from being proxified.
By default, the script outputs whether a proxy is used or not. To disable that, edit
/etc/apt/detect-http-proxy
and changeshow_proxy_messages=1
toshow_proxy_messages=0
.There's now an officially supported way to do this - using the option -
Acquire::http::Proxy-Auto-Detect
(seeapt.conf
man page). Behaviour is similar to the old undocumentedAcquire::http::ProxyAutoDetect
(note presence/absence of hyphens in new/old config options), it's largely backwardly compatible, but has been extended...I'm in the process of submitted a patch to the apt maintainers to improve the documentation, but since this is unlikely to make it into a version of apt which ships with a distro release for quite a while, I'll include the text of the proposed patch here:
Acquire::http::Proxy-Auto-Detect
can be used to specify an external command to discover the http proxy to use. APT may invoke the command multiple times, and will pass a URI to the command as its first and only parameter. APT expects the command to output the proxy which is to be used to contact the URI in question on its stdout as a single line in the stylehttp://proxy:port/
, or the wordDIRECT
if no proxy should be used. No output indicates that the generic proxy settings should be used.Note that auto-detection will not be used for a host if a host-specific proxy configuration is already set via
Acquire::http::Proxy::HOST
.To diagnose interactions with the external command, set
Debug::Acquire::http=yes
and/orDebug::Acquire::https=yes
e.g using the-o
command line parameter.Note that is using a pre-release version of apt, versions 1.3~exp2 to 1.3 then there is a bug (likely fixed by 1.3.1) which causes apt to parse the stderr of the external command along with the stdout.
/etc/apt/apt.conf.d/02proxy
:/usr/local/bin/apt-proxy-detect.sh
:Command Line
nc
to work (sudo apt-get install netcat
) if missing.chmod +x /usr/local/bin/apt-proxy-detect.sh
How it works
If it can connect to a proxy, it prints the proxy out APT uses that. If it can't, it prints out DIRECT and APT chugs along normally.
sauce
I know this question is old, but it's still relevant and I'd like to add another answer.
One of the simplest, most portable and transparent ways these days is to install the squid-deb-proxy-client package on the nodes.