Port ing of iptables in CentOS
I. Environment and demand lab environment
The network settings of the node are as follows:
NodeNicIPNetworkNode1eth0172.19.104.33 Intranet Node1eth16.6.5.5 Internet Node2eth0172.19.104.14 IntranetRequirement
The function we need to implement is to map port 8100 of Node1 to port 8000 of Node2, that is, access http: // 6.6.5.5: 8100 over the Internet to access the WEB service on port 8000 of PC2.
II. Implementation steps
The following operations are performed on node1. some modifications may require the root permission.
1. Edit/etc/sysctl.confConfiguration Filenet.ipv4.ip_forward = 1The default value is 0. Run the command again.sysctl -pMake it take effect
Or directly execute
echo 1 > /proc/sys/net/ipv4/ip_forward
2. Execute
iptables -t nat -A PREROUTING -d 172.19.104.33 -p tcp --dport 8100 -j DNAT --to-destination 172.19.104.14:8000iptables -t nat -A POSTROUTING -d172.19.104.14 -p tcp --dport 8000 -j SNAT --to 172.19.104.33 iptables -A FORWARD -o eth0 -d172.19.104.14 -p tcp --dport 8000 -j ACCEPTiptables -A FORWARD -i eth0 -s 172.19.104.14 -p tcp --sport 8000 -j ACCEPTiptables save
Note the IP address, port, and nic configurations.
Or directly modify the location file
Edit/etc/sysconfig/iptablesAdd the following content
The following is a reference file, which can be modified according to the actual situation.
...-A PREROUTING -d 172.19.104.33 -p tcp -m tcp --dport 8100 -j DNAT --to-destination 172.19.104.14:8000-A POSTROUTING -d 172.19.104.14 -p tcp -m tcp --dport 8000 -j SNAT --to-source 172.19.104.33...-A FORWARD -o eth0 -d 172.19.104.14 -p tcp --dport 8000 -j ACCEPT-A FORWARD -i eth0 -s 172.19.104.14 -p tcp --dport 8000 -j ACCEPT...
3. Restart iptables.
/etc/init.d/iptables restart
Or
service iptables restart
Iii. TestAccess http: // 6.6.5.5: 8100 to access the WEB service on port 8000 of Node2.