<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>
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.
ctstate):
NEW: The first packet of a flow (no existing entry).ESTABLISHED: A bidirectional connection that has been fully established.RELATED: A connection related to an existing one (e.g., an FTP data channel or an ICMP error response).INVALID: A packet that cannot be logically associated with any existing connection (possibly malformed or tampered).SNAT / DNAT / MASQUERADE) relies on conntrack to create and maintain address and port mappings, ensuring that return packets can be correctly translated back.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.
ESTABLISHED connection.NEW connections.RELATED to existing connections (for example, responses or auxiliary connections).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