Chinaunix首页 | 论坛 | 博客
  • 博客访问: 617174
  • 博文数量: 73
  • 博客积分: 1813
  • 博客等级: 上尉
  • 技术积分: 1213
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-27 19:59
文章分类
文章存档

2013年(1)

2012年(12)

2011年(28)

2010年(31)

2009年(1)

我的朋友

分类: LINUX

2010-02-03 15:18:01

要求:将192.168.3.195:80 映射到192.168.3.193:80,即访问
 
192.168.3.195:80,得到192.168.3.193:80的结果,实现linux的路由
 
功能
 
方法:采用下面的脚本即可
 
==========================================================
#!/bin/bash
 
echo "1" > /proc/sys/net/ipv4/ip_forward #打开转发功能
         
/sbin/iptables -F -t filter

/sbin/iptables -F -t nat  #清空iptables
         
/sbin/iptables -t nat -A PREROUTING -d 192.168.3.195 -p tcp --dport 80 -j DNAT --to-destination 192.168.3.193:80    #去192.168.3.193的一条路
 
/sbin/iptables -t nat -A POSTROUTING  -s 192.168.3.0/24 -o eth0 -j SNAT --to 192.168.3.195    #返回的时候的一条路,ip传输要有去有回才能连通
/sbin/iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j SNAT --to 192.168.3.195  #用2网段访问的时候的回路
/sbin/iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j SNAT --to 192.168.3.195  #用0网段访问的时候的回路
=========================================================
 
回路的那一条,或者只用下面这一句:
iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -o eth0 -j SNAT --to 192.168.3.195 
或者
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 192.168.3.195
把193上的80端口打开
 
测试:
在192.168.3.195上访问 192.168.3.195:80看到 195的apache主页,因
 
为在本机上访问,没有走PREROUTING这条链。
在其他主机上访问192.168.3.195:80 返回193的apache主页,路线为
 
PREROUTING-->FORWARD-->PSOTROUTING.
 
 
结束!
阅读(6333) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~