The principle of iptablesnat is the same as that of the filter table. The nat table also has three default chains (chains): PREROUTING: the destination DNAT rule redirects external access to other hosts, such as internal SERVER, or DMZ. Because only the destination IP address of the data packet is checked during routing, the destination PREROUTINGDNAT must be performed before routing. IptablesNat principles
Like filter tables, nat tables also have three default "chains" (chains): PREROUTING: Destination DNAT rules
Redirects external access to other hosts, such as internal servers or DMZ.
Because only the destination IP address of the data packet is checked during routing, the destination prerouting dnat must be performed before routing;
Prerouting dnat first Filter again (FORWARD)?> Last route.
The target address of the match in routing and filtering (FORWARD) is after the prerouting dnat.
POSTROUTING: source SNAT rule
The routing rules in the chain will be executed later.
System router?> Filter again (FORWARD)?> Finally, you must translate the postrouting snat address.
The match source address is before translation.
OUTPUT: defines the destination NAT rule for locally generated packets.
===================================== Intranet access to the Internet-j snat ============== ============================
-J SNAT
-J SNAT: source network address translation. SNAT is the source IP address of the rewrite package.
SNAT can only be used in the POSTROUTING chain of the nat table
Only valid in the nat table, in the POSTROUTING chain.
-J SNAT -- to-source ipaddr [-ipaddr] [: port-port]
Ipaddr:
A single new source IP address
Range of IP addresses
Or you can add several -- to-source options. a simple round-Your takes place between these adresses.
Port range (only valid if the rule also specifies-p tcp or-pudp)
If no port range is specified, then source ports below 512 willbe.
Mapped to other ports below 512.
-J MASQUERADE
The public address of the Internet port is dynamically obtained by DHCP (such as ADSL)
Iptables-t nat-a postrouting Co eth1 Cs 192.168.1.0/24 CjMASQUERADE
Iptables-t nat-a postrouting-o ppp0-j MASQUERADEMASQUERADE -- to-ports port [-port]
Only valid if the rule also specifies-p tcp or-p udp. the most basic intranet access SNAT of a fixed public address (Internet interface address)
Iptables-t nat-a postrouting-s 192.168.0.0/24-o eth0-j SNAT -- to your eth0 address multiple intranet segments SNAT, just use multiple SNAT statements.
Iptables-t nat-a postrouting-s 192.168.100.0/24-o eth0-jMASQUERADE
Iptables-t nat-a postrouting-s 192.168.1.0/24-o eth0-jMASQUERADE
Iptables-t nat-a postrouting-s 192.168.0.0/24-o eth0-jMASQUERADE non-Internet port address is used for NAT. you must first bind it to the interface, for example, eth0: 1, eth0: 2
================================== Access the intranet through the Internet CJDNAT ==== ======================================
DNAT
Only valid in PREROUTING
-- To-destination ipaddr [-ipaddr] [: port-port]
DNAT: an example of a typical DNAT address for rewriting the destination IP address of a package
External interface ip address: 210.83.2.206
Internal interface ip address: 192.168.1.1
Ftp server: ip 192.168.1.3
Web server: ip 192.168.1.4
Iptables-t nat-a prerouting-d 210.83.2.206-p tcp -- dport 21-jDNAT -- to 192.168.1.3
Iptables-t nat-a prerouting-d 210.83.2.206-p tcp -- dport 80-jDNAT -- to 192.168.1.4
DNAT static ing
IPTABLES does not have static map like CISCO
DNAT is used for the load-balance (CISCO rotery) of the internal SERVER)
Iptables Ct nat ca prerouting Cd 219.142.217.161 Cj DNAT -- to-destination 192.168.1.24-192.168.1.25DNAT with Port ing (changing the SERVER port)
An example of ing an ftp server from 192.168.100.125: 21 to 216.94.87.37: 2121
Iptables-t nat-a prerouting-p tcp-d 216.94.87.37 -- dport 2121-j DNAT -- to-destination 192.168.100.125: 21
Generally, the DNAT on the internet accesses the intranet SERVER. the source address of the serv er package in the intranet goes through another independent SNAT process. Not part of the DNATSTATIC process.
In this way, the network application of P-t-P must have an SNAT suitable for DNAT.
For an application that passes through NAT and is mapped by NAT to change the port number, a separate SNAT must also be used to map the port of the returned packet.
Iptables-t nat-a postrouting-p tcp-s 192.168.100.125 -- sport 21-j SNAT -- to-source 216.94.87.37: 2121
Otherwise, the ftp server returns the client 21 to the internet. The Internet user sends an FTP request to 2121 and receives a from21 request.
The above does not seem to have to be necessary. I have done experiments:
/Sbin/iptables-t nat-a postrouting-s 10.4.0.0/16-o $ WAN_INT-jSNAT
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