为了测试集群,在安装了初始虚拟机之后,我采用虚拟机clone方式来部署虚拟服务器。
但是,虚拟机clone之后,发现其中的新虚拟网卡被系统识别为eth3
eth4
和 eth5
,而不是预期的eth0
eth1
和 eth2
。这对管理服务器配置带来一些麻烦。所以还是查询了一些资料,解决了这个问题,记录如下。
参考
- LogicalInterfaceNames 提供了多种设置网卡逻辑名称的方法
说明
现代的Linux操作系统都可以采用udev方式来对加入系统的硬件设备进行命名,这也是推荐的方法。udev根据设置的规则可以确保加入系统的硬件设备命名一致,不会因为系统重启而导致硬件设备识别为其他名称。这对于类似集群系统对共享存储的识别尤为重要,否则会导致集群运行不正常。
设置udev规则配置文件对网卡命名
可以采用如下方法设置网卡:
- 在
/etc/udev/rules.d
添加10_netinterfaces.rules
配置文件
1 2 3 | KERNEL=="eth?", SYSFS{address}=="00:16:36:18:14:51", NAME="eth0" KERNEL=="eth?", SYSFS{address}=="00:16:36:84:02:29", NAME="eth1" KERNEL=="eth?", SYSFS{address}=="00:16:36:63:ef:de", NAME="eth2" |
- 重启系统后系统将根据MAC地址来识别设备命名。
实际的解决方法
以上方法虽然可行,但是在重启系统后,仍然发现网卡被识别为 eth3
eth4
和 eth5
。并且/var/log/messages
中有如下信息
Sep 11 02:28:33 glusterfs-1 kernel: udev: renamed network interface eth0 to eth3Sep 11 02:28:33 glusterfs-1 kernel: udev: renamed network interface eth1 to eth4Sep 11 02:28:33 glusterfs-1 kernel: udev: renamed network interface eth2 to eth5
这说明启动时系统是识别这三块网卡为 eth0
eth1
和 eth2
,但是随后被udev规则设置所改变。
仔细检查了 /etc/udev/rules.d
目录下,发现还有一个 70-persistent-net.rules
规则文件,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # This file was automatically generated by the /lib/udev/write_net_rules # program, run by the persistent-net-generator.rules rules file. # # You can modify it, as long as you keep each rule on a single # line, and change only the value of the NAME= key. # PCI device 0x10ec:0x8139 (8139cp) (custom name provided by external tool) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:db:6b:b0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" # PCI device 0x10ec:0x8139 (8139cp) (custom name provided by external tool) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:32:ed:8d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1" # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:eb:f5:b9", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2" # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:16:36:18:14:51", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3" # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:16:36:84:02:29", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth4" # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:16:36:63:ef:de", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth5" |
这个配置文件是 /lib/udev/write_net_rules
脚本自动生成的,是系统识别新硬件时候添加的配置文件。这样可以确保每个添加的硬件(内核驱动相同)不会判别为相同命名。
所以,在这种情况下,应该修改 70-persistent-net.rules
,去除不需要的配置,并修改为合适的命名。
1 2 3 4 5 6 7 8 | # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:16:36:18:14:51", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:16:36:84:02:29", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1" # PCI device 0x10ec:0x8139 (8139cp) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:16:36:63:ef:de", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2" |
当然,移除这个70-persistent-net.rules
,改为采用前述的 10_netinterfaces.rules
规则配置文件也能起到同样作用。