如何在 linux 上更改网卡的顺序 (eth1 <-> eth0)

Ath*_*ick 21 networking debian

系统安装后有没有办法交换网络接口(eth1 <-> eth0)。

我的全新 Debian 6.0 安装分配的 PCI 网卡默认为“ eth0 ”,主板集成网络设备默认为“ eth1 ”。问题是我想使用集成设备作为默认(eth0)网络接口。

我已经编辑:

/etc/udev/rules.d/70-persistent-net.rules

交换名称,一切似乎都没有问题,网络工作正常,但程序仍在尝试使用 PCI 网卡(现在是“ eth1 ”)作为默认接口。例如iftop现在尝试使用“ eth1 ”作为默认设备,因为它在交换之前使用了“ eth0 ”。

这纯粹是一个软件问题,因为应用程序试图使用第一个找到的设备作为默认设备,尽管它们的接口命名或有没有办法通过配置操作系统来解决这个问题?


编辑:我写了一个小应用程序来打印 iflist 和 PCI 设备(eth1)出现在“ eth0 ”之前。任何想法如何交换设备顺序。


编辑:我找到了一个关于同一问题的线程,我尝试了他们建议的所有内容,但除了“虚拟地”交换名称之外,没有一个解决方案有效。

Ath*_*ick 20

我现在正在回答我自己的问题,因为我终于找到了解决此问题的方法。

我发现可以通过卸载驱动程序然后以正确的顺序加载它们来重新排序设备。

第一种方法(蛮力):

所以我想出的第一种方法很简单,就是用 init.d 脚本强制重新加载驱动程序。

以下 init 脚本是为 Debian 6.0 量身定制的,但同样的原则应该适用于几乎所有使用适当的 init.d 脚本的发行版。

#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          reorder-nics
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Reloads the nics in correct order
### END INIT INFO

#
# This script should reload the nic drivers in corrected order.
# Basically it just unloads and then loads the drivers in different order.
#

echo "Reloading NICs!"

# unload the drivers
modprobe -r driver_0        # eth0 nic interface
modprobe -r driver_1        # eth1 nic interface

# load the drivers in corrected order
modprobe driver_1
modprobe driver_0

#EOF
Run Code Online (Sandbox Code Playgroud)

然后必须将脚本添加到正确的运行级别目录。这可以在 Debian 上使用“ update-rc.d ”命令轻松完成。例如:update-rc.d reorder-nics start S


第二种方法(我认为更好):

我还发现了一种更优雅的方式(至少对于 Debian 和 Ubuntu 系统而言)。

首先确保内核不会自动加载 NIC 驱动程序。这可以通过在/etc/modprobe.d/. 我创建了一个名为“ disable-nics.conf”的文件。请注意,文件中/etc/modprobe.d/必须有.conf后缀。此外命名模块/etc/modprobe.d/blacklist.conf不影响内核自动加载模块,因此您必须制作自己的文件。

# Disable automatic loading of kernel driver modules
# Disable NIC drivers

blacklist driver_0     # eth0 by default
blacklist driver_1     # eth1 by default
Run Code Online (Sandbox Code Playgroud)

然后以 root 身份运行“ depmod -ae

用' update-initramfs -u '重新创建你的initrd

最后按更正的顺序将驱动程序名称添加到/etc/modules文件中。

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

# drivers in wanted order
driver_1    # this one should be loaded as eth0
driver_0    # this one should be loaded as eth1
Run Code Online (Sandbox Code Playgroud)

更改应在下次启动后生效。

但是不需要重新启动;使用以下命令(当然是以 root 身份)切换设备很容易:

modprobe -r driver_0; modprobe -r driver_1; modprobe driver_1; modprobe driver_0
Run Code Online (Sandbox Code Playgroud)

我在搜索解决方案时发现了一些有用的链接:

  • +1 您绝对值得为此投上一票。 (2认同)