Source NAT
Hide private addresses behind one public address
Lesson 12 of 20 · 12 min · Module 4: Address translation
The office uses addresses that do not exist on the internet. Something has to rewrite them on the way out, or no reply would ever find its way back.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Explain why private addresses need translation
- Write a source NAT line for outgoing traffic
- Keep NAT and permission apart
The idea
Private ranges such as 10.0.0.0/8 are not routed on the internet. Source NAT replaces the private source address of an outgoing packet with a public one the firewall owns, and remembers the mapping so the reply can be translated back. NAT is not a permission: the policy still decides whether the packet may leave.
allow tcp from guest to untrust port 80,443
snat from guest to untrust use 198.51.100.20
01Follow the address on the way out
A PC sends from 10.0.1.25 to a website. After source NAT the packet leaves as 203.0.113.10, the firewall's public address. The website answers to 203.0.113.10, and the firewall uses its session table to hand the reply to 10.0.1.25. Real firewalls usually change the source port as well, so many PCs can share one address.
02Translate only what leaves for the internet
"snat from trust to untrust" translates internet-bound traffic. Traffic from the office to the DMZ stays inside your own network and should keep its real source address, so server logs show which PC connected.
03Source NAT on real devices
The policy decides first, using the original private source; translation happens as the packet leaves.
# nftables
nft add rule ip nat postrouting oifname "wan0" ip saddr 10.0.1.0/24 masquerade
# FortiGate: switch NAT on in the outgoing policy
config firewall policy
edit 1
set nat enable
next
end
# Huawei USG
nat-policy
rule name office_out
source-zone trust
destination-zone untrust
source-address 10.0.1.0 mask 255.255.255.0
action source-nat easy-ip
Your turn
trust browse the internet, but packets leave with a private source address. Add a source NAT line so traffic from trust to untrust uses the address public. Do not translate traffic to the DMZ, and do not add any allow rule.What the checks look for
- Internet traffic leaves with the public source address
- The reply is translated back to the PC
- Traffic to the DMZ keeps its real source
- NAT allowed nothing by itself
Take this with you
Source NAT gives outgoing traffic a routable source address and translates replies back. It changes addresses, never permissions.
Go a little further
Change the line to "snat from trust to any use public" and run the lab. Read what the web server would now see as the source of the staff request.