Marcio Cunha

How to Inspect NAT and Firewall Rules on Linux

Demystify packet routing and network address translation on Linux. Learn to use iptables and nftables to diagnose blocked traffic.

Marcio Cunha•5 min
Also available in:EspañolPortuguês
Summary
  • Packet routing on Linux determines where network data goes based on the kernel routing table.
  • Network address translation adjusts source and destination IPs to allow private networks to access the internet.
  • Iptables uses traditional tables and chains to filter packets and enforce security rules efficiently.
  • Nftables replaces the older ecosystem by unifying packet filtering into a modern, flexible infrastructure.
  • Connection tracking allows the firewall to recognize if a packet belongs to an already established session.

How the Linux Kernel Decides the Destination of Network Packets

When a Linux server receives a stream of data from the network, it must immediately decide what to do with each piece of incoming information. In practice, this means the operating system analyzes packet headers—which act like the address and sender on a postal letter—to understand whether the message is intended for itself or if it should be forwarded to another device. This core process is known as packet routing.

To perform this task without stuttering, the kernel maintains an internal routing table indicating which network interface should be used to reach specific destinations. When a packet arrives and finds no clear path, it is silently dropped or generates an error message back to the sender. Understanding this initial flow is the first step in any network diagnosis, especially when figuring out why a cloud application cannot talk to its database.

The Role of Network Address Translation in Modern Connectivity

Often, computers inside a corporate or home network use private IP addresses that do not work directly on the public internet. To solve this address scarcity problem, we use network address translation, commonly known as NAT. In practice, this mechanism alters the source IP address of an outgoing packet to a valid public IP, keeping a record in an internal table to remember who sent the original message.

When the response from the external server arrives, the Linux router or server consults this record, undoes the alteration, and delivers the data back to the correct computer on the internal network. This clever trick saves billions of IP addresses worldwide, but it also adds complexity when debugging why an external connection fails. If the translation table becomes corrupt or runs out of memory, connections simply stop working, generating that frustrating feeling that the internet is half-broken.

Uncovering Blocked Traffic with Traditional Iptables

For years, iptables was the standard utility on Linux for configuring packet filtering and NAT rules, operating directly on top of kernel structures called netfilter. In practice, it organizes rules into tables like filter for security and nat for address translation, divided into chains representing the moments when a packet passes through the system. If you want to inspect what is blocking access to a specific port, listing these active rules is the first logical step.

To inspect current firewall rules along with active packet counters, we run a command that shows exactly how many messages passed through each security rule:

sudo iptables -L -v -n --line-numbers

This command outputs a detailed report containing the rule number, the number of processed packets, and the corresponding action, helping quickly identify if a packet is hitting a generic drop rule. For deeper investigations into address translation and active connections, we can combine diagnostic utilities like how to use tcpdump to capture network packets on Linux directly on the relevant network interface, isolating the exact problem.

Migrating to Nftables and Its Unified Architecture

Although iptables still works on most traditional Linux distributions, it suffers from code duplication and performance drops when rule volumes grow massively in large data centers. To solve these limitations, the community developed nftables, replacing the older framework with a single unified syntax to manage both IPv4 and IPv6 firewalls. In practice, nftables eliminates the need for separate tools and offers much more efficient data structures, like hash tables, that speed up rule lookup on high-traffic servers.

To list all rules configured in the new nftables system in a human-readable format, we use a straightforward command that replaces all the complexity of the previous ecosystem:

sudo nft list ruleset

This organized listing by tables and protocol families greatly simplifies security auditing and the identification of misconfigured NAT rules. Unlike iptables, where each table was managed in isolation, nftables allows grouping multiple inspection criteria into a single command line, making network troubleshooting much faster and more predictable for system administrators.

Diagnosing Connection Tracking and Network States

One of the most important innovations in modern Linux-based firewalls is connection tracking, known as conntrack, which allows the system to view packets not as isolated events, but as part of an ongoing conversation. In practice, this means if we allow a packet to leave our server to reach an external site, the firewall notes that the connection is valid and automatically authorizes the return response without requiring a complex inbound rule.

When an application suffers from mysterious drops or hung connections, inspecting the kernel state table helps reveal resource exhaustion or stuck zombie connections. We can view currently active connections processed by the NAT and firewall subsystem by running the following command:

sudo conntrack -L

This listing displays the protocol used, the remaining lifetime of the connection, and the addresses involved at both ends of the communication. If the table is full, new packets will be dropped regardless of configured firewall rules, a classic systemic failure scenario that confuses many beginner professionals. To complement this implementation in production environments, see our related technical analyses on How to test quartz crystal oscillator on circuit board and Why gRPC and Protocol Buffers Outperform Traditional JSON APIs.

Final Thoughts on Traffic Inspection in Linux

Mastering the inspection of NAT and firewall rules on Linux transforms how we approach complex infrastructure and network connectivity issues. Instead of guessing why a service became inaccessible, the administrator relies on an empirical methodology based on correctly reading kernel states, routing tables, and packet counters. As we explored throughout this article, tools like iptables, nftables, and connection tracking form a powerful ecosystem to ensure security and proper data flow in any server environment.

Maintaining technical curiosity and testing these commands in controlled environments is the safest path to gain confidence in operating Linux systems in production. Whether debugging legacy rules or migrating structures to nftables, clarity on the path each packet takes inside the machine is the ultimate key to keeping services resilient and free from hidden bottlenecks.