Sub topic 6
Static one-to-one NAT
Static one-to-one NAT permanently maps one private address to one public address. This chapter assumes you know the NAT basics from the earlier chapters: private ranges, what a translation table is, and how source NAT and destination NAT differ. It also assumes you can run iptables or nft commands as root.
What static NAT does
Port address translation (PAT) shares one public address among many hosts and tells their connections apart by port. Static NAT does not share anything. It binds exactly one inside address to exactly one outside address, and the binding stays in place whether or not any traffic is flowing.
For a server at 192.168.1.10 mapped to 203.0.113.10, the rules are:
- A packet arriving for
203.0.113.10has its destination rewritten to192.168.1.10. This is destination NAT. - A packet leaving from
192.168.1.10has its source rewritten to203.0.113.10. This is source NAT. - Ports are not touched. TCP port 443 stays 443 in both directions.
Because the mapping is permanent, the translation does not depend on a connection that the inside host started. An outside client can open a connection to the public address at any time, and the NAT device will translate it. This is the main difference from PAT, where an inbound packet with no matching table entry is dropped.
Following a packet through the device
The diagram shows an outside client connecting to the mapped server and the server's reply coming back.
The client sees only 203.0.113.10. The server sees the client's real address 198.51.100.7, which matters for logging and access control on the server. The device still creates a connection-tracking entry for each flow, as it does for any NAT. The entry only records the state of the connection. It does not decide whether translation is allowed.
How it compares with PAT and port forwarding
| Property | Static one-to-one NAT | PAT (masquerading) | Port forwarding |
|---|---|---|---|
| Public addresses used | One per inside host | One for many hosts | One for many hosts |
| Ports rewritten | No | Yes, when needed to keep flows distinct | Only the forwarded port, if it differs |
| Inbound connections | Allowed to every port | Dropped without a table entry | Allowed to the forwarded port only |
| Mapping created by | Configuration | Outbound traffic | Configuration |
| Address cost | High | Very low | Very low |
Static NAT uses the most public addresses, so it fits poorly in a world short of IPv4 addresses. You choose it when the application needs a stable, dedicated public identity:
- A mail server whose reverse DNS and reputation are tied to one IP.
- A protocol that embeds addresses or cannot tolerate port rewriting.
- A service that needs many ports or an unknown range of ports, where forwarding each one is impractical.
- A migration, where a legacy host keeps its old address while its internal addressing changes.
- Overlapping address spaces, for example when two companies merge and both use
10.0.0.0/8.
Cloud platforms use the same idea. On AWS, an Elastic IP is associated with an instance, and the internet gateway performs a one-to-one translation between the public address and the instance's private address. The operating system inside the instance only ever sees the private address.
Configuring it on Linux
Assume eth0 is the public interface, 203.0.113.10 is the public address you want to map, and 192.168.1.10 is the inside server. The gateway needs forwarding enabled and must answer for the public address.
# Enable routing between interfaces
sudo sysctl -w net.ipv4.ip_forward=1
# Make the gateway answer ARP for the mapped address on the WAN side.
# Skip this if your ISP routes the address to you instead of ARPing for it.
sudo ip addr add 203.0.113.10/32 dev eth0Now the two translation rules, using iptables:
# Inbound: rewrite the destination of anything addressed to the public IP
sudo iptables -t nat -A PREROUTING -d 203.0.113.10 -j DNAT --to-destination 192.168.1.10
# Outbound: rewrite the source of traffic from the server, inserted at the top
sudo iptables -t nat -I POSTROUTING 1 -s 192.168.1.10 -o eth0 -j SNAT --to-source 203.0.113.10
# Allow forwarded traffic to and from the server
sudo iptables -A FORWARD -d 192.168.1.10 -j ACCEPT
sudo iptables -A FORWARD -s 192.168.1.10 -j ACCEPTThe SNAT rule is inserted at position 1 on purpose. If a general MASQUERADE rule for 192.168.1.0/24 is already in POSTROUTING, it would match the server's traffic first. The server would then leave with the gateway's main address instead of 203.0.113.10. Put the specific rule before the general one.
The same mapping in nftables, in a single ruleset file:
sudo nft -f - <<'EOF'
table ip static_nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
ip daddr 203.0.113.10 dnat to 192.168.1.10
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
ip saddr 192.168.1.10 oifname "eth0" snat to 203.0.113.10
}
}
EOFIf you also have a masquerade rule, add it to the end of this same postrouting chain so the specific SNAT rule is evaluated first.
The netmap alternative
To map a whole block, such as 254 inside hosts to 254 public hosts, write one rule per host or use NETMAP. It translates the network part and keeps the host part:
# 192.168.1.x <-> 203.0.113.x, host part preserved
sudo iptables -t nat -A PREROUTING -d 203.0.113.0/24 -j NETMAP --to 192.168.1.0/24
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j NETMAP --to 203.0.113.0/24Here 203.0.113.37 maps to 192.168.1.37. This is also the usual tool for the overlapping-address case, where you translate one side's 10.0.0.0/8 into an unused range.
Verifying the mapping
Start with the connection-tracking table, which shows both directions of each translated flow:
sudo conntrack -L -p tcpAn entry for a client at 198.51.100.7 connecting to the public address should show the original tuple with destination 203.0.113.10. The reply tuple should show the source 192.168.1.10. If you see the reply tuple with the gateway's other address as the source, the SNAT rule is being shadowed by a masquerade rule.
Then test from a host outside your network. A test from inside takes the hairpin path and tells you nothing about the external mapping.
# From an outside host
ping -c 3 203.0.113.10
curl -v http://203.0.113.10/Edge cases and gotchas
- Every port is exposed. Static NAT translates all protocols and ports. Any service listening on the inside host, including SSH, databases, and admin panels, becomes reachable from outside unless the
FORWARDchain restricts it. - Outbound traffic uses the mapped address. The server's connections to the internet are sourced from
203.0.113.10, not from the gateway's shared address. Its reputation and any allowlists at remote sites apply to that address. - Address ownership on the WAN link. If the public address sits in the same subnet as the ISP's router, the router sends an ARP request for it. The gateway must answer, which adding the address to
eth0does. Without it, packets never arrive. As an alternative to assigning the address, you can useip neigh add proxy 203.0.113.10 dev eth0. - Inside clients using the public address. A LAN host that connects to
203.0.113.10hits the hairpin problem from the earlier chapter. Either use split DNS to give inside clients the private address, or add a hairpin masquerade rule. - Embedded addresses. Protocols that carry IP addresses inside the payload still need a helper or an application-level fix. Static NAT removes port rewriting but not payload problems.
- Fragmentation and ICMP. The kernel translates ICMP errors that refer to a tracked flow, so path MTU discovery works through the mapping. Check that your firewall does not drop
fragmentation-neededmessages.
Choosing static NAT
Use static NAT when the host needs its own public identity, needs many or unpredictable ports, or must keep a fixed address across an internal renumbering. Use port forwarding when one or two services are enough and public addresses are scarce. Use PAT for ordinary client traffic. The three often coexist on one gateway, with the specific one-to-one rules placed ahead of the general masquerade rule.
Exercise
A gateway has iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE and you later append iptables -t nat -A POSTROUTING -s 192.168.1.10 -o eth0 -j SNAT --to-source 203.0.113.10. The server at 192.168.1.10 still leaves the network with the gateway's normal address. Why, and how do you fix it?
Show answer
Rules in a chain are evaluated in order, and the first NAT rule that matches a new connection decides its translation. The broad MASQUERADE rule matches the server's traffic before the appended SNAT rule is reached. Fix it by inserting the specific rule ahead of the general one, for example with iptables -t nat -I POSTROUTING 1 -s 192.168.1.10 -o eth0 -j SNAT --to-source 203.0.113.10, or by deleting and re-adding the rules in the right order.
Exercise
You configure a static mapping and test it by running curl http://203.0.113.10/ from another laptop on the same LAN as the server. It fails, but conntrack -L on the gateway shows nothing. Name two likely reasons and a better test.
Show answer
First, the LAN client's packet may never reach the gateway's NAT rules in the way you expect, or the server's reply may bypass the gateway and go straight to the client, which rejects it because it comes from an unexpected address. This is the hairpin problem. Second, the test does not exercise the external path at all, so it cannot confirm the mapping. A better test is to run curl or ping from a host outside the network, such as a cloud VM or a phone on mobile data, and then check conntrack -L on the gateway.