Pcornel
Browse topics
Options
Recent
Favorites
Linux · Technical reference

NAT (Network Address Translation)

Direct answer

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.

When to use this

Use it to share an internet connection, publish an internal service, or when NAT traffic does not flow.

First 30 seconds

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.

Operational verification

packet → PREROUTING (DNAT) → routing → FORWARD → POSTROUTING (SNAT/masquerade) → conntrack reverses replies

Evidence · next step

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
Platform / version applicability
linux
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformlinux
Scope / prerequisiteUse it to share an internet connection, publish an internal service, or when NAT traffic does not flow.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

5 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points

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.

Key checks
  • nft list ruleset — all nftables rules
  • iptables -S; iptables -t nat -S — iptables rules (if used)
  • firewall-cmd --list-all — firewalld zone settings
  • ufw status verbose — UFW rules and defaults
  • ss -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

NeedTypenft statement
Internal clients reach the Internet; the uplink address changesMasqueradeoifname "eth0" masquerade
The same, with a fixed public addressSNAToifname "eth0" snat ip to 203.0.113.10
Publish an internal web serverDNATiifname "eth0" tcp dport 443 dnat ip to 10.30.0.20
Redirect to a local portRedirecttcp dport 80 redirect to :8080

Requirements around NAT

RequirementWhy
ip_forward=1NAT on a router always involves forwarding
Forward rules that allow the translated trafficNAT changes addresses; filtering still applies
Connection trackingNAT works per flow; replies are translated back automatically
Hairpin NAT, if internal clients use the public addressOtherwise 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor doclinux current

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%
Technical changelog & impact

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.

Related reference impactReview these references after material technical changes when their shared boundary is affected.
Terminology layer

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.

Aliases that resolve here
linux natnft natsnat dnat masqueradenetfilter nat
Embedded reference data

Quick lookup tables

NAT operational reference

Topic-specific lookup boundaries, proof limits and next-step Evidence for NAT.

Lookup boundaryTopic-specific referenceOperational use
Scope / effective stateLinux 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 verificationPin 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 chainoriginal tuple → NAT hook match on new flow → conntrack mapping → route/filter using stage-appropriate tuple → postrouting translation → remote endpoint → reverse conntrack translation → workloadTrace requested/configured state through effective state to the observed workload result.
Failure boundaryNAT 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 evidenceCorrelated 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 → workloadUse this observation only for the specific claim it actually supports.
Evidence limitIt 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 checkContinue 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 boundaryNAT
Topic pathFirewall → NAT
Runtime stateLinux 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 chainoriginal 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 boundaryNAT 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 ruleProve which configuration source owns the state, then prove that the running consumer loaded or generated the intended state.
Confirmation

Confirm action

Continue?