Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using apt-cacher-ng at my local network with the following configuration on the clients:

Acquire::http { Proxy "http://acng-host:3142"; };

Some of the clients are laptops, so how do I configure them to use the cache only when it's available on this network?

share|improve this question
up vote 2 down vote accepted

Something like this should work:


nc -w 1 $ip $port
if [ $? -eq 0 ]; then
    echo "Acquire::http { Proxy \"http://$ip:$port\"; };" > $proxy_file
    echo 'Acquire::https { Proxy "false"; };' >> $proxy_file
    rm -f $proxy_file

Fix permissions

sudo chmod +x /etc/NetworkManager/dispatcher.d/99SetAptProxy


  • The "nc" command tests that it can connect to the 3142 port on the given IP address.
  • This script is run everytime the networking interfaces are changed by network manager.
  • Feel free to alter the way that you detect for the proxy, this works for me, but it is a security vulnerability if you install packages on a foreign network, for example.
share|improve this answer

From the server you can announce to the network there is an apt-cacher-ng instance through avahi

From the client you can check if exist an apt-cacher-ng service and modify apt proxy settings accordly.



$ sudo apt-get install apt-cacher-ng squid-deb-proxy-client

Put the following snippet in /etc/avahi/services/apt-cacher-ng.service

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
 <name replace-wildcards="yes">apt-cacher-ng proxy on %h</name>
 <service protocol="ipv4">



$ sudo apt-get install squid-deb-proxy-client

Here is a snippet to install server|client depending if there is already an apt-get proxy or not:


share|improve this answer

Not an exact answer, since you'll have to change your existing cache setup, but squid-deb-proxy is a good solution to providing a cache that will be used transparently if available, and is very easy to set up on both cache server and clients. See the first answer to this question for more details.

share|improve this answer
... that was hopefully meant to be "that will be used transparently if available and if avahi is installed on the client side" ;) – 0xC0000022L Feb 28 '13 at 20:34

Your Answer


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.