iptables中DNAT和SNAT转发的配置方法

1.一对一流量彻底DNAT服务器

首先说一下网络环境,普通主机一台作防火墙用,网卡两块网络

eth0 192.168.0.1  内网tcp

eth1 202.202.202.1 外网spa

内网中一台主机 192.168.0.101.net

如今要把外网访问202.202.202.1的全部流量映射到192.168.0.101上code

命令以下:blog

#将防火墙改成转发模式  
echo 1 > /proc/sys/net/ipv4/ip_forward  
iptables -F  
iptables -t nat -F  
iptables -t mangle -F  
iptables -X  
iptables -t nat -X  
iptables -t mangle -X  
  
iptables -A INPUT -i lo -j ACCEPT  
iptables -A OUTPUT -o lo -j ACCEPT  
  
iptables -P INPUT ACCEPT  
iptables -P OUTPUT ACCEPT  
iptables -P FORWARD ACCEPT  
  
iptables -t nat -A PREROUTING -d 202.202.202.1 -j DNAT --to-destination 192.168.0.101  
iptables -t nat -A POSTROUTING -d 192.168.0.101 -j SNAT --to 192.168.0.1  

2.多对多流量彻底DNATip

说是多对多,实际上这里的配置是指定了多个一对一get

环境:io

eth0 192.168.0.1  内网

eth1 202.202.202.1 、202.202.202.2 外网

内网中2台主机 192.168.0.10一、192.168.0.102

如今要把外网访问202.202.202.1的全部流量映射到192.168.0.101上,同时把把外网访问202.202.202.2的全部流量映射到192.168.0.102上

这里顺便提一下如何为网卡配置多个IP

ifconfig eth1:1 202.202.202.2 netmask 255.255.255.0 up  

命令以下: 

#将防火墙改成转发模式  
echo 1 > /proc/sys/net/ipv4/ip_forward  
iptables -F  
iptables -t nat -F  
iptables -t mangle -F  
iptables -X  
iptables -t nat -X  
iptables -t mangle -X  
  
iptables -A INPUT -i lo -j ACCEPT  
iptables -A OUTPUT -o lo -j ACCEPT  
  
iptables -P INPUT ACCEPT  
iptables -P OUTPUT ACCEPT  
iptables -P FORWARD ACCEPT  
  
iptables -t nat -A PREROUTING -d 202.202.202.1 -j DNAT --to-destination 192.168.0.101  
iptables -t nat -A POSTROUTING -d 192.168.0.101 -j SNAT --to 192.168.0.1  
  
iptables -t nat -A PREROUTING -d 202.202.202.2 -j DNAT --to-destination 192.168.0.102  
iptables -t nat -A POSTROUTING -d 192.168.0.102 -j SNAT --to 192.168.0.1 

3.一对多根据协议DNAT

 这个最经常使用,通常是内网或DMZ区内有多个应用服务器,能够将不一样的应用流量映射到不一样的服务器上

环境:

eth0 192.168.0.1  内网

eth1 202.202.202.1  外网

内网中2台主机 192.168.0.101(Web服务器)、192.168.0.102(邮件服务器)

如今要把外网访问202.202.202.1的Web流量映射到192.168.0.101上,同时把把外网访问202.202.202.1的邮件访问流量映射到192.168.0.102上

命令以下:

#将防火墙改成转发模式  
echo 1 > /proc/sys/net/ipv4/ip_forward  
iptables -F  
iptables -t nat -F  
iptables -t mangle -F  
iptables -X  
iptables -t nat -X  
iptables -t mangle -X  
  
iptables -A INPUT -i lo -j ACCEPT  
iptables -A OUTPUT -o lo -j ACCEPT  
  
iptables -P INPUT ACCEPT  
iptables -P OUTPUT ACCEPT  
iptables -P FORWARD ACCEPT  
  
iptables -t nat -A PREROUTING -d 202.202.202.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.101:80  
iptables -t nat -A POSTROUTING -d 192.168.0.101 -p tcp --dport 80 -j SNAT --to 192.168.0.1  
  
iptables -t nat -A PREROUTING -d 202.202.202.1 -p tcp --dport 25 -j DNAT --to-destination 192.168.0.102:25  
iptables -t nat -A POSTROUTING -d 192.168.0.102 -p tcp --dport 25 -j SNAT --to 192.168.0.1  
  
iptables -t nat -A PREROUTING -d 202.202.202.1 -p tcp --dport 110 -j DNAT --to-destination 192.168.0.102:110  
iptables -t nat -A POSTROUTING -d 192.168.0.102 -p tcp --dport 110 -j SNAT --to 192.168.0.1

转自:http://blog.csdn.net/bill_lee_sh_cn/article/details/4401896

相关文章
相关标签/搜索