Sub topic 2
Many Hosts, One Public Address: Source NAT and PAT
Assume you know IPv4 subnets, default gateways, TCP and UDP ports, and basic Linux shell commands. This chapter focuses on how a gateway lets private hosts share one public IPv4 address.
Source NAT and PAT
Source Network Address Translation (SNAT) changes a packet's source address as it leaves a network. Private IPv4 addresses, such as 192.168.1.0/24, are not normally routable on the public Internet.
Port Address Translation (PAT) extends SNAT by also changing the source port. The gateway records each flow in a translation table, so replies return to the correct internal host.
| Internal flow | Public flow |
|---|---|
| `192.168.1.20:51500 → 198.51.100.8:443` | `203.0.113.5:40001 → 198.51.100.8:443` |
| `192.168.1.21:51500 → 198.51.100.8:443` | `203.0.113.5:40002 → 198.51.100.8:443` |
The two clients can use the same internal port because PAT assigns different public ports.
Static One-to-One NAT
Static NAT permanently maps one private address to one public address. It does not depend on a changing port mapping. For example, 192.168.1.10 might always translate to 203.0.113.10. This uses one public address per host, so it is predictable but consumes scarce IPv4 addresses.
Port Forwarding
Port forwarding uses Destination NAT (DNAT). The gateway changes an incoming packet's destination from its public address and port to a private server:
203.0.113.5:8080 → 192.168.1.10:80
The gateway also tracks the connection and reverses the translation in the reply. A firewall must permit the forwarded traffic.
> callout note > Port forwarding exposes a service to the outside network. Restrict source addresses and secure the service whenever possible.
Hands-On: Linux NAT
On a Linux gateway, enable IPv4 forwarding and add masquerading for outbound LAN traffic:
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADEForward WAN port 8080 to an internal web server:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 \
-j DNAT --to-destination 192.168.1.10:80
sudo iptables -A FORWARD -i eth0 -p tcp -d 192.168.1.10 --dport 80 -j ACCEPTTest from outside the gateway:
curl -v http://203.0.113.5:8080/Exercise
Why can two private clients using source port 51500 share one public address?
Show answer
PAT assigns each flow a distinct public source port and stores both mappings in the translation table.