我有一台带有两个网络接口(interface)的 PC:eth0 和 eth1。
eth0 - IP 为 192.168.11.X/24。
eth1 - IP 为 192.168.130.X/24。 eth1 有互联网连接。
当我尝试连接到互联网时,我的操作系统如何知道要使用哪个接口(interface)?它会迭代所有默认网关吗?它是否有每个接口(interface)提供的缓存? Windows 和 Linux 之间的行为有什么区别吗?
最佳答案
我将回答 Linux 方面的问题(至少对于基于 Debian 的系统,例如 Ubuntu,因为目前它对于用户来说更为常见):
在命令行中输入以下内容:
route -n
您应该会看到“路由表”出现,如下所示:
Destination Gateway Genmask ... Iface
0.0.0.0 192.168.11.254 0.0.0.0 ... eth0
169.254.0.0 0.0.0.0 255.255.0.0 ... eth0
192.168.11.0 0.0.0.0 255.255.255.0 ... eth0
192.168.130.0 0.0.0.0 255.255.255.0 ... eth1
我省略了几列,但基本上,“目的地”下显示“0.0.0.0”的行是确定您的默认路由所在位置的行。换句话说,所有流量都不会流向其他线路中的任何其他子网(google.com、facebook.com 等)。
如果不正确(如上表中,“eth1”是您想要访问互联网的卡),您应该更改默认路由:
sudo route del default
sudo route add default gw 192.168.130.254 netmask 255.255.255.0
现在就可以解决这个问题了。要使其永久化,请编辑您的接口(interface)文件:
sudo gedit /etc/network/interfaces
将其编辑为如下所示(根据您的具体情况进行必要的更改):
auto eth0
iface eth0 inet dhcp
up route del default
auto eth1
iface eth1 inet dhcp
up route add default gw 192.168.130.254 netmask 255.255.255.0
然后重新启动网络以查看是否有效:
sudo /etc/init.d/networking restart
关于sockets - 操作系统如何知道使用哪个网络接口(interface)来连接互联网?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109708/