37

I am confused what's the actual difference between SNAT and Masquerade?

If I want to share my internet connection on local network then whether should I select SNAT or Masquerade?

39

The SNAT target requires you to give it an IP address to apply to all the outgoing packets. The MASQUERADE target lets you give it an interface, and whatever address is on that interface is the address that is applied to all the outgoing packets. In addition, with SNAT, the kernel's connection tracking keeps track of all the connections when the interface is taken down and brought back up; the same is not true for the MASQUERADE target.

Good documents include the HOWTOs on the Netfilter site and the iptables man page.

  • 2
    I'm having trouble understanding the benefit of SNAT. Why does it matter if the kernel tracks connections or not when the interface goes down? Regarding MASQUERADE, the netfilter docs say "But more importantly, if the link goes down, the connections (which are now lost anyway) are forgotten, meaning fewer glitches when connection comes back up with a new IP address." Sounds reasonable (although what are the glitches?) Now looking at SNAT, what is the benefit of tracking lost connections? Why not use MASQUERADE every time? – Carl G Nov 9 '16 at 12:29
  • 1
    @CarlG, I guess the glitches would occur with the permanently tracked -j SNAT (as opposed to the recycling tracking with -j MASQUERADE) when a new outgoing connection from a LAN node uses the same source port number as the severed outgoing connection from the same LAN node. In that case, I can imagine incoming packets from the old outgoing connection getting sent to the node, confusing its TCP stack. As for the benefit of -j SNAT, what if the NAT box is configured with the same external IP address and the kernel keeps forwarding packets from old connections instead of replying with RST? – eel ghEEz Mar 18 '18 at 14:50
  • SNAT is useful, if for example you have multiple IP addresses assigned to the outgoing interface and want the NAT source to be a particular one of these. – pgoetz Feb 9 at 2:09
17

Basically SNAT and MASQUERADE do the same source NAT thing in the nat table within the POSTROUTING chain.

Differences

  • MASQUERADE does not require --to-source as it was made to work with dynamically assigned IPs

  • SNAT works only with static IPs, that's why it has --to-source

  • MASQUERADE has extra overhead and is slower than SNAT because each time MASQUERADE target gets hit by a packet, it has to check for the IP address to use.

NOTE: A typical use case for MASQUERADE: AWS EC2 instance in a VPC, it has a private IP within the VPC CIDR (e.g. 10.10.1.0/24) - 10.10.1.100 for example, it also has a public IP so as to communicate with the Internet (assume it is in a public subnet) thru which the private IP 1:1 NAT. The public IP may change after instance reboot (if it is NOT an EIP), MASQUERADE is a better option in this use case.

Important: It is still possible to use MASQUERADE target with static IP, just be aware of the extra overhead.

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.