linux - 网络接口(interface)之间的路由 Ubuntu

标签 linux ubuntu networking routing ubuntu-16.04

我想把我的 Ubuntu 变成一个路由器。我想在 eth0 (192.168.0.0/24) 接口(interface)和 eth1 (10.0.0.0/8) 接口(interface)之间路由流量。我有以下/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 两个以太网接口(interface)。

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

最佳答案

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

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

-I interface; interface is either an address, or an interface name.

并指定不同 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

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

My goal is to turn my Ubuntu into a router which connects 10.0.0.0/8 with 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 地址。

关于linux - 网络接口(interface)之间的路由 Ubuntu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46036667/

相关文章:

c - "make"编译写在 "cc ex1.c -o ex1"文件中的代码,为什么我们使用 "exe1.c"命令

java - 服务如何检测 Windows 何时完全启动?

c++ - 数据包大小

Java网络服务器-客户端错误

linux grep/sed 某些行 - 空间删除

linux - 如果 git 仓库已经存在,我可以克隆它吗?

python - 没有名为 'openpyxl' 的模块 - Python 3.4 - Ubuntu

windows - 为 CPAN(和 CorporatePAN)打包 perl 脚本的推荐方法是什么?

linux - (bash) 如何向文件名添加一些迭代

django - Apache2 WSGI Django - 目录访问