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?
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 do the same source NAT thing in the nat table within the POSTROUTING chain.
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
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.