What is the difference between SNAT and MASQUERADE in IPtables?

Source: Internet
Author: User
Differences between SNAT and MASQUERADE in IPtables 1. SNAT and DNAT can be flexibly implemented in IPtables. There are two main types of Network Address Translation: SNAT and DNAT. SNAT is the abbreviation of sourcenetworkaddresstranslation, that is, source address target conversion. For example, multiple PCs share the Internet with the ADSL Router

Differences between SNAT and MASQUERADE in IPtables

I. Concepts of SNAT and DNAT

In IPtables, You can flexibly perform various network address translation (NAT). There are two main types of Network Address Translation: SNAT and DNAT.

SNAT is source networkDdRessTrAnslation refers to source address destination conversion. For example, multiple PCs share the Internet with the ADSL Router. Each pc is configured with an intranet IP address. When the PC accesses an external network, the router replaces the source ip address in the packet header with the IP address of the router, when an external network server, such as a website web server, receives an access request, its log records the ip address of the router rather than the Intranet ip address of the PC. This is because, the "Source Address" in the header of the packet received by this server has been replaced, so it is called SNAT. It is based on Source Address conversion.

DNAT is short for destination network address translation, that is, destination network address translation. A typical application is to have a web server configured with an intranet ip address on the Intranet, and a firewall configured with a public ip address on the frontend, A visitor on the Internet uses a public ip address to access the website. When the visitor accesses the website, the client sends a packet. The destination address in the packet header is the public ip address of the firewall, the firewall will rewrite the packet header once, rewrite the target address to the Intranet ip address of the web server, and then send the packet to the Intranet web server, data Packets penetrate the firewall and change from a public ip address to an intranet address, that is, DNAT.

Ii. MASQUERADE Concept

MASQUERADE, address disguise, has a similar effect with SNAT in iptables, but there are also some differences, but when using SNAT, the ip address range of the egress ip address can be one, it can also be multiple, for example:

As follows:CommandAll the packets in the 10.8.0.0 CIDR block are SNAT into an ip address of 192.168.5.3 and then sent out,

Iptables-t nat-a postrouting-s 10.8.0.0/255.255.255.0-o eth0-j SNAT -- to-source 192.168.5.3

Run the following command to SNAT all 10.8.0.0 packets into several ip addresses, such as 192.168.5.3/192.168.5.4/192.168.5.5, and then send

Iptables-t nat-a postrouting-s 10.8.0.0/255.255.255.0-o eth0-j SNAT -- to-source 192.168.5.3-192.168.5.5

This is how to use SNAT, that is, you can use NAT as an address or multiple addresses. However, for SNAT, whether it is a few addresses, you must specify the ip address to be SNAT, if the current system uses the dynamic dialing method of ADSL, the outbound IP address 192.168.5.3 will be changed each time and the change range is large, not necessarily the IP address from 192.168.5.3 to 192.168.5.5, at this time, if you configure iptables in the current way, the problem will occur, because the server address will change after each dial, and the ip address in the iptables rule will not change automatically, after each address change, you must manually modify iptables and change the fixed ip address in the rule to a new ip address. This is very difficult to use.

MASQUERADE is designed for this scenario. Its role is to automatically obtain the current IP address from the server's Nic for NAT.

For example, the following command:

Iptables-t nat-a postrouting-s 10.8.0.0/255.255.255.0-o eth0-j MASQUERADE

In this configuration, you do not need to specify the target SNAT ip. No matter what kind of dynamic ip address is obtained at the exit of eth0, MASQUERADE automatically reads the current ip address of eth0 and then performs SNAT, in this way, the dynamic SNAT address conversion is achieved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.