Sub topic 5
Many hosts, one public address: source NAT and PAT
This chapter assumes you know IPv4 addressing and subnet masks, what a default gateway does, how TCP and UDP use port numbers, and how to run commands in a Linux shell. Earlier chapters covered the 5-tuple and the translation table. Here you will see how they combine to let a whole LAN share one public address.
Source NAT: rewriting who the sender is
Source NAT (SNAT) rewrites the source address of packets that leave a private network. The destination address is untouched. The NAT device applies the rewrite to the first packet of a connection and records it in the translation table. Every later packet of that connection gets the same treatment, and replies are translated back.
SNAT is a family of behaviors, split by what gets rewritten:
| Variant | Rewrites | Typical use |
|---|---|---|
| Basic SNAT (one-to-one) | Source address only | One private host gets one public address |
| SNAT with a pool | Source address, chosen from several public addresses | Large sites with a block of public IPs |
| PAT (port address translation) | Source address and source port | Many private hosts share one public address |
PAT is also called NAT overload, NAPT, or masquerading. It is what a home router does. Everything below focuses on PAT because it is the case where port handling matters.
Why the port must change
Two LAN hosts can easily pick the same ephemeral source port. Suppose 192.168.1.10 and 192.168.1.11 both open a connection from port 50000 to the same server, 198.51.100.7:443. After the source address is rewritten to the shared public address 203.0.113.5, the server would see two connections with an identical 5-tuple: TCP, 203.0.113.5:50000 to 198.51.100.7:443. The gateway could not tell the replies apart.
PAT solves this by treating the NAT device's public address and port as a scarce resource. When a new connection would collide with an existing mapping, the gateway picks a different source port for it. The rule is that each translated 5-tuple must be unique.
The diagram shows two hosts that collide on port 50000.
On the return path the gateway looks up the destination port in its table. Port 50001 maps to Host B and port 50000 maps to Host A, so each reply is restored to the original private address and port.
How Linux picks the translated port
Linux connection tracking (conntrack) tries to preserve the original source port. It changes the port only when the translated tuple would collide with an existing entry. When it must change the port, it chooses within a range based on the original port:
- Ports below 512 map to ports below 512.
- Ports 512 to 1023 map to ports below 1024.
- All other ports map into 1024 to 65535.
Because uniqueness is checked on the whole 5-tuple, the same public port can be reused for connections to different destinations. One public address therefore supports far more than 65,535 simultaneous connections, as long as no single destination address, port, and protocol combination exceeds roughly 64,000. Heavy traffic to one popular server is where exhaustion first appears.
Configuring PAT on Linux
First make sure the host forwards packets between interfaces:
# Allow the Linux host to forward packets between interfaces
sudo sysctl -w net.ipv4.ip_forward=1This setting is lost on reboot. Persist it in a file under /etc/sysctl.d/ if the host is a permanent gateway.
The usual rule is masquerading:
# eth0 = public/WAN interface, 192.168.1.0/24 = private LAN
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADEThe rule belongs in the POSTROUTING chain of the nat table. Routing has already chosen the outgoing interface at that point, which is why -o eth0 works. Only the first packet of each connection traverses the nat table. After that, conntrack applies the stored translation automatically.
MASQUERADE versus SNAT
MASQUERADE reads the outgoing interface's current address each time it creates a mapping. That suits interfaces with a dynamic address, such as DHCP or PPPoE. If the interface goes down or its address changes, Linux flushes the conntrack entries that used the old address. Those connections could not survive anyway.
If the public address is static, use SNAT with an explicit address. It avoids the per-connection address lookup and does not flush entries when the link flaps:
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 203.0.113.5You can also restrict the port range, or give a pool of addresses:
# Restrict translated source ports
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -p tcp -j SNAT --to-source 203.0.113.5:10000-20000
# Spread connections across a pool of public addresses
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 203.0.113.5-203.0.113.8The equivalent in nftables:
sudo nft add table ip nat
sudo nft 'add chain ip nat postrouting { type nat hook postrouting priority srcnat; policy accept; }'
sudo nft add rule ip nat postrouting oifname "eth0" ip saddr 192.168.1.0/24 masquerade
# For a static address, use snat instead of masquerade:
# sudo nft add rule ip nat postrouting oifname "eth0" ip saddr 192.168.1.0/24 snat to 203.0.113.5Watching the translation happen
The conntrack tool lists the table. Each line shows the original tuple first and the reply tuple second:
# Inspect the kernel's connection tracking table (NAT mappings)
sudo conntrack -L -p tcpAn abbreviated and illustrative line for Host B from the diagram looks like this:
tcp 6 431999 ESTABLISHED src=192.168.1.11 dst=198.51.100.7 sport=50000 dport=443 src=198.51.100.7 dst=203.0.113.5 sport=443 dport=50001 [ASSURED]Read it this way. The first half is what the LAN host sent. The second half is what the server will send back. Where the reply half does not mirror the original half, translation is happening. Here the reply is addressed to 203.0.113.5:50001, not to 192.168.1.11:50000.
Limits and failure modes
Port and table exhaustion
Two separate limits can bite:
- Port exhaustion. Too many simultaneous connections from one public address to the same destination IP, port, and protocol leave no free port. New connections fail or stall, while connections to other destinations still work.
- Table exhaustion. The conntrack table has a maximum size (
net.netfilter.nf_conntrack_max). When it fills, the kernel logsnf_conntrack: table full, dropping packetand drops new flows. Check usage withsudo conntrack -C, and compare it to the maximum.
Common causes are port scanners, misbehaving clients that open connections in a loop, and short-lived DNS or UDP traffic at high rates.
Idle timeouts
A mapping exists only while the table entry lives. Linux defaults differ by protocol. Established TCP entries last five days. UDP entries last 30 seconds, extended to 120 seconds once traffic has flowed in both directions. When an entry expires, the next packet from the outside finds no mapping and is dropped, and a new outbound packet may get a different public port. Applications behind NAT therefore send keepalives, often every 15 to 25 seconds for UDP.
Existing connections keep their old translation
Rules are evaluated only for the first packet of a flow. If you edit a rule, already tracked connections continue under the old translation until they expire. Delete entries with sudo conntrack -D when you need a change to take effect immediately.
Unsolicited inbound traffic
PAT creates mappings only for outbound connections. A packet from the internet to 203.0.113.5:50000 has no entry unless an inside host started that flow. This behavior is a side effect that looks like a firewall, but it is not a security policy. Reaching a private service on purpose needs destination NAT, which a later chapter covers.
Common errors
- Forwarding disabled. If
net.ipv4.ip_forwardis 0, LAN packets arrive at the gateway and go no further. - Wrong interface in
-o. The-ooption names the outgoing interface. Using the LAN interface means the rule never matches. - Wrong source match. A
-svalue that does not cover the LAN subnet leaves those hosts untranslated. Their packets leave with private source addresses and are dropped by the first ISP router. - A drop policy in FORWARD. If the filter table's
FORWARDchain defaults toDROP, you need explicit accept rules for LAN-to-WAN traffic and for established return traffic. - Clients with the wrong gateway. NAT only applies to traffic that reaches the gateway. Check that LAN hosts use it as their default route.
Exercise
Two LAN hosts each open a TCP connection from source port 40000 to the same server on port 443. The gateway has one public address. What does the gateway do so the server can tell the connections apart, and what would be different if the two hosts connected to different servers?
Show answer
The gateway rewrites the source address of both connections to its public address. It keeps port 40000 for the first connection, and for the second it picks a different source port, because the translated 5-tuple must be unique. If the hosts connected to different servers, the tuples would already differ in destination address, so both could keep source port 40000 on the public side.
Exercise
A gateway has a DHCP-assigned WAN address. Which rule type fits better, MASQUERADE or SNAT with a fixed --to-source, and why?
Show answer
MASQUERADE. It looks up the interface's current address and flushes stale entries when the address changes. A fixed SNAT address would become wrong after a new lease, and traffic would leave with a source address the gateway no longer owns.