Can I easily make a host "mirror" that caches downloaded Debian packages?

For having a local deb cache to server my Debian server farm, I actually prefer to use apt-cacher-ng (caching proxy server for software repositories)

It is a proxy specially APT/deb aware, quite customisable and can cache your deb files for quite a while (configurable).

You install it with:

apt-get install apt-cacher-ng

And by default it caches repositories/debs into /home/apt-cacher-ng. Under this directory, it creates a directory per repository used in your Debian/Ubuntu servers, then distros used, much similar to mirror structures. As an added bonus, is also much easier to fetch manually a deb from cache from here, than from a Squid server.

To use it in all your servers, add to the directory /etc/apt/apt.conf.d a file 02proxy with the contents:

Acquire::http { Proxy "http://your_proxy_APT_server:3142"; };

After you add that file, the Debian package manager will proxy all the configured repositories via the configured http APT proxy.

It also got an interesting statics page for consulting it´s activity.

You might also need to open 3142/TCP in your firewalls to allow the servers to talk with your new proxy APT server.

The advantage of such setup is that besides downloading only one deb copy for a bucketload of servers, and saving bandwidth and the public repositories usage is that allows you to update internal servers that do not need to have Internet access (example: DHCP servers).

As documented in Appendix B of the Official Install Guide, you can have your DHCP server give out a preseed file, by adding something like this to it's config:

if substring (option vendor-class-identifier, 0, 3) = "d-i" {
    filename "http://host/preseed.cfg";
}

Then using these preseed options, you can configure the mirror and proxy automatically:

d-i mirror/protocol string http
d-i mirror/country string manual
d-i mirror/http/hostname string http.us.debian.org
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string http://your_proxy_APT_server:3128/

See also: How to set up Apt caching server on Ubuntu or Debian


edit: The preseeding approach I edited in to Rui F Ribeiro's answer works with Squid—or any other proxy—as well, and (as long as you're using DHCP) is likely a better approach.


First, mirror will be completed with the system domain name, so add a DNS entry for mirror.yourdomain.com. Point that to a server running Apache; in this example, the same server also runs Squid.

Make sure Apache has both mod_proxy and mod_proxy_http enabled (e.g., a2enmod proxy; a2enmod proxy_http on Debian). Then add a new virtual host:

<VirtualHost *:80>
    ServerName mirror.yourdomain.com
    ServerAlias mirror

    ProxyPass /debian http://http.us.debian.org/debian
    ProxyRemote http http://localhost:3128/
    ProxyVia off
</VirtualHost>

After restarting Apache, package downloads from the Debian mirror "mirror" should go through the cache.