Skip to content

Linux conntrack tuning: table sizing, timeouts, and avoiding the drop

Learn how to stop a busy Linux router or NAT gateway from dropping packets when its connection-tracking table fills up. This guide shows you how to size nf_conntrack_max, tune the per-protocol timeouts, and read /proc to spot the problem early.

This article is for you if a busy Linux machine, such as a router, load balancer or NAT gateway, has started dropping packets. A NAT gateway is a box that translates private addresses to public ones. The usual sign is nf_conntrack: table full, dropping packet in dmesg, the kernel log. That message means the machine has run out of conntrack slots, even when memory and CPU look fine. Conntrack is the connection-tracking table that netfilter, the Linux firewall layer, uses to remember each connection. It is a fixed-size hash table with one entry per active connection, plus reservation slots for half-open ones. On a NAT gateway, an edge load balancer or any busy multi-purpose server, the default size and timeouts run out faster than people expect. You will learn how to size the table, tune the timeouts, and use a few observability commands that catch the squeeze before your users feel it.

How to verify

Check current usage, capacity, and per-protocol breakdown:

sysctl net.netfilter.nf_conntrack_max net.netfilter.nf_conntrack_count
sysctl net.netfilter.nf_conntrack_buckets
cat /proc/net/stat/nf_conntrack | column -t
conntrack -S
conntrack -L -o extended | awk '{print $1}' | sort | uniq -c
dmesg --notime | grep -i conntrack | tail

nf_conntrack_count divided by nf_conntrack_max is your headroom; alert at 75% and page at 90%. The conntrack -S output exposes per-CPU counters including insert_failed, drop, and early_drop, which name the failure mode when the table is under pressure.

What’s happening

Conntrack records one entry per flow, identified by the 5-tuple (src ip, src port, dst ip, dst port, protocol) plus the reverse tuple. Entries are inserted on the first packet of a flow (NEW) and removed when the per-protocol timeout expires after the last packet seen. UDP and ICMP entries are short-lived (30–60 seconds by default); TCP ESTABLISHED entries live up to 5 days unless the connection closes.

Two pressure modes appear in practice:

  • Capacity pressure: nf_conntrack_count approaches nf_conntrack_max. New flows fail to insert; the kernel logs table full and drops the SYN. This is the visible failure.
  • Hash collision pressure: even with capacity to spare, if nf_conntrack_buckets is too small, each bucket holds many entries and lookups become a linear walk. Symptoms are CPU spikes and search_restart counter increase in /proc/net/stat/nf_conntrack.

The default nf_conntrack_max is derived from RAM (RAMSIZE / 16384 / sizeof(struct nf_conn)), which on small VMs lands around 65k — too low for any edge box. Bucket count defaults to nf_conntrack_max / 4; the hash works best when buckets are roughly equal to the expected concurrent flow count.

The procedure

  1. Size the table for actual peak flows. As a starting point, take observed peak concurrent flows and multiply by 4. For a small edge router with ~50k concurrent flows:

    cat > /etc/sysctl.d/60-conntrack.conf <<'EOF'
    net.netfilter.nf_conntrack_max = 524288
    net.netfilter.nf_conntrack_buckets = 131072
    EOF
    sysctl --system
  2. Bucket count must be set at module load — runtime sysctl writes to nf_conntrack_buckets are accepted on newer kernels but ignored on older ones. The safe path is a modprobe config:

    cat > /etc/modprobe.d/nf_conntrack.conf <<'EOF'
    options nf_conntrack hashsize=131072
    EOF
  3. Tune per-protocol timeouts. Defaults assume idle flows might revive; on a high-churn edge, shorter timeouts free slots faster.

    cat >> /etc/sysctl.d/60-conntrack.conf <<'EOF'
    net.netfilter.nf_conntrack_tcp_timeout_established = 86400
    net.netfilter.nf_conntrack_tcp_timeout_time_wait = 30
    net.netfilter.nf_conntrack_tcp_timeout_close_wait = 30
    net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 30
    net.netfilter.nf_conntrack_udp_timeout = 30
    net.netfilter.nf_conntrack_udp_timeout_stream = 120
    net.netfilter.nf_conntrack_icmp_timeout = 30
    EOF
    sysctl --system
  4. Exclude tracked-traffic that does not need state. DNS responders and stateless HTTP redirectors can use raw/PREROUTING -j NOTRACK to bypass conntrack entirely:

    iptables -t raw -A PREROUTING -p udp --dport 53 -j NOTRACK
    iptables -t raw -A OUTPUT     -p udp --sport 53 -j NOTRACK
  5. Watch for SYN floods. A SYN flood inflates nf_conntrack_count with half-open entries that live as long as nf_conntrack_tcp_timeout_syn_recv (default 60s). Lower this aggressively on internet-facing boxes:

    sysctl -w net.netfilter.nf_conntrack_tcp_timeout_syn_recv=15
    sysctl -w net.netfilter.nf_conntrack_tcp_timeout_syn_sent=15
  6. Wire conntrack metrics into Prometheus. node_exporter exposes node_nf_conntrack_entries and node_nf_conntrack_entries_limit; an alert on the ratio crossing 0.75 catches the squeeze early.

    - alert: ConntrackTableFilling
      expr: node_nf_conntrack_entries / node_nf_conntrack_entries_limit > 0.75
      for: 5m
      labels:
        severity: warning

Operational notes

  • Restarting netfilter-persistent does not reload conntrack settings — they are loaded once at module load and via sysctl --system at boot. After changing hashsize, you need to unload and reload nf_conntrack, which kills all tracked flows (so do it in a maintenance window).
  • conntrack-tools is separate from iptables. Install it (apt install conntrack) on every gateway; conntrack -L, conntrack -E (event stream) and conntrack -D (delete) are essential for live debugging.
  • kube-proxy in iptables mode is a heavy conntrack user. Every Service IP plus every NodePort flow gets a conntrack entry; nodes with hundreds of pods routinely run with nf_conntrack_max at 1M+.
  • HAProxy and Nginx do not use conntrack for their reverse-proxy flows because both ends are local sockets, not NAT — but the client → frontend flow still occupies a conntrack slot.
  • early_drop is the gentle warning. When nf_conntrack_count exceeds 87.5% of max, the kernel starts evicting old entries to make room; early_drop increases. Once the table fully fills, insert_failed increases and you see dmesg drops.

In the engagements we run, conntrack tuning is one of the standard host hardening steps for any internet-facing box — load balancer, NAT gateway, K8s worker, ingress node. The defaults are tuned for a single-purpose desktop, not a 100k-flow edge router, and the cost of waiting for the first table full line in dmesg is a few seconds of TCP unreachability for real users. We bake the sysctl set above into the managed-operations Ansible role and surface the headroom metric on the console dashboard so the squeeze is visible long before it bites.