嵌入式 创建 GRE Tunnel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011425939/article/details/80610941

一、确定ip_gre 模块是否被加载

$ sudo modprobe ip_gre
$ lsmod | grep gre

结果:

# lsmod | grep gre
ip_gre                 12594  0 
gre                     1106  1 ip_gre

如果没有要进行内核的配置,步骤如下。

1、进入内核文件目录 cd linux-3.0.y

make menuconfig

2、进入 Networking support


3、Networking options


4、找到红色框中的选项,选择模块编译,这样就只要加载模块,而不需要重新烧写内核。

5、保存退出


6、编译内核

make zimage

7、拷贝gre.ko 、ip_gre.ko 两个文件到设备目录

8、加载模块

# insmod ./gre.ko 
# insmod ./ip_gre.ko 

9、查看是否加载

lsmod | grep gre
如果出现了,则加载完成。

ip_gre                 12594  0 
gre                     1106  1 ip_gre

二、建立GRE Tunnel

1、在主机A和B两个接口之间创建一个GRE隧道,并使用以下IP地址。

host A:192.168.9.161
host B:192.168.9.166

这个两个地址可以在不同网段中,只要能互相ping同就可以。

2、在主机A运行如下命令

ip tunnel add atb mode gre remote 192.168.9.166 local 192.168.9.161 ttl 255
ip link set atb up
ip addr add 10.10.10.1/24 dev atb

在上面,我们创建了一个名为atb的灰色隧道设备,并将其远程地址设置为192.168.9.166。隧道数据包将来自192.168.9.161(本地IP地址),并且它们的TTL字段将被设置为255。隧道设备被分配IP地址10.10.10.1和netmask 255.255.255.0。

3、在主机B运行如下命令

ip tunnel add atb mode gre remote 192.168.9.161 local 192.168.9.166 ttl 255
ip link set atb up
ip addr add 10.10.10.2/24 dev atb

4、在主机B查看路由

# ip route show
default via 192.168.9.1 dev eth0 
10.10.10.0/24 dev atb  src 10.10.10.2
192.168.9.0/24 dev eth0  src 192.168.9.166 

这里多了 个atb ,同样也可以在A主机看到相同的结果。

 5、ifcong 查找A主机

root@ubuntu16:# ifconfig
atb       Link encap:未指定  硬件地址 C0-A8-09-A1-00-00-3C-45-00-00-00-00-00-00-00-00  
          inet 地址:10.10.10.1  点对点:10.10.10.1  掩码:255.255.255.0
          inet6 地址: fe80::5efe:c0a8:9a1/64 Scope:Link
          UP POINTOPOINT RUNNING NOARP  MTU:1476  跃点数:1
          接收数据包:1479 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:1021 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1 
          接收字节:1576499 (1.5 MB)  发送字节:81643 (81.6 KB)

6、ifconfig 查看B主机

# ifconfig       
atb       Link encap:UNSPEC  HWaddr C0-A8-09-A6-00-00-00-00-00-00-00-00-00-00-00-00  
          inet addr:10.10.10.2  P-t-P:10.10.10.2  Mask:255.255.255.0
          UP POINTOPOINT RUNNING NOARP  MTU:1476  Metric:1
          RX packets:877 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1591 errors:17 dropped:0 overruns:0 carrier:17
          collisions:0 txqueuelen:0 
          RX bytes:69631 (67.9 KiB)  TX bytes:1551103 (1.4 MiB)
7、在主机 A ping 主机B
root@ubuntu16:# ping 10.10.10.2
PING 10.10.10.2 (10.10.10.2) 56(84) bytes of data.
64 bytes from 10.10.10.2: icmp_seq=1 ttl=64 time=1.96 ms
64 bytes from 10.10.10.2: icmp_seq=2 ttl=64 time=1.73 ms
64 bytes from 10.10.10.2: icmp_seq=3 ttl=64 time=1.22 ms
64 bytes from 10.10.10.2: icmp_seq=4 ttl=64 time=1.87 ms
64 bytes from 10.10.10.2: icmp_seq=5 ttl=64 time=1.68 ms

可以ping通说明已经在主机A和主机B之间建立了GRE Tunnel

8、如果想改变,远程主机的ip或者本地主机的ip

ip tunnel change atb remote 192.168.9.161 local 192.168.9.166

9、断开隧道

ip link set atb down

10、删除隧道

ip tunnel del atb




猜你喜欢

转载自blog.csdn.net/u011425939/article/details/80610941
gre