Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418740
  • 博文数量: 86
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 822
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-25 10:36
文章分类

全部博文(86)

文章存档

2022年(1)

2021年(3)

2020年(1)

2019年(9)

2018年(24)

2017年(20)

2016年(20)

2015年(8)

我的朋友

分类: 系统运维

2021-05-18 11:23:03

1.一对一流量完全DNAT

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

    eth0 192.168.0.1  内网

    eth1 202.202.202.1 外网

内网中一台主机 192.168.0.101

现在要把外网访问202.202.202.1的所有流量映射到192.168.0.101上

命令如下:

[xhtml]  view plain  copy
  1. #将防火墙改为转发模式  
  2. echo 1 > /proc/sys/net/ipv4/ip_forward  
  3. iptables -F  
  4. iptables -t nat -F  
  5. iptables -t mangle -F  
  6. iptables -X  
  7. iptables -t nat -X  
  8. iptables -t mangle -X  
  9.   
  10. iptables -A INPUT -i lo -j ACCEPT  
  11. iptables -A OUTPUT -o lo -j ACCEPT  
  12.   
  13. iptables -P INPUT ACCEPT  
  14. iptables -P OUTPUT ACCEPT  
  15. iptables -P FORWARD ACCEPT  
  16.   
  17. iptables -t nat -A PREROUTING -d 202.202.202.1 -j DNAT --to-destination 192.168.0.101  
  18. iptables -t nat -A POSTROUTING -d 192.168.0.101 -j SNAT --to 192.168.0.1  


2.多对多流量完全DNAT

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

环境:

    eth0 192.168.0.1  内网

    eth1 202.202.202.1 、202.202.202.2 外网

内网中2台主机 192.168.0.101、192.168.0.102

现在要把外网访问202.202.202.1的所有流量映射到192.168.0.101上,
同时把把外网访问202.202.202.2的所有流量映射到192.168.0.102上

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

[c-sharp]  view plain  copy
  1. ifconfig eth1:1 202.202.202.2 netmask 255.255.255.0 up  


命令如下:

[c-sharp]  view plain  copy
  1. #将防火墙改为转发模式  
  2. echo 1 > /proc/sys/net/ipv4/ip_forward  
  3. iptables -F  
  4. iptables -t nat -F  
  5. iptables -t mangle -F  
  6. iptables -X  
  7. iptables -t nat -X  
  8. iptables -t mangle -X  
  9.   
  10. iptables -A INPUT -i lo -j ACCEPT  
  11. iptables -A OUTPUT -o lo -j ACCEPT  
  12.   
  13. iptables -P INPUT ACCEPT  
  14. iptables -P OUTPUT ACCEPT  
  15. iptables -P FORWARD ACCEPT  
  16.   
  17. iptables -t nat -A PREROUTING -d 202.202.202.1 -j DNAT --to-destination 192.168.0.101  
  18. iptables -t nat -A POSTROUTING -d 192.168.0.101 -j SNAT --to 192.168.0.1  
  19.   
  20. iptables -t nat -A PREROUTING -d 202.202.202.2 -j DNAT --to-destination 192.168.0.102  
  21. 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上

命令如下:

[c-sharp]  view plain  copy
  1. #将防火墙改为转发模式  
  2. echo 1 > /proc/sys/net/ipv4/ip_forward  
  3. iptables -F  
  4. iptables -t nat -F  
  5. iptables -t mangle -F  
  6. iptables -X  
  7. iptables -t nat -X  
  8. iptables -t mangle -X  
  9.   
  10. iptables -A INPUT -i lo -j ACCEPT  
  11. iptables -A OUTPUT -o lo -j ACCEPT  
  12.   
  13. iptables -P INPUT ACCEPT  
  14. iptables -P OUTPUT ACCEPT  
  15. iptables -P FORWARD ACCEPT  
  16.   
  17. iptables -t nat -A PREROUTING -d 202.202.202.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.0.101:80  
  18. iptables -t nat -A POSTROUTING -d 192.168.0.101 -p tcp --dport 80 -j SNAT --to 192.168.0.1  
  19.   
  20. iptables -t nat -A PREROUTING -d 202.202.202.1 -p tcp --dport 25 -j DNAT --to-destination 192.168.0.102:25  
  21. iptables -t nat -A POSTROUTING -d 192.168.0.102 -p tcp --dport 25 -j SNAT --to 192.168.0.1  
  22.   
  23. iptables -t nat -A PREROUTING -d 202.202.202.1 -p tcp --dport 110 -j DNAT --to-destination 192.168.0.102:110  
  24. 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

阅读(724) | 评论(0) | 转发(0) |
0

上一篇:pidof 逗号分隔

下一篇:multipass

给主人留下些什么吧!~~