两台linux建立GRE隧道

简介:

1.拓扑图: 

备注:因为应用原因,需要在linux2上添加一个公网地址,并且在中间路由设备不受控制的情况下,Linux1能访问到linux2上面的公网地址。

2.基本接口配置:

linux1:192.168.10.1/24

linux2:192.168.20.2/24

R1:

interface FastEthernet0/0
 ip address 192.168.10.10 255.255.255.0
 no shutdown
!
interface FastEthernet0/1
 ip address 192.168.20.10 255.255.255.0
 no shutdown

3.路由配置:

linux1网关:192.168.10.10
linux2网关:192.168.20.10
R1:只有直连路由

4.Linux2单网卡多地址配置:

[root@Linux1 ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
[root@Linux1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0:0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0:0
BOOTPROTO=none
ONBOOT=yes
HWADDR=00:0c:29:08:48:63
NETMASK=255.255.255.252
IPADDR=202.100.2.2

TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes

[root@Linux1 ~]# service network restart
[root@Linux1 ~]# ping 202.100.2.2
PING 202.100.2.2 (202.100.2.2) 56(84) bytes of data.
64 bytes from 202.100.2.2: icmp_seq=1 ttl=64 time=0.124 ms

--- 202.100.2.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.124/0.124/0.124/0.000 ms
[root@ams ~]# ping 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10: icmp_seq=1 ttl=255 time=70.6 ms

--- 192.168.10.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 70.629/70.629/70.629/0.000 ms

5.GRE tunnel配置:

A.确定是否加载了GRE模块 
[root@Linux1 ~]# lsmod |grep ip_gre
[root@Linux2 ~]# lsmod |grep ip_gre
B.加载GRE模块

[root@linux1 ~]# uname -an
Linux linux1 2.6.9-78.EL #1 Wed Jul 9 15:27:01 EDT 2008 i686 i686 i386 GNU/Linux
[root@linux1 ~]# insmod /lib/modules/2.6.9-78.EL/kernel/net/ipv4/ip_gre.ko
[root@linux2 ~]# uname -an
Linux linux2 2.6.18-164.el5 #1 SMP Thu Sep 3 03:33:56 EDT 2009 i686 i686 i386 GNU/Linux
[root@linux2 ~]# insmod /lib/modules/2.6.18-164.el5/kernel/net/ipv4/ip_gre.ko

C.GRE tunnel接口配置

Linux1:
ip tunnel add tunnel0 mode gre remote 192.168.20.2 local 192.168.10.1 ttl 255
ip link set tunnel0 up mtu 1400
ip addr add 172.16.1.1/30  dev tunnel0
ip addr add 172.16.1.1/30 peer 172.16.1.2/30 dev tunnel0
ip route add 202.100.2.2/32 dev tunnel0
Linux2:
ip tunnel add tunnel0 mode gre remote 192.168.10.1 local 192.168.20.2 ttl 255
ip link set tunnel0 up mtu 1400
ip addr add 172.16.1.2/30  dev tunnel0
ip addr add 172.16.1.2/30 peer 172.16.1.1/30 dev tunnel0
D.将tunnel配置开机运行:

linux1:
vi /etc/init.d/gre.sh   ##内容如下:
insmod /lib/modules/2.6.9-78.EL/kernel/net/ipv4/ip_gre.ko
ip tunnel add tunnel0 mode gre remote 192.168.20.2 local 192.168.10.1 ttl 255
ip link set tunnel0 up mtu 1400
ip addr add 172.16.1.1/30  dev tunnel0
ip addr del 172.16.1.1/30 peer 172.16.1.2/30 dev tunnel0
ip addr add 172.16.1.1/30 peer 172.16.1.2/30 dev tunnel0
ip route add 202.100.2.2/32 dev tunnel0

chmod +x /etc/init.d/gre.sh 
echo "/etc/init.d/gre.sh" >> /etc/rc.d/rc.local


linux2:
vi /etc/init.d/gre.sh   ##内容如下:
insmod /lib/modules/2.6.18-164.el5/kernel/net/ipv4/ip_gre.ko
ip tunnel add tunnel0 mode gre remote 192.168.10.1 local 192.168.20.2 ttl 255
ip link set tunnel0 up mtu 1400
ip addr add 172.16.1.2/30  dev tunnel0
ip addr del 172.16.1.2/30 peer 172.16.1.1/30 dev tunnel0
ip addr add 172.16.1.2/30 peer 172.16.1.1/30 dev tunnel0

chmod +x /etc/init.d/gre.sh 
echo "/etc/init.d/gre.sh" >> /etc/rc.d/rc.local


D.验证GRE接口 

[root@Linux1 ~]# ip addr show
1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:e4:65:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.1/24 brd 192.168.10.255 scope global eth0
    inet6 fe80::20c:29ff:fee4:6578/64 scope link 
       valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop 
    link/sit 0.0.0.0 brd 0.0.0.0
4: gre0: <NOARP> mtu 1476 qdisc noop 
    link/gre 0.0.0.0 brd 0.0.0.0
5: tunnel0@NONE: <POINTOPOINT,NOARP,UP> mtu 1400 qdisc noqueue 
    link/gre 192.168.10.1 peer 192.168.20.2
    inet 172.16.1.1 peer 172.16.1.2/30 scope global tunnel0
[root@Linux2 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:08:48:63 brd ff:ff:ff:ff:ff:ff
    inet 192.168.20.2/24 brd 192.168.20.255 scope global eth0
    inet 202.100.2.2/30 brd 202.100.2.3 scope global eth0:0
    inet6 fe80::20c:29ff:fe08:4863/64 scope link 
       valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop 
    link/sit 0.0.0.0 brd 0.0.0.0
4: gre0: <NOARP> mtu 1476 qdisc noop 
    link/gre 0.0.0.0 brd 0.0.0.0
5: tunnel0@NONE: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1400 qdisc noqueue 
    link/gre 192.168.20.2 peer 192.168.10.1
    inet 172.16.1.2 peer 172.16.1.1/30 scope global tunnel0

5.效果测试:

[root@linux1 ~]#  ping 202.100.2.2
PING 202.100.2.2 (202.100.2.2) 56(84) bytes of data.
64 bytes from 202.100.2.2: icmp_seq=0 ttl=64 time=82.4 ms
64 bytes from 202.100.2.2: icmp_seq=1 ttl=64 time=48.7 ms

--- 202.100.2.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 48.784/65.633/82.482/16.849 ms, pipe 2





本文转自 碧云天 51CTO博客,原文链接:http://blog.51cto.com/333234/931805,如需转载请自行联系原作者

相关文章
|
1月前
|
安全 Linux 网络安全
Linux使用HTTP隧道代理代码示例模版
Linux使用HTTP隧道代理代码示例模版
16 0
|
26天前
|
存储 安全 Linux
【Shell 命令集合 磁盘维护 】Linux 在特定的分区上建立 linux 文件系统 mkfs命令使用教程
【Shell 命令集合 磁盘维护 】Linux 在特定的分区上建立 linux 文件系统 mkfs命令使用教程
25 0
|
26天前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 建立串行连接 cu命令 使用指南
【Shell 命令集合 网络通讯 】Linux 建立串行连接 cu命令 使用指南
28 0
|
6月前
|
IDE Linux Shell
linux中建立pycharm快捷方式
linux中建立pycharm快捷方式
|
8月前
|
网络协议 Linux 数据库
Linux TCP作为服务器连接方式:建立稳健高效的服务器通信
在Linux服务器开发中,TCP(Transmission Control Protocol)是一种常用的传输层协议,它为服务器与客户端之间的连接提供可靠的、面向连接的通信方式。本文将深入探讨Linux TCP作为服务器连接方式的工作原理,包括服务器端的建立、连接管理和数据传输,以帮助读者建立稳健高效的服务器通信。
205 0
|
9月前
|
关系型数据库 MySQL Shell
linux下建立mysql数据库备份脚本
将以上内容放到text文件中,将后缀名修改为.sh文件。
|
9月前
|
存储 Unix Linux
Linux设备驱动程序(二)——建立和运行模块
本章介绍所有的关于模块和内核编程的关键概念,通过一个 hello world 模块来认识驱动加载的流程及相关细节。
84 0
|
10月前
|
Linux 开发工具 数据安全/隐私保护
如何建立Linux与git的连接?
建立连接三板斧: add , commit ,push Linux与git远程连接的方法: 1.设置全局的用户名和邮箱 git config – global user.name “你的用户名” git config – global user.email “你的邮箱”
|
Java 应用服务中间件 Linux
linux篇-linux 下建立多个tomcat
linux篇-linux 下建立多个tomcat
80 0
linux篇-linux 下建立多个tomcat
|
Linux
11.8 Linux查询已建立好的磁盘配额(quota和repquota命令)
对于建立好的磁盘配额,还需要有效的查询配额的手段,这样才能知道系统中到底有哪些分区设定了哪些配额。
250 0
11.8 Linux查询已建立好的磁盘配额(quota和repquota命令)