iptables MASQUERADE vs SNAT

Both rewrite the source address of outgoing packets so a whole network can share one address. They differ in where that address comes from. Pick one below and copy the commands for a NAT gateway. IPv4 only.

MASQUERADESNAT
New source addressThe current address of the outgoing interfaceA fixed address you name with --to-source
Address changes (DHCP, PPP)Follows the change automaticallyYou must update the rule
Address ranges and portsPort range onlyAddress range and port range
Interface goes downDrops the tracked connections of that interfaceKeeps them
Best forDynamic address, home or cloud private addressesStatic address you control

Both belong in the nat table's POSTROUTING chain, after the routing decision, see iptables tables and chains explained. The reverse translation of replies is automatic through connection tracking, so no rule is needed for it.

Generate a NAT gateway

Not sure of your subnet notation? Check it with CIDR to IP range.

1. Let the server forward packets

2. Add the rules

3. Check it

On a client in the translated network, whose default gateway is this server, curl -s https://ifconfig.me should print the gateway's public address. The packet counters in the listing rise when traffic matches.

Undo

Each -D line removes the first rule that matches exactly. To keep the rules after a reboot, see saving iptables rules.

Which one should I use?

With a static public address you control, SNAT is the precise choice. With a dynamic address, or a cloud instance that only knows its private address, use MASQUERADE: the public address is mapped by the provider and is not configured on the instance, so SNAT to it does not work. MASQUERADE is also what you add to a port forwarding setup when replies would not return through the server.

Nothing works? Check these

IP forwarding is off. cat /proc/sys/net/ipv4/ip_forward must print 1.
Wrong interface. The -o name must be the interface that faces the internet, see ip route show default.
Clients use another gateway. Their default route must point to this server.
Cloud providers usually drop forwarded traffic unless source and destination checking is disabled on the network interface, and private subnets need a route to this instance. Check your provider's documentation.
FORWARD blocks it. The ACCEPT rules must sit above any REJECT or DROP rule.

FAQ

What is the difference between MASQUERADE and SNAT in iptables?

Both rewrite the source address. SNAT uses a fixed address you specify, MASQUERADE uses whatever address the outgoing interface has at the moment.

When should I use MASQUERADE instead of SNAT?

When the outgoing address can change, for example with DHCP or PPP, or when the instance only has a private address, as on most cloud servers.

Do I need IP forwarding for NAT?

Yes, when the server translates traffic for other machines. Set net.ipv4.ip_forward to 1 and make it permanent in /etc/sysctl.d.

© PacketUtils. Tools run in your browser. Review every command before using it in production.HomeAboutContactPrivacy PolicyGitHub