Tan*_*ngo 16 networking routing ipv4 ifconfig
我有一个带有两个 NIC 的系统。这台机器和一些随附的设备将被移动并连接到不同的局域网,有时它会使用拨号上网。
eth0:
- 10.x.x.x address space
- no internet gateway
- only a few devices
eth1 (when used):
- 172.16.x.x or 192.168.x.x or other address spaces
- access to the gateway from LAN to internet
ppp0 (when used):
- internet access through dialup using KPPP
Run Code Online (Sandbox Code Playgroud)
我正在使用 ifconfig 来启动或关闭接口(除了由 KPPP 处理的 ppp0)。
如果我首先启动 eth1,它会从其 DHCP 获取地址并获取网关,然后将其添加到路由中,因此访问 LAN 和 Internet 没有问题。
如果我首先或第二次启动 eth0,它会获取其地址并将默认网关设置在其地址空间内(在 10.xxx 范围内)。如果我先打开 eth0,然后再打开 eth1,默认网关仍然保持在 10.xxx 范围内。
所以无论我做什么,eth0 都会覆盖 eth1 并在路由中“声明”网关。
有什么方法可以阻止 eth0 声明网关,或者确保 eth1(如果是第二个)使用它的网关?或者我可以以某种方式优先考虑应该使用哪个接口的网关而不是其他接口?
我基本上想确保使用 eth1 的默认地址空间网关,如果它处于活动状态,如果不是,则使用 ppp0 的默认网关。我希望能够阻止 eth0 拥有默认网关。
小智 22
我在 Raspbian 上遇到了类似的问题(我想下面的解决方案也适用于 Debian)。Raspberry Pi 3 集成了 2 个 NIC:Wi-Fi 和以太网。我两个都用,分别是wlan0和eth0。wlan0 连接到我的家庭 Wi-Fi 网络,互联网访问通过这个接口。它通过 DHCP 从我的家用路由器获取其设置。eth0 直接连接到我的 Windows PC 并分配了静态IP。由于我没有在我的 Windows PC 上配置它,因此无法通过 eth0 访问互联网。
在 Raspbian 中,dhcpcd 守护进程负责配置网络接口。为了将静态IP设置为eth0接口,在末尾添加了以下几行/etc/dhcpcd.conf:
interface eth0
static ip_address=192.168.2.2/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1
Run Code Online (Sandbox Code Playgroud)
使用此设置,dhcpcd 创建了 2 个默认路由,并且通过 eth0 的路由的优先级高于通过 wlan0 的路由:
pi@raspberrypi:~ $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.2.1 0.0.0.0 UG 202 0 0 eth0
default 192.168.1.254 0.0.0.0 UG 303 0 0 wlan0
192.168.1.0 * 255.255.255.0 U 303 0 0 wlan0
192.168.2.0 * 255.255.255.0 U 202 0 0 eth0
Run Code Online (Sandbox Code Playgroud)
所以我没有互联网访问权限,因为系统试图通过 eth0 路由它,但它没有互联网访问权限,正如我上面提到的。
为了解决这个问题,我nogateway在/etc/dhcpcd.conffor eth0 接口中使用了选项。所以特定于 eth0 的配置开始看起来像这样:
interface eth0
static ip_address=192.168.2.2/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1
nogateway
Run Code Online (Sandbox Code Playgroud)
保存此配置并重新启动后,没有通过 eth0 的默认路由:
pi@raspberrypi:~ $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.1.254 0.0.0.0 UG 303 0 0 wlan0
192.168.1.0 * 255.255.255.0 U 303 0 0 wlan0
192.168.2.0 * 255.255.255.0 U 202 0 0 eth0
Run Code Online (Sandbox Code Playgroud)
上网出现了,问题解决了。
在 RHEL6/Fedora 22 上,以下已经过测试。
在 /etc/sysconfig/network-scripts/ifcfg-eth1 中添加以下行:
DEFROUTE=no
Run Code Online (Sandbox Code Playgroud)
将 eth1 替换为不需要默认路由的接口名称。
这也可以通过网络管理器 GUI 来完成,方法是选中 IPv4 选项卡底部的“仅将此连接用于其网络上的资源”框。
DEFROUTE=no 阻止在启用接口时将默认路由(目的地 0.0.0.0 )添加到路由表中。IE。不会添加以下条目。
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.16.x.x 0.0.0.0 UG 0 0 0 eth1
Run Code Online (Sandbox Code Playgroud)
小智 5
DHCP 服务器配置错误。当它不能提供到世界其他地方的路由时,它不能发送默认网关选项。如果它确实发送了该选项,那么任何客户端都可以假定它可以将任何离线目的地的数据包发送到指定的默认网关。
因此,如果 DHCP 告诉您,您的机器就可以使用 eth0 的默认网关。解决方案是从您的 DHCP 服务器中删除错误选项。