Sub topic 6
Static one-to-one NAT
This chapter assumes you know IPv4 addressing and subnet masks, what a default gateway does, and how TCP and UDP ports work. It also assumes you have read how NAT rewrites packets and how the translation table works. Static NAT is the simplest NAT mode.
What static NAT does
Static NAT (one-to-one NAT) permanently maps one private address to one public address. Every packet to or from that private host uses the same public address. There is no sharing and no port rewriting.
Compare this with port address translation (PAT), where many hosts share one public address. PAT picks a source port per connection and removes the mapping when the connection ends. A static mapping is configured by an administrator and stays until it is removed.
| Property | PAT (masquerading) | Static one-to-one NAT |
|---|---|---|
| Public addresses used | One for many hosts | One per mapped host |
| Ports rewritten | Yes, source ports | No, ports stay the same |
| Mapping created by | Outbound traffic | Configuration |
| Inbound connections | Blocked unless forwarded | Allowed if the firewall permits |
| Mapping lifetime | Until the connection times out | Until removed |
Why use it
Use static NAT when a private host must be reachable on every port at a stable public address. Common cases are a mail server whose public address must match its DNS and reverse DNS records, or a legacy system that cannot work behind a port forward. It also helps with renumbering. You can move a server to a new private address and keep the public address clients already know.
The cost is address consumption. Each mapped host needs its own public address, so static NAT does nothing for IPv4 exhaustion.
Packet flow
Suppose the gateway maps private 192.168.1.10 to public 203.0.113.10. The gateway rewrites in both directions:
- Outbound: the source address
192.168.1.10becomes203.0.113.10. - Inbound: the destination address
203.0.113.10becomes192.168.1.10.
Configuring it on Linux
Linux has no single "static NAT" switch. You combine a destination rule (DNAT) for inbound traffic with a source rule (SNAT) for outbound traffic. The gateway must also forward packets, and the public address must be present on its WAN interface or routed to it by your provider.
# Enable forwarding
sudo sysctl -w net.ipv4.ip_forward=1
# Inbound: anything sent to 203.0.113.10 goes to the private host
sudo iptables -t nat -A PREROUTING -d 203.0.113.10 -j DNAT --to-destination 192.168.1.10
# Outbound: traffic from the private host leaves as 203.0.113.10
sudo iptables -t nat -A POSTROUTING -s 192.168.1.10 -o eth0 -j SNAT --to-source 203.0.113.10The SNAT rule matters. Without it, the host would leave through a general masquerade rule and appear as the gateway's main address. Remote systems would then see two different addresses for one server.
Add the SNAT rule before any broader MASQUERADE rule for the same LAN. The kernel applies the first matching NAT rule for a connection.
Common errors
- Missing SNAT rule. Inbound connections work, but the server's own outbound traffic shows the wrong address.
- Wrong rule order. A general masquerade rule placed first overrides the static source mapping.
- Address not on the gateway. If the public address is not configured on the WAN interface or routed to the gateway, packets never arrive to be translated.
- No
FORWARDpermission. The translation is correct, but the filter table drops the packets.
Exercise
A gateway has -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE followed by the static SNAT rule for 192.168.1.10. Outbound traffic from 192.168.1.10 shows the gateway's main address instead of 203.0.113.10. What is wrong, and how do you fix it?
Show answer
The masquerade rule comes first and matches the host's traffic, so the SNAT rule is never reached. Insert the SNAT rule before the masquerade rule (for example with iptables -t nat -I POSTROUTING 1 ...), or exclude 192.168.1.10 from the masquerade rule.