首页 文章

网络接口Ubuntu之间的路由

提问于
浏览
0

我想把我的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 .

我不能ping eth1和eth0:

$ 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,反之亦然?

2 回答

  • 0

    除非您有路由器,否则无法从一个网络到达另一个网络 . 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网络 .

  • 0

    您的观察结果仅基于'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地址 .

相关问题