9.2. 使用 nmcli 配置 GRE 隧道来封装 IPv4 数据包中的层 3 流量

Generic Routing Encapsulation(GRE)隧道封装 IPv4 数据包中的第 3 层流量,如 RFC 2784 所述。GRE 隧道可以使用有效的以太网类型封装任何第 3 层协议。

重要

通过 GRE 隧道发送的数据没有加密。出于安全考虑,只在已经加密的数据中使用隧道,比如 HTTPS。

例如,您可以在两个 RHEL 路由器之间创建一个 GRE 隧道来通过互联网连接两个内部子网,如下图所示:

GRE 隧道
注意

保留 gre0 设备名称。对该设备使用 gre1 或者不同的名称。

先决条件

  • 每个 RHEL 路由器都有一个网络接口,它连接到其本地子网。
  • 每个 RHEL 路由器都有一个连接到互联网的网络接口。

流程

  1. 在网络 A 的 RHEL 路由器上:

    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

      remotelocal 参数设置远程和本地路由器的公共 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 gre1 +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. 在网络 B 中的 RHEL 路由器中:

    1. 创建名为 gre1 的 GRE 隧道接口:

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

      remotelocal 参数设置远程和本地路由器的公共 IP 地址。

    2. 将 IPv4 地址设为 gre1 设备:

      # nmcli connection modify gre1 ipv4.addresses '10.0.1.2/30'
    3. gre1 连接配置为使用手动 IPv4 配置:

      # nmcli connection modify gre1 ipv4.method manual
    4. 添加一个静态路由,其将路由到 192.0.2.0/24 网络的流量路由到路由器 A 上的隧道 IP:

      # nmcli connection modify gre1 +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

验证

  1. 从每个 RHEL 路由器中,ping 路由器的内部接口的 IP 地址:

    1. 在路由器 A 上,ping 172.16.0.1

      # ping 172.16.0.1
    2. 在路由器 B 上,ping 192.0.2.1

      # ping 192.0.2.1

其他资源

  • nmcli(1) 手册页
  • nm-settings(5) 手册页