conntrack

On this page 7

conntrack is the connection tracking subsystem of netfilter in the Linux kernel, together with the userspace command of the same name that lists, monitors and deletes its entries. In Linux networking, conntrack is what turns a packet filter into a stateful firewall: it remembers every flow that crosses the host so that rules can match packets by connection state rather than by headers alone.

The kernel side lives in the nf_conntrack module and is used by iptables, nftables, firewalld and UFW alike. The userspace side, shipped in the conntrack-tools package, talks to the kernel through the ctnetlink interface and is the standard way to inspect the state table on a running system.

How connection tracking records flows

When the first packet of a flow arrives, conntrack creates an entry keyed by a tuple of addresses, ports and protocol. Each entry stores two directions, the original tuple and the reply tuple, so that a returning packet is recognized as part of the same connection even when NAT has rewritten its addresses.

Entries are kept in a hash table in kernel memory, sized by nf_conntrack_buckets and capped by nf_conntrack_max. Each entry carries a protocol-specific timeout that is refreshed by matching traffic; when it expires, the entry is removed and later packets are treated as a new flow.

Firewall rules see a simplified connection state for every packet. The following table describes the states that rules can match with ct state in nftables or --ctstate in iptables.

StateMeaningTypical rule decision
NEWFirst packet of a flow not yet confirmedAccept only for allowed services
ESTABLISHEDPackets seen in both directionsAccept early in the rule set
RELATEDNew flow linked to an existing oneAccept, for example ICMP errors
INVALIDPacket that fits no valid flowDrop
UNTRACKEDPacket exempted from trackingHandle with explicit rules
Connection states visible to firewall rules

For TCP, conntrack additionally follows the protocol state machine with internal states such as SYN_SENT, ESTABLISHED, FIN_WAIT and TIME_WAIT. These internal states are what the userspace tool displays, and they decide which timeout applies to an entry.

Working with the conntrack command

The conntrack command gives administrators direct read and write access to the kernel table. It is far more precise than reading /proc/net/nf_conntrack, because it supports filters, event streams and deletion.

The table below lists the operations used most often in troubleshooting.

CommandPurposePractical note
conntrack -LList all tracked entriesAdd -p tcp –dport 443 to filter
conntrack -EStream new, updated and destroyed eventsIdeal for watching flows live
conntrack -DDelete matching entriesForces flows to be reevaluated
conntrack -FFlush the whole tableDisrupts NAT flows; use with care
conntrack -CPrint the current entry countCompare with nf_conntrack_max
conntrack -SShow per-CPU statisticsReveals drops, inserts and searches
Common conntrack operations

Deleting entries is often necessary after a firewall or NAT change, because existing flows keep their original translation until their entry expires. A long-lived UDP stream, for example, can continue using an outdated NAT mapping until it is removed with conntrack -D.

Timeouts and table sizing

Timeouts determine how long idle flows occupy memory, and they are tunable through sysctl keys under net.netfilter. Defaults differ between kernel versions, so the values below are typical rather than guaranteed.

Sysctl parameterTypical defaultApplies to
nf_conntrack_tcp_timeout_established432000 s (5 days)Idle established TCP connections
nf_conntrack_tcp_timeout_time_wait120 sTCP connections after close
nf_conntrack_udp_timeout30 sUDP flows seen in one direction
nf_conntrack_udp_timeout_stream120 to 180 sUDP flows seen in both directions
nf_conntrack_icmp_timeout30 sICMP echo exchanges
Typical default conntrack timeouts

The five-day TCP default is generous and can fill the table on busy NAT gateways with many abandoned connections. Lowering it, and raising nf_conntrack_max in proportion to available memory, is a standard tuning step for load balancers and container hosts.

Network address translation in Linux depends entirely on conntrack. The NAT decision is made once, on the first packet of a flow, and stored in the conntrack entry; all later packets are rewritten from that stored mapping without consulting the NAT rules again.

Connection tracking helpers extend the system for protocols such as FTP and SIP that negotiate extra ports inside their payload. Since kernel 4.7, automatic helper assignment is disabled by default, so helpers must be attached explicitly with ct helper rules, which closes a known abuse path.

Several neighboring components are often confused with conntrack, as the following comparison shows.

ComponentRoleRelation to conntrack
netfilterKernel packet filtering frameworkHosts conntrack as one of its modules
nftablesRule language and kernel tablesMatches ct state, ct mark and ct zone
iptablesLegacy rule front endUses the conntrack and state matches
conntrackdSynchronization daemonReplicates entries between firewall nodes
tcpdumpPacket capture toolShows packets, not tracked flow state
conntrack and neighboring components

Limitations and failure modes

The most familiar failure is the kernel message table full, dropping packet, which appears when the entry count reaches nf_conntrack_max. New flows are then silently dropped, which looks like random connection failures to users while existing flows continue to work.

  • Memory cost: every entry consumes kernel memory, so very large tables need RAM planning.
  • DDoS exposure: SYN floods and UDP floods create entries quickly and can exhaust the table.
  • Asymmetric routing: replies that bypass the host make flows look INVALID and get dropped.
  • Stale NAT mappings: rule changes do not affect flows that are already tracked.

High-volume stateless traffic, such as authoritative DNS, is often exempted with notrack rules in the raw table or an nftables prerouting chain. This removes the tracking cost entirely but also removes stateful protection for that traffic, so it must be filtered by explicit rules.

Put simply

conntrack is the memory of a Linux firewall. Every time a computer starts a conversation over the network, conntrack writes it down, so that when the answer comes back the firewall knows it was expected and lets it through.

It works like a receptionist with a visitor log. Guests who were invited get waved in, unknown arrivals are checked against the rules, and old entries are crossed out after a while. If the log runs out of pages, new visitors are turned away until space frees up.

In practice

An operations engineer runs a Linux NAT gateway that serves about 4,000 office and guest devices. Users begin reporting that some websites fail to load at peak hours, while ongoing video calls continue normally. The kernel log shows the table full message, and conntrack -C returns 262,144, exactly the configured nf_conntrack_max.

Running conntrack -L reveals that most entries are idle established TCP flows left by mobile devices that disconnected without closing their sessions. The engineer lowers the established timeout from five days to two hours, raises the table limit to 524,288 after checking memory headroom and makes both settings persistent in a sysctl file. Within an hour the entry count stabilizes at about 90,000, and the failures stop.