两台linux机器上建立ip隧道

Linux知识
0 661

机器A:173.26.100.80
机器B:173.26.100.162

第一步:
在机器A上:
ip tunnel add ethn mode ipip local 173.26.100.80 remote 173.26.100.162
其中,ethn为ip隧道的虚拟网卡名,
mode ipip设置ip隧道的模式为ipip,除了ipip,还有很多其他模式,如gre,sit等等,以ipip和gre最为常见
同样,在机器B上:
ip tunnel add ethm mode ipip local 173.26.100.162 remote 173.26.100.80
ethm为ip隧道的虚拟网卡名

至此,ip隧道已经建立起来了


第二步:
但是要想实现A和B通过ip隧道通信,还需要给A和B的隧道对应的网卡设置一个ip,
在机器A上:
ifconfig ethn 41.211.25.78
在机器B上:
ifconfig ethm 62.72.100.128
这里A和B的隧道网卡的ip可以随便设定
可以在A和B上分别ifconfig看看,是不是多了ethn/ethm这个网卡

第三步:
别急,最后一步,设置一下路由规则
在机器A上:
route add -host 62.72.100.128 dev ethn
让目的ip为62.72.100.128的数据包走ethn这个网卡
在机器B上:
route add -host 41.211.25.78 dev ethm
让目的ip为41.211.25.78的数据包走ethm这个网卡

第四步:
在机器A上
ping  62.72.100.128
在机器B上
ping  41.211.25.78

如果都可以ping通,恭喜了,ipip隧道已经成功建立了

reference:
*[http://blog.chinaunix.net/u/553/showart_430370.html Linux下的IP隧道研究]
*[http://blog.chinaunix.net/u/16029/showart_133364.html Linux中IP隧道的分析与建议]
*[http://www.chinaunix.net/jh/4/1055425.html (原创) linux tunnel 技术]
*[http://www.linuxsir.org/bbs/thread89987.html 使用Linux建立IP隧道]
*[http://www.xiaojb.com/archives/it/ip-tunnel.shtml  2台linux服务器建立IP隧道]