博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iptables中DNAT转发的配置方法
阅读量:2390 次
发布时间:2019-05-10

本文共 2740 字,大约阅读时间需要 9 分钟。

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]   
  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]   
  1. ifconfig eth1:1 202.202.202.2 netmask 255.255.255.0 up  

命令如下:

[c-sharp]   
  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]   
  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://xupab.baihongyu.com/

你可能感兴趣的文章
qtcreator中常用快捷键
查看>>
PowerDesigner 简介
查看>>
VS2008快捷键大全
查看>>
Access 操作或事件已被禁用模式阻止
查看>>
C# 控件置于最顶层、最底层
查看>>
几个常见的压缩算法
查看>>
浮点数的存储
查看>>
点到线段的距离
查看>>
HTML_5_Canvas
查看>>
NuGet学习笔记(1)——初识NuGet及快速安装使用
查看>>
NuGet学习笔记(2)——使用图形化界面打包自己的类库
查看>>
C# 数据类型基础,堆栈,装箱与拆箱
查看>>
HTML 中的<div>
查看>>
C#中的static、readonly与const的比较
查看>>
C# ListView用法详解
查看>>
搜索自己博客中的文章
查看>>
出色图形用户界面(GUI)设计规范(下)
查看>>
C# winform 实现选择文件夹对话框
查看>>
c# 获取相对路径
查看>>
在ORACLE中给表、列增加注释以及读取注释
查看>>