centos iptables端口映射

端口映射 实现步骤如下:
一、环境和要实现功能
PC1的网络设置如下:
eth0 10.10.77.1 内网
eth1 10.6.5.5 外网
PC2的网络设置则为:10.10.77.99 内网
我们要实现的功能就是将PC1的8080端口映射到PC2的80端口,也即访问
http://6.6.5.5:8080
即可访问到PC2上的WEB服务。
  二、实现步骤
1、 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0

/etc/sysconfig/iptables 样例 映设端口 8111

# Generated by iptables-save v1.3.5 on Mon Oct 14 17:12:54 2013
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A PREROUTING -p udp -m udp --dport 53 -j DNAT --to-destination 8.8.8.8 
-A PREROUTING -p udp -m udp --dport 53 -j DNAT --to-destination 8.8.4.4 
-A POSTROUTING -s 10.10.77.0/255.255.255.0 -o eth0 -j MASQUERADE 
-A POSTROUTING -s 10.10.88.0/255.255.255.0 -o eth0 -j MASQUERADE 
-A POSTROUTING -s 10.10.99.0/255.255.255.0 -o eth0 -j MASQUERADE 
-A POSTROUTING -s 10.10.77.0/255.255.255.0 -j SNAT --to-source 10.6.5.5 
-A POSTROUTING -s 10.10.88.0/255.255.255.0 -j SNAT --to-source 10.6.5.5 
-A POSTROUTING -s 10.10.99.0/255.255.255.0 -j SNAT --to-source 10.6.5.5

-A PREROUTING -d 10.6.5.5 -p tcp -m tcp --dport 8111 -j DNAT --to-destination 10.10.77.99:8111
-A POSTROUTING -d 10.10.77.99 -p tcp -m tcp --dport 8111 -j SNAT --to-source 10.10.77.1




COMMIT
# Completed on Mon Oct 14 17:12:54 2013
# Generated by iptables-save v1.3.5 on Mon Oct 14 17:12:54 2013
*filter
:INPUT ACCEPT [1:40]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1:648]
-A INPUT -i tun+ -j ACCEPT 
-A INPUT -i tap+ -j ACCEPT 
-A INPUT -d 10.6.5.5 -p udp -m udp --dport 500 -j ACCEPT 
-A INPUT -d 10.6.5.5 -p udp -m udp --dport 4500 -j ACCEPT 
-A INPUT -d 10.6.5.5 -p udp -m udp --dport 1701 -j ACCEPT 

COMMIT
# Completed on Mon Oct 14 17:12:54 2013
点赞