Sub topic 8
Hands-on: masquerading and port forwarding on Linux
This chapter assumes you know IPv4 subnets, what a default gateway does, how TCP and UDP ports work, and how to run commands in a Linux shell. It also assumes you have read the earlier chapters on source NAT, PAT, and port forwarding. Here you configure them on a Linux gateway.
The examples use this layout:
eth0is the public (WAN) interface. Its address is203.0.113.5.- The private LAN is
192.168.1.0/24. - A web server runs on
192.168.1.10, port 80.
Step 1: Enable forwarding
A Linux host drops packets that arrive on one interface and are addressed to another unless forwarding is on. Enable it first.
# Allow the Linux host to forward packets between interfaces
sudo sysctl -w net.ipv4.ip_forward=1This setting is lost at reboot. To keep it, add net.ipv4.ip_forward=1 to a file in /etc/sysctl.d/.
Step 2: Masquerade outbound traffic
Masquerading is source NAT that uses the outgoing interface's current address. It suits links whose address can change, such as DHCP.
# eth0 = public/WAN interface, 192.168.1.0/24 = private LAN
sudo iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADEThe rule sits in the POSTROUTING chain of the nat table. It runs after the routing decision, so the kernel already knows the packet leaves through eth0. If two LAN hosts use the same source port, the kernel picks a different source port for one of them. The reply then maps back to the right host.
Step 3: Forward a port
Destination NAT must happen before routing, so the rule goes in PREROUTING. The kernel then routes the packet using the new destination.
# Forward TCP port 8080 on the gateway's WAN interface to 192.168.1.10:80
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.10:80
# Allow the forwarded traffic through the filter table
sudo iptables -A FORWARD -i eth0 -p tcp -d 192.168.1.10 --dport 80 -j ACCEPTThe FORWARD rule matches the packet after DNAT, so it uses the translated address and port (192.168.1.10:80), not 8080. You need it only when the FORWARD policy would otherwise drop the traffic. Reply packets pass without a rule of their own if your firewall already accepts established connections.
The same setup with nftables
nftables uses the same hooks. You create a nat table and two chains with the right hook and priority.
# Same setup with nftables
sudo nft add table ip nat
sudo nft 'add chain ip nat prerouting { type nat hook prerouting priority dstnat; policy accept; }'
sudo nft 'add chain ip nat postrouting { type nat hook postrouting priority srcnat; policy accept; }'
sudo nft add rule ip nat prerouting iifname "eth0" tcp dport 8080 dnat to 192.168.1.10:80
sudo nft add rule ip nat postrouting oifname "eth0" masqueradeStep 4: Inspect the translations
The conntrack tool lists the kernel's tracked connections.
# Inspect the kernel's connection tracking table (NAT mappings)
sudo conntrack -L -p tcpEach entry shows an original tuple and a reply tuple. When the two differ in addresses or ports, NAT rewrote the packet. For a port forward, the reply tuple's source is 192.168.1.10:80, while the original destination is the gateway's 203.0.113.5:8080.
Common mistakes
- Forgetting
ip_forward. Packets arrive but never leave. - Using
-owith the wrong interface in the masquerade rule, so nothing matches. - Putting DNAT in
POSTROUTING. It must be inPREROUTING. - Writing the
FORWARDrule with the public port (8080) instead of the translated port (80). - Testing from inside the LAN. That needs hairpin NAT, which a later chapter covers.
Exercise
A port forward rule maps gateway port 8080 to 192.168.1.10:80, and IP forwarding is on. Why must the FORWARD rule match --dport 80 and not --dport 8080?
Show answer
DNAT in PREROUTING runs before the FORWARD chain, so by the time the packet reaches FORWARD its destination has already been rewritten to 192.168.1.10:80. A rule matching port 8080 would never match.
Exercise
Which conntrack information tells you that a connection to port 8080 was translated by DNAT?
Show answer
The original tuple shows the destination 203.0.113.5:8080, while the reply tuple shows the source 192.168.1.10:80. The differing addresses and ports show the rewrite.