# GRE隧道
## 一、GRE介绍
GRE(Generic Routing Encapsulation)即通用路由封装协议,是有Cisco公司开发的一项轻量级隧道协议(目前网络厂商基本都支持GRE协议)。它能够将各种网络协议(IP协议与非IP协议)封装在隧道内。GRE之所以称为轻量级隧道协议时因为GRE头部小,所以封装效率高。但是GRE没有任何安全机制,是以明文方式传输,可通过抓包看到Tunnl IP信息。一般都是使用IPSec来对GRE进行加密保护。
## 二、配置要点
1、配置隧道接口编号 (interface Tunnel 1)
2、配置隧道模式(本例以配置GRE IP模式为例)
3、配置隧道的源地址
4、配置隧道目的地址
5、配置隧道路由
注意:如果隧道两端Tunnel接口的地址不在同一个网段,则必须配
置通过隧道到达对端的转发路由,以便需要进行封装的报文能正常
转发。用户可以配置静态路由,也可以配置动态路由,在Tunnel的
两端都要进行此项配置。对两个或两个以上使用同种封装协议的
Tunnel接口,不能同时配置完全相同的源地址和目的地址。配置
Tunnel接口的源端地址时,若采用配置源接口形式,则Tunnel的源
地址取的是源接口的主IP地址。

配置步骤:
基本的接口给ip
隧道配置

```
3-SW1:
3-SW1(config)#interface tunnel 1 #配置隧道接口编号
3-SW1(config-if)#tunnel mode gre ip #配置隧道模式
3-SW1(config-if)#ip address 10.10.100.1
255.255.255.0 #隧道ip地址
3-SW1(config-if)#tunnel source 172.16.1.1 #源地址
f0/2
3-SW1(config-if)#tunnel destination 172.16.1.2 #目标地址
```
3-SW2:

```
3-SW1(config)#interface tunnel 1 #配置隧道接口编号
3-SW1(config-if)#tunnel mode gre ip #配置隧道模式
3-SW1(config-if)#ip address 10.10.100.2 255.255.255.0 #隧道ip地址
3-SW1(config-if)#tunnel source 172.16.1.2 #源地址
f0/2
3-SW1(config-if)#tunnel destination 172.16.1.1 #目标地址
```
配置全网通信还差一条静态路由
3-SW1少192.168.2.0这个网段的
3-SW2少192.168.1.0这个网段的
例题:

R3
```
ip rouet 10.1.1.0 255.255.255.0 10.3.3.2 #与R0建立通信
ip route 10.2.2.0 255.255.255.0 10.3.3.2 #与R1建立通信
```
R3创建隧道
```
int tunnel 1
tunnel mode gre ip
ip add 1.1.1.1 255.255.255.0
tunnel source g0/0 #源端口
tunnel destination 10.1.1.1 #目的地址
int tunnel 2
tunnel mode gre ip
ip add 2.2.2.2 255.255.255.0
tunnel source g0/0
tunnel destination 10.2.2.1
```
R3
```
ip route 192.168.10.0 255.255.255.0 1.1.1.2
与192.168.10.0网段建立通信,下一跳出口为R0的隧道ip
ip route 192.168.20.0 255.255.255.0 2.2.2.2
与192.168.20.0网段建立通信,下一跳出口为R1的隧道ip
```
R0返回路由
```
ip route 10.3.3.0 255.255.255.0 10.1.1.2
```
R0建立隧道
```
int tunnel 1
tunnel mode gre ip
ip add 1.1.1.2 255.255.255.0
tunnel source g0/1 #源端口
tunnel destination 10.3.3.1 #目的地址
```
R2返回路由
```
ip route 10.3.3.0 255.255.255.0 10.2.2.2
```
R2建立隧道
```
int tunnel 2
tunnel mode gre ip
ip add 2.2.2.2 255.255.255.0
tunnel source g0/1 #源端口
tunnel destination 10.3.3.1 #目的地址
```
使用tracert查看效果

最后修改:2021 年 11 月 15 日
© 允许规范转载