配置IP隧道

類似於VPN,IP隧道通過第三個網絡(例如Internet)直接連接兩個網絡。但是,並非所有隧道協議都支持加密。

建立隧道的兩個網絡中的路由器都至少需要兩個接口:

  • 連接到本地網絡的一個接口

  • 一個連接到建立隧道的網絡的接口

要建立隧道,請使用遠程子網中的IP地址在兩個路由器上創建一個虛擬接口。

NetworkManager支持以下IP隧道:

  • 通用路由封裝(GRE)

  • IPv6上的通用路由封裝(IP6GRE)

  • 通用路由封裝終端訪問點(GRETAP)

  • IPv6上的通用路由封裝終端訪問點(IP6GRETAP)

  • IPv4 over IPv4(IPIP)

  • IPv4 over IPv6(IPIP6)

  • IPv6 over IPv6(IP6IP6)

簡單的互聯網過渡(SIT) 根據類型的不同,這些隧道將作用於開放系統互連(OSI)模型的第2層或第3層。

一、使用nmcli配置IPIP隧道以將IPv4流量封裝在IPv4數據包中

IP over IP(IPIP)隧道在OSI層3上運行,並將IPv4流量封裝在RFC 2003中描述的IPv4數據包中。

通過IPIP隧道發送的數據未加密。出於安全原因,僅將隧道用於已被其他協議(例如HTTPS)加密的數據。

請注意,IPIP隧道僅支持單播數據包。如果需要支持多播的IPv4隧道,請參閱使用nmcli配置GRE隧道以將第3層流量封裝在IPv4數據包中。

下面示例在兩個RHEL路由器之間創建IPIP隧道,以通過Internet連接兩個內部子網,如下圖所示:

IPIP tunnel

先決條件

  • 每個RHEL路由器都有一個連接到其本地子網的網絡接口

  • 每個RHEL路由器都有一個連接到Internet的網絡接口

  • 通過隧道發送的流量是IPv4單播

1.1 在網絡A中的路由器上:

創建一個名爲tun0的IPIP隧道接口:

# nmcli connection add type ip-tunnel ip-tunnel.mode ipip con-name tun0 ifname tun0 remote 198.51.100.5 local 203.0.113.10

remote和local參數設置遠程和本地路由器的公共IP地址。

將IPv4地址設置爲tun0設備:

# nmcli connection modify tun0 ipv4.addresses '10.0.1.1/30'

請注意,具有兩個可用IP地址的/30子網足以用於隧道。

配置tun0連接以使用手動IPv4配置:

# nmcli connection modify tun0 ipv4.method manual

添加將路由到172.16.0.0/24網絡的流量路由到路由器B上的隧道IP的靜態路由:

# nmcli connection modify tun0 +ipv4.routes "172.16.0.0/24 10.0.1.2"

啓用tun0連接

# nmcli connection up tun0

啓用數據包轉發

# echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/95-IPv4-forwarding.conf
# sysctl -p /etc/sysctl.d/95-IPv4-forwarding.conf

1.2 在網絡B中的路由器上:

創建一個名爲tun0的IPIP隧道接口:

# nmcli connection add type ip-tunnel ip-tunnel.mode ipip con-name tun0 ifname tun0 remote 203.0.113.10 local 198.51.100.5

remote和local參數設置遠程和本地路由器的公共IP地址。

將IPv4地址設置爲tun0設備:

# nmcli connection modify tun0 ipv4.addresses '10.0.1.2/30'

配置tun0連接以使用手動IPv4配置:

# nmcli connection modify tun0 ipv4.method manual

添加一條靜態路由,該路由將到192.0.2.0/24網絡的流量路由到路由器A上的隧道IP:

# nmcli connection modify tun0 +ipv4.routes "192.0.2.0/24 10.0.1.1"

啓用tun0連接

# nmcli connection up tun0

啓用數據包轉發:

# echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/95-IPv4-forwarding.conf
# sysctl -p /etc/sysctl.d/95-IPv4-forwarding.conf

1.3 驗證步驟

從每個路由器ping另一個路由器的內部接口的IP地址:

在路由器A上ping 172.16.0.1:

# ping 172.16.0.1

在路由器B上ping 192.0.2.1:

# ping 192.0.2.1

二、使用nmcli配置GRE隧道以將第3層流量封裝在IPv4數據包中

通用路由封裝(GRE)隧道將第3層流量封裝在RFC 2784中描述的IPv4數據包中。GRE隧道可以封裝具有有效以太網類型的任何第3層協議。

通過GRE隧道發送的數據未加密。 出於安全原因,僅將隧道用於已被其他協議(例如HTTPS)加密的數據。

下面示例在兩個路由器之間創建GRE隧道,以通過Internet連接兩個內部子網,如下圖所示:

先決條件

  • 每個路由器都有一個連接到其本地子網的網絡接口

  • 每個路由器都有一個連接到Internet的網絡接口

2.1 在網絡A中的路由器上:

1) 創建一個名爲gre1的GRE隧道接口:

# nmcli connection add type ip-tunnel ip-tunnel.mode gre con-name gre1 ifname gre1 remote 198.51.100.5 local 203.0.113.10

remote和local參數設置遠程和本地路由器的公共IP地址。

2) 將ipv4地址設置爲gre1設備:

# nmcli connection modify gre1 ipv4.addresses '10.0.1.1/30'

請注意,具有兩個可用IP地址的/30子網足以用於隧道。

3) 配置gre1連接以使用手動IPv4配置:

# nmcli connection modify gre1 ipv4.method manual

4) 添加將路由到172.16.0.0/24網絡的流量路由到路由器B上的隧道IP的靜態路由:

# nmcli connection modify tun0 +ipv4.routes "172.16.0.0/24 10.0.1.2"

5) 啓用gre1連接

# nmcli connection up gre1

6) 啓用數據包轉發:

# echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/95-IPv4-forwarding.conf
# sysctl -p /etc/sysctl.d/95-IPv4-forwarding.conf

2.2 在網絡B中的路由器上:

1) 創建一個名爲gre1的GRE隧道接口:

# nmcli connection add type ip-tunnel ip-tunnel.mode ipip con-name gre1 ifname gre1 remote 203.0.113.10 local 198.51.100.5

remote和local參數設置遠程和本地路由器的公共IP地址。

2) 將ipv4地址設置爲gre1設備:

# nmcli connection modify gre1 ipv4.addresses '10.0.1.2/30'

請注意,具有兩個可用IP地址的/ 30子網足以用於隧道。

3) 配置gre1連接以使用手動IPv4配置:

# nmcli connection modify gre1 ipv4.method manual

4) 添加將路由到192.0.2.0/24網絡的流量路由到路由器B上的隧道IP的靜態路由:

# nmcli connection modify tun0 +ipv4.routes "192.0.2.0/24 10.0.1.1"

5) 啓用gre1連接

# nmcli connection up gre1

6) 啓用數據包轉發:

# echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/95-IPv4-forwarding.conf
# sysctl -p /etc/sysctl.d/95-IPv4-forwarding.conf

2.3 驗證步驟

從每個路由器ping另一個路由器的內部接口的IP地址:

在路由器A上ping 172.16.0.1:

# ping 172.16.0.1

在路由器B上ping 192.0.2.1:

# ping 192.0.2.1

三、配置GRETAP隧道以通過IPv4傳輸以太網幀

通用路由封裝終端接入點(GRETAP)隧道在OSI級別2上運行,並將以太網流量封裝在RFC 2784中描述的IPv4數據包中。

通過GRETAP隧道發送的數據未加密。 出於安全原因,請通過VPN或其他加密連接建立隧道。

此過程描述瞭如何在兩個路由器之間創建GRETAP隧道,以使用網橋連接兩個網絡,如下圖所示:

gretap0設備名稱已保留,爲設備使用gretap1或其他名稱。

先決條件

  • 每個RHEL路由器都有一個連接到其本地網絡的網絡接口,並且該接口未分配IP配置。

  • 每個RHEL路由器都有一個連接到Internet的網絡接口。

3.1 在網絡A中的路由器上:

1) 創建一個名爲bridge0的網橋接口:

# nmcli connection add type bridge con-name bridge0 ifname bridge0

2) 配置網橋的IP設置:

# nmcli connection modify bridge0 ipv4.addresses '192.0.2.1/24'
# nmcli connection modify bridge0 ipv4.method manual

3) 爲連接到本地網絡到網橋的接口添加新的連接配置文件:

# nmcli connection add type ethernet slave-type bridge con-name bridge0-port1 ifname enp1s0 master bridge0

4) 將GRETAP隧道接口的新連接配置文件添加到網橋:

# nmcli connection add type ip-tunnel ip-tunnel.mode gretap slave-type bridge con-name bridge0-port2 ifname gretap1 remote 198.51.100.5 local 203.0.113.10 master bridge0

remote和local參數設置遠程和本地路由器的公共IP地址。

5) 可選:如果不需要,請禁用生成樹協議(STP):

# nmcli connection modify bridge0 bridge.stp no

默認情況下,STP處於啓用狀態,並且會導致延遲,然後才能使用連接。

6) 配置激活bridge0連接自動激活網橋的端口:

# nmcli connection modify bridge0 connection.autoconnect-slaves 1

7) 激活bridge0連接:

# nmcli connection up bridge0

3.2 在網絡B中的路由器上:

1) 創建一個名爲bridge0的網橋接口:

# nmcli connection add type bridge con-name bridge0 ifname bridge0

2) 配置網橋的IP設置:

# nmcli connection modify bridge0 ipv4.addresses '192.0.2.2/24'
# nmcli connection modify bridge0 ipv4.method manual

3) 爲連接到本地網絡到網橋的接口添加新的連接配置文件:

# nmcli connection add type ethernet slave-type bridge con-name bridge0-port1 ifname enp1s0 master bridge0

4) 將GRETAP隧道接口的新連接配置文件添加到網橋:

# nmcli connection add type ip-tunnel ip-tunnel.mode gretap slave-type bridge con-name bridge0-port2 ifname gretap1 remote 203.0.113.10 local 198.51.100.5 master bridge0

remote和local參數設置遠程和本地路由器的公共IP地址。

5) 可選:如果不需要,請禁用生成樹協議(STP):

# nmcli connection modify bridge0 bridge.stp no

6) 配置激活bridge0連接自動激活網橋的端口:

# nmcli connection modify bridge0 connection.autoconnect-slaves 1

7) 激活bridge0連接:

# nmcli connection up bridge0

3.3 驗證步驟

在兩個路由器上,驗證是否已連接enp1s0和gretap1連接,並且CONNECTION列顯示端口的連接名稱:

# nmcli device
nmcli device
DEVICE   TYPE      STATE      CONNECTION
...
bridge0  bridge    connected  bridge0
enp1s0   ethernet  connected  bridge0-port1
gretap1  iptunnel  connected  bridge0-port2

從每個路由器ping另一個路由器的內部接口的IP地址:

在路由器A上ping 192.0.2.2:

# ping 192.0.2.2

在路由器B上ping 192.0.2.1:

# ping 192.0.2.1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章