其他國家的互聯網如同一個孤島。要想訪問國外網站異常的緩慢,甚至被和諧了。可以建立一條隧道來避免這種情況,下面說說GRE隧道如何建立。
1. GRE介紹
GRE隧道是一種IP-over-IP的隧道,是通用路由封裝協議,可以對某些網路層協議的數據報進行封裝,使這些被封裝的數據報能夠在IPv4/IPv6 網絡中傳輸。
Tunnel 是一個虛擬的點對點的連接,提供了一條通路使封裝的數據報文能夠在這個通路上傳輸,並且在一個Tunnel 的兩端分別對數據報進行封裝及解封裝。 一個X協議的報文要想穿越IP網絡在Tunnel中傳輸,必須要經過加封裝與解封裝兩個過程。
要在Linux上創建GRE隧道,需要ip_gre內核模塊,它是GRE通過IPv4隧道的驅動程序。
2. 查看是否有加載ip_gre模塊
|
1
2
3
4
|
# modprobe ip_gre
# lsmod | grep gre
ip_gre 22432 0
gre 12989 1 ip_gre
|
3. 創建步驟
環境如下:
host A : 121.207.22.123
host B: 111.2.33.28
在host A上面:
|
1
2
3
|
# ip tunnel add gre1 mode gre remote 111.2.33.28 local 121.207.22.123 ttl 255
# ip link set gre1 up
# ip addr add 10.10.10.1 peer 10.10.10.2 dev gre1
|
創建一個GRE類型隧道設備gre0, 並設置對端IP為111.2.33.28。隧道數據包將被從121.207.22.123也就是本地IP地址發起,其TTL字段被設置為255。隧道設備分配的IP地址為10.10.10.1,掩碼為255.255.255.0。
在host B上面:
|
1
2
3
|
# ip tunnel add gre1 mode gre remote 121.207.22.123 local 111.2.33.28 ttl 255
# ip link set gre1 up
# ip addr add 10.10.10.2 peer 10.10.10.1 dev gre1
|
此時,host A 和 host B 建立起GRE隧道了。
4. 檢測連通性
|
1
2
3
4
5
|
# ping 10.10.10.2 (host A)
PING 10.10.10.2 (10.10.10.2) 56(84) bytes of data.
64 bytes from 10.10.10.2: icmp_req=1 ttl=64 time=0.319 ms
64 bytes from 10.10.10.2: icmp_req=2 ttl=64 time=0.296 ms
64 bytes from 10.10.10.2: icmp_req=3 ttl=64 time=0.287 ms
|
5. 撤銷GRE隧道
在任一一端操作下面命令
|
1
2
|
# ip link set gre1 down
# ip tunnel del gre1
|
轉載請注明來自運維生存時間: http://www.ttlsa.com/html/4138.html
參考文章:http://www.ttlsa.com/linux/create-a-gre-tunnel-linux/
