【问题标题】:Route between network interfaces Ubuntu网络接口之间的路由 Ubuntu
【发布时间】:2018-02-12 16:03:30
【问题描述】:

我想把我的 Ubuntu 变成一个路由器。我想在 eth0 (192.168.0.0/24) 接口和 eth1 (10.0.0.0/8) 接口之间路由流量。我有以下 /etc/network/interfaces 配置:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.8
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 8.8.8.8 8.8.4.4
up route add -net 10.74.0.0/16 gateway 10.11.131.1 eth1

# ETH1
auto eth1
iface eth1 inet static
address 10.11.131.12
netmask 255.0.0.0
broadcast 10.255.255.255
gateway 10.11.131.1
dns-nameservers 8.8.8.8 8.8.4.4
up route add -net 8.8.0.0/16 gateway 192.168.0.1 eth0
up route add -net 10.74.0.0/16 gateway 10.11.131.1 eth1

route -n 给我以下输出:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use 
Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth1
10.74.0.0       10.11.131.1     255.255.0.0     UG    0      0        0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

cat /proc/sys/net/ipv4/ip_forward 返回 1。

我无法在 eth1 和 eth0 之间 ping:

$ ping 192.168.0.8 -I eth1
PING 192.168.0.8 (192.168.0.8) from 10.11.131.12 eth1: 56(84) bytes of data.
From 10.11.131.12 icmp_seq=1 Destination Host Unreachable

$ ping 10.11.131.12 -I eth0
PING 10.11.131.12 (10.11.131.12) from 192.168.0.8 eth0: 56(84) bytes of data.
...  no response   ...

但是,我可以从环回 ping 两个以太网接口。

$ ping 10.11.131.12 -I lo
ping: Warning: source address might be selected on device other than lo.
PING 10.11.131.12 (10.11.131.12) from 192.168.0.8 lo: 56(84) bytes of data.
64 bytes from 10.11.131.12: icmp_seq=1 ttl=64 time=0.032 ms

$ ping 192.168.0.8 -I lo
ping: Warning: source address might be selected on device other than lo.
PING 192.168.0.8 (192.168.0.8) from 192.168.0.8 lo: 56(84) bytes of data.
64 bytes from 192.168.0.8: icmp_seq=1 ttl=64 time=0.032 ms

我需要做什么才能从 eth0 ping eth1,反之亦然?

【问题讨论】:

    标签: linux ubuntu networking routing ubuntu-16.04


    【解决方案1】:

    您的观察仅基于“ping -I”命令的输出。但是,当两个目的地都在同一个盒子上时,它似乎无法与作为源提到的接口一起正常工作。

    我有完全相同的环境,Linux 主机在两个 NIC 网络之间变成路由器。尽管路由功能工作正常,但接口之间的“ping -I”也没有成功。此选项还需要一个 IP 地址:

    -I接口; interface 可以是地址,也可以是接口名称。

    并指定不同 NIC 的 IP 地址作为源 - 一切正常。我可以看到,在您的环境中,这也是正确的。当您尝试使用 Lo 作为源 ping 时,请提及其中一个 ping 仍在两个以太网卡之间运行:

    $ ping 10.11.131.12 -I lo
    ping: Warning: source address might be selected on device other than lo.
    PING 10.11.131.12 (10.11.131.12) from 192.168.0.8 lo: 56(84) bytes of data.
    64 bytes from 10.11.131.12: icmp_seq=1 ttl=64 time=0.032 ms
    

    另一个问题是您的配置。看起来你已经在不同的时间点接受了它。您在回复中提到:

    我的目标是将我的 Ubuntu 变成一个连接 10.0.0.0/8 的路由器 与 192.168.0.0/24。

    为了这个目标,你的路由表是正确的,但是 /etc/network/interfaces 文件告诉你不同。您应该稍微清理一下 - 删除重复的网关、dns 和路由条目,就像这样:

    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface
    auto eth0
    iface eth0 inet static
    address 192.168.0.8
    netmask 255.255.255.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
    dns-nameservers 8.8.8.8 8.8.4.4
    # Here you have 192.168.0.0/24 directly connected, so no need to add route
    
    # ETH1
    auto eth1
    iface eth1 inet static
    address 10.11.131.12
    netmask 255.0.0.0
    broadcast 10.255.255.255
    up route add -net 10.0.0.0/8 gateway 10.11.131.1 eth1
    

    因此,请尝试通过位于其中一个本地网络上的设备执行实时测试以到达另一个网络,当然是将其网关设置为此 Linux 主机的 IP 地址。

    【讨论】:

      【解决方案2】:

      除非您有路由器,否则您无法从一个网络连接到另一个网络。 IP 转发适用于传入的数据包,并使您的机器成为路由器。我的建议是,使用两个接口将您的机器与另一台机器背靠背连接

      机器 1 eth0(192.168.0.8) 机器 2 eth0 (192.168.0.9)

      机器 1 eth1(10.11.131.12) 机器 2 eth1 (10.11.131.13)

      在机器 2 中启用两个网络之间的 IP 转发,您可以双向 ping 网络。

      【讨论】:

      • 我的目标是将我的 Ubuntu 变成一个连接 10.0.0.0/8 和 192.168.0.0/24 的路由器。我最终可能需要配置 NAT,但我应该能够在设置 NAT 之前在接口之间 ping。
      • 在这种情况下,您的配置应该可以工作。由于您的机器设置了 IP 转发,当数据包到达时,您的机器将路由并将其发送到另一个接口,最终它会出来。但是,当您从 eth1 ping eth0 时,您的数据包不会通过您的路由器(ip 转发模块),而是来自网络。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2018-09-12
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多