Interview Prep/Networking/Sockets, ports, TIME_WAIT and conntrack

`dmesg` is full of `nf_conntrack: nf_conntrack: table full, dropping packet` and new connections randomly time out.

IntermediateTroubleshootingSockets, ports, TIME_WAIT and conntrack
Answer

Likely causes → The conntrack table hit nf_conntrack_max. Every tracked flow (including short DNS and health-check flows) holds an entry until its timeout expires, so a traffic spike, a scan, or long TIME_WAIT/UDP timeouts fill it. When full, the kernel drops new flows silently, which looks like random timeouts.

Diagnose →

sysctl net.netfilter.nf_conntrack_count net.netfilter.nf_conntrack_max
sudo conntrack -S | head                       # look at drop / early_drop counters
sudo conntrack -L -o extended | awk '{print $3}' | sort | uniq -c | sort -rn   # by protocol
sudo conntrack -L | awk '{print $5}' | sort | uniq -c | sort -rn | head        # top sources

Fix → Raise the limit and the hash size together, and shorten timeouts for flows you don't need held long:

sudo sysctl -w net.netfilter.nf_conntrack_max=524288
echo 131072 | sudo tee /sys/module/nf_conntrack/parameters/hashsize
sudo sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=30

For high-volume stateless traffic (e.g. a DNS server), exempt it with iptables -t raw -A PREROUTING -p udp --dport 53 -j NOTRACK.

Prevent → Persist values in /etc/sysctl.d/, export nf_conntrack_count / nf_conntrack_max to monitoring and alert at 75%.

All Networking questions Roadmap: Networking for DevOps

More on Sockets, ports, TIME_WAIT and conntrack