What Is Connection Tracking?

<aside> 💡

Conntrack, short for connection tracking, is a core feature of the Linux kernel’s Netfilter subsystem.

It records and manages the state of every network connection and data flow, enabling the kernel to understand whether a packet belongs to a new, established, or related connection. This mechanism is fundamental for implementing stateful firewalls and Network Address Translation (NAT) , forming the backbone of many higher-level networking systems such as container networking and Kubernetes Services.

</aside>

Connection Tracking Table

A table maintained by the Linux kernel that records the state and NAT mappings of each *flow* based on the five-tuple (src IP, dst IP, src port, dst port, protocol). Each record in this table is called an entry . Conntrack continuously maintains this table and uses it to determine which existing connection each packet belongs to.

Why Do We Need Conntrack?

When Using Only iptables to Filter Packets

In previous sections (Demystifying Container Networking ), we examined iptables rules extensively to understand which packets are forwarded, accepted by the host, or dropped.

However, if we rely only on iptables rules (for example, a simple rule like iptables -A INPUT -p tcp --dport 22 -j ACCEPT), each packet is evaluated independently — iptables has no memory of past packets.

This means the kernel cannot determine whether a packet is part of an existing connection or simply a new incoming request. As a result, it becomes difficult to distinguish between new packets and response packets.

With the Help of Conntrack

With this information, firewall rules can be written in a simpler and safer way like below.

# Allow Packets from Established or Related Connections
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

Deep Dive into ctstate