Sub topic 5
Many hosts, one public address: source NAT and PAT
This chapter assumes you know IPv4 subnets, what a default gateway does, and how TCP and UDP use port numbers. It also assumes you can run commands in a Linux shell. Earlier chapters covered the 5-tuple and the translation table. This chapter shows how they combine to let many private hosts share one public address.
Source NAT in one sentence
Source NAT (SNAT) rewrites the source address of packets that leave a private network. The reply comes back to the NAT device, which reverses the rewrite and delivers the packet to the original host.
A plain one-to-one SNAT would need one public address per private host. That does not fix address exhaustion. Port address translation (PAT) fixes it by rewriting the source port as well. PAT is also called NAT overload, and on Linux it is called masquerading.
How PAT tells connections apart
Replies arrive addressed to one public IP. The only thing that tells them apart is the destination port. So the NAT device picks a public source port for each outgoing connection and records the pairing in its translation table.
Suppose two LAN hosts both use local port 50000 to reach the same web server:
| Private source | Public source after PAT | Destination |
|---|---|---|
| 192.168.1.20:50000 | 203.0.113.5:50000 | 198.51.100.7:443 |
| 192.168.1.21:50000 | 203.0.113.5:50001 | 198.51.100.7:443 |
Both connections now have different 5-tuples, so the gateway can map each reply back to the right host. The kernel keeps the original port when it is free and chooses another only on a collision.
Configuring masquerading on Linux
First enable forwarding, or the host will not route between its interfaces.
# Allow the Linux host to forward packets between interfaces
sudo sysctl -w net.ipv4.ip_forward=1Then add the SNAT rule in the POSTROUTING chain of the nat table. Rewriting must happen after the routing decision, so the kernel already knows the outgoing interface.
# 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 MASQUERADEMASQUERADE uses eth0's current address. This suits a WAN address that changes, such as one from DHCP. If the address is fixed, -j SNAT --to-source 203.0.113.5 is slightly cheaper because the kernel does not look up the interface address for every new connection.
The sysctl -w setting is lost on reboot. To keep it, put net.ipv4.ip_forward=1 in a file under /etc/sysctl.d/.
Seeing the mappings
Use conntrack to see the translation table.
# Inspect the kernel's connection tracking table (NAT mappings)
sudo conntrack -L -p tcpEach entry shows an original tuple and a reply tuple. For a masqueraded connection, the reply tuple's destination is the public address and the translated port.
Common errors
- Forwarding is off. Hosts reach the gateway but nothing leaves it. Check
sysctl net.ipv4.ip_forward. - Wrong interface in
-o. The rule never matches, so private addresses leak onto the WAN and are dropped upstream. - Port exhaustion. Each public IP has about 64,000 usable ports per destination address and protocol. A busy gateway talking to one server can run out, and new connections fail.
- Timeouts. Entries expire after idle time. Long-lived idle connections can lose their mapping unless keepalives are sent.
- Inbound traffic fails. PAT creates a mapping only when an inside host sends first. Unsolicited connections from outside have no entry and are dropped. Reaching an inside service needs destination NAT, covered in a later chapter.
Exercise
Two LAN hosts both open connections from source port 50000 to the same server through one masquerading gateway. How does the gateway keep the replies apart?
Show answer
It rewrites the source port of at least one connection to a different free port, for example 50001, and records each mapping in the translation table. Replies addressed to each public port are matched to their entry and rewritten back to the correct private host and port.
Exercise
Why is the masquerade rule placed in POSTROUTING rather than PREROUTING?
Show answer
Source NAT must run after the routing decision, once the kernel knows which interface the packet leaves through and therefore which address to use. PREROUTING runs before routing and is used for destination NAT.