Debian: apt-cacher-ng

apt-cacher-ng is a caching proxy built for software packages. The first machine that installs or upgrades a package fetches it from the internet. Every machine after that gets it from your LAN, in seconds. It’s made for Debian and its derivatives, but it also caches Ubuntu and several other distributions. Fleet upgrades get faster, your internet uplink stays free, and patch day stops being a bandwidth event.

This guide sets up apt-cacher-ng on Debian 13 so that it:

  • Caches every Debian mirror in one place, whichever mirror a client uses
  • Is reachable only from your LAN, and is not an open proxy
  • Protects its admin page with a password
  • Caches HTTPS repositories (Docker, Wazuh, …) as well, without opening a generic tunnel
  • Finds itself automatically on clients, which fall back to direct downloads if the cache is down
  • Cleans itself up and is tested end to end

Tested on: Debian 13 “trixie” with the Debian package apt-cacher-ng, and Debian 13 clients. All commands run as root (or with sudo).


Table of contents

  1. Placeholders
  2. How it works
  3. Install apt-cacher-ng
  4. Firewall
  5. Server configuration
  6. Protect the admin page
  7. Configure the clients
  8. HTTPS repositories
  9. Testing
  10. Day-2 operations
  11. Troubleshooting
  12. Checklist

1. Placeholders

PlaceholderMeaning
apt-proxy.example.orgFQDN of the cache server
10.20.0.7The cache server’s LAN IP address
10.20.0.0/16Your client network(s)
example.orgYour internal DNS domain (for auto-discovery)

2. How it works

StepWhat happens
1The client’s APT sends its request to http://apt-proxy.example.org:3142 instead of to the mirror
2apt-cacher-ng serves the file from /var/cache/apt-cacher-ng if it has it, or downloads it once and caches it
3APT checks every package against the repository’s signed metadata, exactly as without the cache

Two consequences to understand:

  • Plain HTTP between client and cache is fine. APT’s security comes from signatures, not from the transport. A tampered package fails verification, whoever delivered it. The only thing HTTP reveals on your LAN is which packages a machine downloads.
  • HTTPS repositories can’t be cached by a normal proxy, because the proxy can’t see inside a TLS tunnel. Section 8 shows the clean way around this.

Mirror merging: apt-cacher-ng knows the Debian, Ubuntu and other mirror lists (/usr/lib/apt-cacher-ng/deb_mirrors.gz and friends). A client using deb.debian.org and another using ftp.de.debian.org share one cache entry for the same file.


3. Install apt-cacher-ng

apt update
apt install apt-cacher-ng

The installer asks one question: “Allow HTTP tunnels through Apt-Cacher NG?” Answer No. “Yes” writes PassThroughPattern: .* to /etc/apt-cacher-ng/zz_debconf.conf, which lets anyone who can reach port 3142 tunnel to any host and port on the internet. That’s an open proxy. Section 8 shows how to allow only the HTTPS repositories you need.

If it was answered “Yes” earlier, reverse it:

dpkg-reconfigure apt-cacher-ng          # answer "No" to the tunnel question
grep -r PassThroughPattern /etc/apt-cacher-ng/Code language: PHP (php)

Debian enables and starts apt-cacher-ng.service automatically, so you don’t need separate systemctl enable or systemctl start commands:

systemctl status apt-cacher-ng --no-pager
ss -tlnp 'sport = :3142'Code language: JavaScript (javascript)

Disk space: the cache lives in /var/cache/apt-cacher-ng. For a few dozen Debian hosts, plan 20โ€“50 GB. Give it its own filesystem (LVM volume) if you can, so a full cache never fills /var.


4. Firewall

apt-cacher-ng must only serve your LAN. Create /etc/nftables.d/apt-cacher-ng.nft:

install -d -m 0755 /etc/nftables.d

cat > /etc/nftables.d/apt-cacher-ng.nft <<'EOF'
table inet apt_cacher_ng {
    set clients_v4 {
        type ipv4_addr
        flags interval
        elements = { 10.20.0.0/16 }
    }

    chain input {
        type filter hook input priority filter - 10; policy accept;

        iif "lo" accept
        tcp dport 3142 ip saddr @clients_v4 accept
        tcp dport 3142 counter drop
    }
}
EOF

grep -q 'nftables.d' /etc/nftables.conf || \
  echo 'include "/etc/nftables.d/*.nft"' >> /etc/nftables.conf

nft -c -f /etc/nftables.conf && systemctl enable --now nftables && systemctl reload nftablesCode language: JavaScript (javascript)

Why a separate table with a final drop? In nftables, a drop in any base chain is final, so this table blocks the port no matter what your other tables do. If your main ruleset uses policy drop, also allow tcp/3142 from the LAN there.


5. Server configuration

apt-cacher-ng reads every *.conf file in /etc/apt-cacher-ng/, in alphabetical order, and later values win. Leave Debian’s acng.conf untouched (it’s a conffile) and put your settings in a file that sorts last, after zz_debconf.conf.

Create /etc/apt-cacher-ng/zzz-local.conf:

# /etc/apt-cacher-ng/zzz-local.conf: local settings (read last, values win)

# Listen on the LAN address and localhost only, not on every interface
BindAddress: 10.20.0.7 localhost
Port: 3142

# Upstream connections only to the standard HTTP port; no arbitrary ports
AllowUserPorts: 80

# HTTPS tunnels (CONNECT) only to these repositories, nowhere else.
# Tunnelled traffic is NOT cached; prefer the HTTPS/// method from section 8.
# Leave this line out entirely if you don't need any tunnel.
PassThroughPattern: ^(download\.docker\.com|packages\.wazuh\.com):443$

# Expiration: keep files that no index references any more for 4 more days
ExThreshold: 4

# Normal logging (0 = minimal, 1 = default); raise only while debugging
VerboseLog: 1Code language: PHP (php)
systemctl restart apt-cacher-ng
journalctl -u apt-cacher-ng -n 20 --no-pager
ss -tlnp 'sport = :3142'      # 10.20.0.7:3142 and 127.0.0.1:3142 onlyCode language: PHP (php)
SettingWhy
BindAddressOnly the LAN interface and localhost listen, not public or management interfaces
AllowUserPorts: 80Clients can’t make the cache fetch from arbitrary ports, such as internal admin UIs
PassThroughPatternThe one setting that turns a package cache into a general proxy. An anchored list of host:443 patterns allows exactly the repositories you name, and never .*.
ExThresholdHow long unreferenced files survive the daily cleanup

Pick your preferred Debian mirror: /etc/apt-cacher-ng/backends_debian lists the upstream mirror(s) apt-cacher-ng uses for all Debian requests, whichever mirror the client names. Set it to http://deb.debian.org/debian/, the CDN, or to a fast mirror near you.


6. Protect the admin page

http://apt-proxy.example.org:3142/acng-report.html shows statistics and has maintenance buttons: expiry, import, deleting files. Protect those operations with a password in /etc/apt-cacher-ng/security.conf:

PW=$(openssl rand -base64 24)
echo "AdminAuth: admin:${PW}" >> /etc/apt-cacher-ng/security.conf
echo "Admin password: ${PW}"    # store it in your password manager
unset PW

chown apt-cacher-ng:apt-cacher-ng /etc/apt-cacher-ng/security.conf
chmod 0600 /etc/apt-cacher-ng/security.conf
systemctl restart apt-cacher-ngCode language: PHP (php)

The password is stored in plain text, which is why the file must be 0600 and owned by the apt-cacher-ng user. The statistics stay visible without it. Only the actions need it.


7. Configure the clients

The auto-apt-proxy package finds the cache on its own, and uses direct downloads when no cache answers. A laptop that leaves the office, or a cache that’s down for maintenance, never breaks apt.

Publish the cache in DNS once (in your internal zone):

_apt_proxy._tcp.example.org.  3600  IN  SRV  0 0 3142 apt-proxy.example.org.Code language: CSS (css)

Then on every client:

apt install auto-apt-proxy
auto-apt-proxy          # prints http://apt-proxy.example.org:3142 when foundCode language: PHP (php)

auto-apt-proxy checks, in order: a cache on localhost, on the default gateway, a host named apt-proxy, and the DNS SRV record. It hooks itself into APT through Acquire::http::Proxy-Auto-Detect, so you don’t need to configure anything else.

7.2 Option B: static

For servers that always sit next to the cache, a fixed setting is simpler. Create /etc/apt/apt.conf.d/01proxy:

// /etc/apt/apt.conf.d/01proxy: use the LAN package cache for HTTP repositories
Acquire::http::Proxy "http://apt-proxy.example.org:3142";Code language: PHP (php)

The catch: if the cache is down, apt update fails on these clients until it’s back.

7.3 Option C: at installation time

The Debian installer asks for an HTTP proxy on the “Configure the package manager” screen. Enter http://apt-proxy.example.org:3142, and the whole installation comes from the cache. For automated installs, preseed it:

d-i mirror/http/proxy string http://apt-proxy.example.org:3142Code language: JavaScript (javascript)

Keep the Debian sources on http://. Debian 13’s default sources (/etc/apt/sources.list.d/debian.sources) use http://deb.debian.org, and that’s exactly what the cache needs. Switching them to https:// makes them bypass the cache. As section 2 explains, http:// isn’t a security problem for APT.


8. HTTPS repositories

Many third-party repositories (Docker, Wazuh, Grafana, …) only offer HTTPS. There are two ways to handle them.

The HTTPS/// method (cached, recommended). The client talks plain HTTP to the cache, and apt-cacher-ng fetches over HTTPS and caches the result. In the client’s .sources file, replace https:// with http://HTTPS///:

Types: deb
URIs: http://HTTPS///download.docker.com/linux/debian
Suites: trixie
Components: stable
Signed-By: /etc/apt/keyrings/docker.ascCode language: HTTP (http)

The repository’s Signed-By key still verifies everything. The catch: this URI only works through the cache, so use it on clients with Option A or B, not on machines that sometimes go direct.

Passthrough (not cached). Clients keep https:// URLs, and the cache only forwards the encrypted tunnel. That needs the PassThroughPattern from section 5 for each host, plus this on the clients:

Acquire::https::Proxy "http://apt-proxy.example.org:3142";Code language: PHP (php)

It saves no bandwidth and only centralises egress. Use it when a repository breaks with HTTPS///.


9. Testing

9.1 The cache answers and caches

On a client:

apt-get update
apt-get install --reinstall --download-only -y hello
apt-get clean
apt-get install --reinstall --download-only -y hello    # second download comes from the cacheCode language: JavaScript (javascript)

On the server, the second request shows as a hit:

tail -n 5 /var/log/apt-cacher-ng/apt-cacher.logCode language: JavaScript (javascript)

The report page http://apt-proxy.example.org:3142/acng-report.html shows the hit ratio for requests and for data volume.

9.2 It isn’t an open proxy

From a client in the LAN:

# CONNECT to an arbitrary host must be refused
curl -s -o /dev/null -w '%{http_connect}\n' -x http://apt-proxy.example.org:3142 https://example.com/
# Expect: 403 (or another refusal), never 200

# Fetching a non-package URL must be refused too
curl -s -o /dev/null -w '%{http_code}\n' -x http://apt-proxy.example.org:3142 http://example.com/
# Expect: 403Code language: PHP (php)

From a host outside 10.20.0.0/16, the connection to port 3142 must time out (firewall).

9.3 The fallback works (Option A)

systemctl stop apt-cacher-ng          # on the server
auto-apt-proxy; apt-get update        # on a client: no proxy printed, update succeeds directly
systemctl start apt-cacher-ng         # on the serverCode language: PHP (php)

10. Day-2 operations

TaskHow
CleanupA daily job (/etc/cron.daily/apt-cacher-ng) expires files no index references any more. Check its result on the report page.
Disk usagedu -sh /var/cache/apt-cacher-ng. Lower ExThreshold or grow the volume if it creeps up.
Pre-fill from a DVD or old cacheCopy .deb files to /var/cache/apt-cacher-ng/_import/, then click Start Import on the report page
Logs/var/log/apt-cacher-ng/apt-cacher.log (transfers) and apt-cacher.err (errors). Debian rotates them.
Updatesapt-cacher-ng comes from the Debian archive, so unattended-upgrades covers it
MonitoringCheck http://apt-proxy.example.org:3142/acng-report.html (HTTP 200) and the free space on /var/cache/apt-cacher-ng

11. Troubleshooting

SymptomCause / fix
Clients: Could not connect to apt-proxy.example.org:3142The service is down, the firewall blocks the client, or BindAddress doesn’t include the LAN IP
Clients: 403 Forbidden for an HTTPS repositoryIt isn’t in PassThroughPattern. Use the HTTPS/// method instead, or add an anchored host:443 pattern.
Clients: 404 Not Found for a third-party repositoryapt-cacher-ng doesn’t know the repository’s layout. Check apt-cacher.err. The HTTPS/// method or passthrough usually solves it.
Hash Sum mismatchUsually a mirror in the middle of a sync. Run apt-get update again later. If it persists, delete the affected index files on the report page (Expiration with “validate by file name”)
Nothing gets cachedClients use https:// sources and go around the cache. Switch Debian sources back to http:// (section 7).
Cache disk fullRun the expiration from the report page, lower ExThreshold, and give the cache its own volume

12. Checklist

  • โ˜ The tunnel question was answered No, and no PassThroughPattern: .* exists anywhere in /etc/apt-cacher-ng/
  • โ˜ Port 3142 is reachable only from the LAN (nftables), and BindAddress is limited to the LAN IP and localhost
  • โ˜ Local settings live in zzz-local.conf, and Debian’s acng.conf is untouched
  • โ˜ PassThroughPattern, if used, is an anchored list of host:443 entries
  • โ˜ The admin page actions require AdminAuth, and security.conf is 0600
  • โ˜ Clients use auto-apt-proxy (with the DNS SRV record) or a static 01proxy file
  • โ˜ Debian sources stay on http://, and HTTPS repositories use http://HTTPS/// or a deliberate passthrough
  • โ˜ The cache has its own volume, or at least monitored disk space
  • โ˜ Tests pass: cache hit, CONNECT to a random host refused, outsiders blocked, fallback works