Use it to share an internet connection, publish an internal service, or when NAT traffic does not flow.
NAT (Network Address Translation)
NAT on Linux rewrites addresses in netfilter: SNAT/masquerade in POSTROUTING lets internal hosts share the host's address; DNAT in PREROUTING forwards an incoming port to an internal host.
1. nft list table ip nat (or iptables -t nat -S) — rules present?
2. ip_forward = 1 and FORWARD allows it?
3. conntrack -L -p tcp --dport <port> — translation seen?
4. DNAT target's default route back through this host?
5. Test from outside / inside.
packet → PREROUTING (DNAT) → routing → FORWARD → POSTROUTING (SNAT/masquerade) → conntrack reverses replies
For Firewall → NAT, continue with the first unproven Topic check: Capture the exact original tuple and expected translated tuple, interfaces, namespace and whether traffic is local, forwarded or hairpin. If that boundary is healthy, advance to the adjacent check: Inspect NAT base chains/hooks/priorities and the filter rules that see pre/post-translation identities at their respective stages. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Operational evidence path Verify boundary
original tuple → NAT hook match on new flow → conntrack mapping → route/filter using stage-appropriate tuple → postrouting translation → remote endpoint → reverse conntrack translation → workload
Healthy evidence: fresh-flow conntrack and captures show the intended pre/post translation, filtering and routing succeed in both directions, and the application sees the expected peer/endpoint behavior.
Step 2 Verification and output interpretation Verify boundary
Follow routing/namespace placement into the expected Netfilter hook, watch the matching rule/chain counter, inspect conntrack/NAT state when applicable, and verify reply traffic.
Existing fast checks for this Topic. - Capture the exact original tuple and expected translated tuple, interfaces, namespace and whether traffic is local, forwarded or hairpin.
- Inspect NAT base chains/hooks/priorities and the filter rules that see pre/post-translation identities at their respective stages.
- Correlate conntrack entries and packet capture on both sides of translation while starting a fresh flow when rule changes must be tested.
- Verify bidirectional return translation and the original application transaction, not just a rewritten packet on one interface.
Verification chain. original tuple → NAT hook match on new flow → conntrack mapping → route/filter using stage-appropriate tuple → postrouting translation → remote endpoint → reverse conntrack translation → workload
Step 3 Commands Verify boundary
nft list table ip nat
iptables -t nat -L -n -v
Step 4 Failure modes and next checks Narrow the fault
Suspicious evidence: stale conntrack retains an old mapping, NAT is mistaken for permission, translation occurs on the wrong hook/interface, hairpin path differs, or return translation/routing fails.
It does not show behavior outside the captured namespace, family, five-tuple, interface/direction, manager/backend, ruleset generation, conntrack/NAT state, time window or application scope. NAT is confirmed only when a fresh original flow receives the intended mapping, filtering/routing for the translated tuple succeeds in both directions, and the application completes; a configured rewrite or one translated packet is insufficient.
Step 5 Common mistakes Narrow the fault
- Masquerading on the wrong interface.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
Masquerade needs ip_forward and a FORWARD rule allowing the traffic. Port forwarding (DNAT) also needs the FORWARD rule and a return route via this host. conntrack -L shows the translations.
nft list ruleset— all nftables rulesiptables -S; iptables -t nat -S— iptables rules (if used)firewall-cmd --list-all— firewalld zone settingsufw status verbose— UFW rules and defaultsss -tulpn— which services listen
ReferenceWhat it is1 section · depth on demand
Core model
Record the tuple at each boundary: original wire tuple, post-DNAT/routed tuple and post-SNAT egress tuple. Determine where the first packet creates conntrack/NAT state, then show routing and filtering for the translated identity plus return translation. Treat hairpin/loopback and dynamic-interface masquerade as separate cases.
ReferenceImportant terms & boundaries5 sections · depth on demand
What this shows and what it does not
NAT is confirmed only when a fresh original flow receives the intended mapping, filtering/routing for the translated tuple succeeds in both directions, and the application completes; a configured rewrite or one translated packet is insufficient.
In depth
NAT on Linux: masquerade, SNAT and DNAT
Network address translation rewrites addresses as packets pass through the host. The iptables-extensions(8) manual page describes the main targets:
- MASQUERADE is valid in the nat table's POSTROUTING chain. It uses the outgoing interface's current address, which suits dynamically assigned addresses. Its connections are forgotten when the interface goes down.
- SNAT sets a fixed source address.
- DNAT (in PREROUTING and OUTPUT) changes the destination: port forwarding to an internal server.
In nftables, the same functions are masquerade, snat to and dnat to, in chains of type nat. The nftables wiki gives their priorities as srcnat 100 on postrouting and dstnat −100 on prerouting.
Which NAT for which job
| Need | Type | nft statement |
|---|---|---|
| Internal clients reach the Internet; the uplink address changes | Masquerade | oifname "eth0" masquerade |
| The same, with a fixed public address | SNAT | oifname "eth0" snat ip to 203.0.113.10 |
| Publish an internal web server | DNAT | iifname "eth0" tcp dport 443 dnat ip to 10.30.0.20 |
| Redirect to a local port | Redirect | tcp dport 80 redirect to :8080 |
Requirements around NAT
| Requirement | Why |
|---|---|
ip_forward=1 | NAT on a router always involves forwarding |
| Forward rules that allow the translated traffic | NAT changes addresses; filtering still applies |
| Connection tracking | NAT works per flow; replies are translated back automatically |
| Hairpin NAT, if internal clients use the public address | Otherwise their replies bypass the router |
A small NAT gateway in nftables
host$ sudo tee /etc/nftables.d/nat.nft >/dev/null <<'EOF'
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
iifname "eth0" tcp dport 443 dnat to 10.30.0.20
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
oifname "eth0" ip saddr 10.30.0.0/24 masquerade
}
}
EOF
host$ sudo nft -f /etc/nftables.d/nat.nft
host$ sudo nft add rule inet filter forward iifname eth0 ip daddr 10.30.0.20 tcp dport 443 ct status dnat accept
host$ sudo conntrack -L -p tcp --dport 443 | head -2
tcp 6 431990 ESTABLISHED src=198.51.100.7 dst=203.0.113.10 sport=52011 dport=443 src=10.30.0.20 dst=198.51.100.7 sport=443 dport=52011 [ASSURED]
Reading the conntrack entry
The first tuple is the packet as it arrived: from the client to the public address. The second is the expected reply: from 10.30.0.20, the internal server. The difference between the two is the translation the kernel applies to the whole flow.
Documented example, not captured output.
How it works at runtime
NAT is part of Firewall → NAT. Linux firewall NAT rewrites packet addresses and/or ports using netfilter state, commonly DNAT for destination publication and SNAT/MASQUERADE for source translation, with rule placement tied to prerouting/output/postrouting and conntrack tracking of the resulting flow.
Connection tracking and NAT add per-flow state; routing decisions and namespaces determine which hooks a packet traverses.
Topic-specific state model. Linux firewall NAT rewrites packet addresses and/or ports using netfilter state, commonly DNAT for destination publication and SNAT/MASQUERADE for source translation, with rule placement tied to prerouting/output/postrouting and conntrack tracking of the resulting flow.
Technical metadata. Host/VM + network namespace, distribution/kernel, IPv4/IPv6 family, firewall manager/backend, table/family, base-chain hook/type/priority/policy, rule handle/order, sets/maps, interface, five-tuple, conntrack state/zone, NAT pre/post tuple, packet/rule counters, firewall logs/trace, route, socket/listener, ingress/egress capture and original application result.
Interpretation rule. - NAT rule exists ≠ packet matches it or conntrack selected a new mapping for the observed flow.
- DNAT success ≠ FORWARD/INPUT policy permits the translated destination.
- SNAT/MASQUERADE success ≠ return routing or remote application is healthy.
- Changing a NAT rule ≠ existing conntrack entries immediately use the new mapping; tracked flows can retain prior translation state.
Quick reference
NAT on Linux with nftables:
table ip nat {
chain prerouting { type nat hook prerouting priority dstnat;
iifname "eth0" tcp dport 8443 dnat to 10.0.20.15:443 }
chain postrouting { type nat hook postrouting priority srcnat;
oifname "eth0" ip saddr 10.0.20.0/24 masquerade }
}
# firewalld
firewall-cmd --permanent --zone=external --add-masquerade
firewall-cmd --permanent --zone=external --add-forward-port=port=8443:proto=tcp:toport=443:toaddr=10.0.20.15
DNAT also needs a FORWARD accept rule and IP forwarding. Use masquerade for dynamic outside addresses, snat to <ip> for static ones.
Limits and how it fails
Primary failure point. NAT is confirmed only when a fresh original flow receives the intended mapping, filtering/routing for the translated tuple succeeds in both directions, and the application completes; a configured rewrite or one translated packet is insufficient.
NAT changes addresses/ports but does not create application reachability by itself.
ReferenceConfiguration & state1 section · depth on demand
Configuration, persistence and lifecycle
Keep default policy, zone/interface/source assignment, forwarding and NAT ownership explicit.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Firewall → NAT. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://wiki.nftables.org/wiki-nftables/index.php/Main_Page · confidence 94%What changed and what may be affected
Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.
Quick lookup tables
NAT operational reference
Topic-specific lookup boundaries, proof limits and next-step Evidence for NAT.
| Lookup boundary | Topic-specific reference | Operational use |
|---|---|---|
| Scope / effective state | Linux firewall NAT rewrites packet addresses and/or ports using netfilter state, commonly DNAT for destination publication and SNAT/MASQUERADE for source translation, with rule placement tied to prerouting/output/postrouting and conntrack tracking of the resulting flow. | Establish exactly what this Topic owns before crossing into another subsystem. |
| Fast verification | Pin exact namespace + five-tuple + interfaces/direction → prove route/listener prerequisites → identify manager/backend → snapshot full ruleset + hook priorities/default policies → reproduce with counters/trace + packet capture → correlate conntrack/NAT → change one demonstrated cause → read back effective state → repeat original workload + negative regression flow. | Use the shortest evidence path first; a fast check is a boundary test, not blanket proof. |
| Verification chain | original tuple → NAT hook match on new flow → conntrack mapping → route/filter using stage-appropriate tuple → postrouting translation → remote endpoint → reverse conntrack translation → workload | Trace requested/configured state through effective state to the observed workload result. |
| Failure boundary | NAT is proven only when a fresh original flow receives the intended mapping, filtering/routing for the translated tuple succeeds in both directions, and the application completes; a configured rewrite or one translated packet is insufficient. | Stop broad troubleshooting at the first boundary that is not supported by evidence. |
| Positive evidence | Correlated Linux firewall evidence can establish this proof path: original tuple → NAT hook match on new flow → conntrack mapping → route/filter using stage-appropriate tuple → postrouting translation → remote endpoint → reverse conntrack translation → workload | Use this observation only for the specific claim it actually supports. |
| Evidence limit | It does not independently prove behavior outside the captured namespace, family, five-tuple, interface/direction, manager/backend, ruleset generation, conntrack/NAT state, time window or application scope. NAT is proven only when a fresh original flow receives the intended mapping, filtering/routing for the translated tuple succeeds in both directions, and the application completes; a configured rewrite or one translated packet is insufficient. | Do not promote a local success signal into end-to-end or adjacent-dependency proof. |
| Next troubleshooting check | Continue from the first unproven firewall boundary: preserve the current ruleset/state, capture the exact packet path and rule verdict, change only the demonstrated cause through the authoritative manager, read back effective policy, then repeat the original workload and regression flows. | Continue to the next unresolved boundary while preserving the original test condition. |
NAT · operational reference
Compact Linux state, persistence, verification and boundary reference for fast lookup.
| Reference boundary | NAT |
|---|---|
| Topic path | Firewall → NAT |
| Runtime state | Linux firewall NAT rewrites packet addresses and/or ports using netfilter state, commonly DNAT for destination publication and SNAT/MASQUERADE for source translation, with rule placement tied to prerouting/output/postrouting and conntrack tracking of the resulting flow. |
| Fast checks | - Capture the exact original tuple and expected translated tuple, interfaces, namespace and whether traffic is local, forwarded or hairpin. - Inspect NAT base chains/hooks/priorities and the filter rules that see pre/post-translation identities at their respective stages. - Correlate conntrack entries and packet capture on both sides of translation while starting a fresh flow when rule changes must be tested. - Verify bidirectional return translation and the original application transaction, not just a rewritten packet on one interface. |
| Verification chain | original tuple → NAT hook match on new flow → conntrack mapping → route/filter using stage-appropriate tuple → postrouting translation → remote endpoint → reverse conntrack translation → workload Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return path or regression → explicit Evidence boundary. |
| Failure boundary | NAT is proven only when a fresh original flow receives the intended mapping, filtering/routing for the translated tuple succeeds in both directions, and the application completes; a configured rewrite or one translated packet is insufficient. Boundary chain: process/context → userspace configuration → kernel subsystem/resource → device/network/filesystem dependency → effective state → original workload. |
| Persistence rule | Prove which configuration source owns the state, then prove that the running consumer loaded or generated the intended state. |