如何修改网卡名称由enp0s25为eth0 (by quqi99)

问题

今天用下列配置创建测试用的VLAN网卡,

sudo apt install -y vlan
sudo modprobe 8021q

auto enp0s25.1
iface enp0s25.1 inet static
    address 10.12.1.1/24
    netmask 255.255.255.0
    network 10.12.1.0/24
    broadcast 10.12.1.255
    mtu 1492

sudo ifup enp0s25.1

但是失败,报下列错。

Dec  6 08:54:50 localhost ifup[29785]: Cannot find device "enp0s25.1"
Dec  6 08:54:50 localhost ifup[29785]: Failed to bring up enp0s25.1.

原因是需要将enp0s25改为eth0。在谷歌上搜索一百篇文章起码有九十九篇都说要修改70-persistent-net.rules文件,但是照着改了就是不成功,那是怎么一回事呢?

enp0s25代表什么

根据systemd中udev的代码(https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L20 ), enp0s25 意为: en:ethernet, p0:bus_num=0, s25:slot_num=25

root@t440p:~# lspci |grep Ethernet
00:19.0 Ethernet controller: Intel Corporation Ethernet Connection I217-LM (rev 04) 

root@t440p:~# lspci -n -s 00:19.0
00:19.0 0200: 8086:153a (rev 04)

root@t440p:~# find /sys/devices -type d -wholename '/sys/devices/pci*/net' -print
/sys/devices/pci0000:00/0000:00:19.0/net
/sys/devices/pci0000:00/0000:00:1c.1/0000:04:00.0/net
/sys/devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.0/net

hua@t440p:~$ sudo udevadm info /sys/class/net/enp0s25 |grep DEVPATH
E: DEVPATH=/devices/pci0000:00/0000:00:19.0/net/enp0s25

方法一,修改70-persistent-net.rules

修改/etc/udev/rules.d/70-persistent-net.rules文件如下内容(注:ubuntu 16.04上由systemd.link管理默认无此文件,直接添加即可,并将如下ATTR{address}改为网卡的MAC地址,其他不变)

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="28:d2:44:52:31:1d", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

但这样修改后并不生效,原因是动了udev的文件需要下列命令更新initrd.

sudo update-initramfs -u

将上面配置改为下面的内容也行:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNELS=="0000:00:19.0", KERNEL=="eth*", NAME="eth0"

该配置可直接运行这段脚本生成:

scansys() {
    find /sys/devices -type d -wholename '/sys/devices/pci*/net' -print
}
extract() {
    awk -F/ '{printf "%s eth%u\n",$(NF-1),NR-1;}'
}
generate() {
    echo '# these rules are for persistence based on bus device address'
    awk '{printf "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNELS==\"%s\", KERNEL==\"eth*\", NAME=\"%s\"\n",$1,$2;}'
}
scansys | extract | sort | generate

一般地,上面方法就会成功,至少我成功了,如果你重启机器后还不成功,可以考虑如下方法去Disable the Predictable Network Interface Names,见:https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/

mv /lib/udev/rules.d/80-net-setup-link.rules /bak/bin/
ln -s /dev/null /lib/udev/rules.d/80-net-setup-link.rules  #Disable the Predictable Network Interface Names

修改文件/etc/systemd/network/10-internet.link添加如下内容后重启机器即可:

[Match]
MACAddress=28:d2:44:52:31:1d
[Link]
Name=eth0

但参考该网页 (http://manpages.ubuntu.com/manpages/zesty/man5/systemd.link.5.html)做如下配置却不成功:

[Match]
Path=pci-0000:0000:19.0-*
[Link]
Name=eth0

方法三,修改grub的方式

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash net.ifnames=0"
sudo update-grub
本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院