[Linux系统] 如何修改CentOS7网卡名

时间:2023-03-09 20:20:13
[Linux系统] 如何修改CentOS7网卡名

一、关闭一致性网络设备命名法

cat /etc/sysconfig/grub

GRUB_TIMEOUT=
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

在GRUB_CMDLINE_LINUX中添加net.ifnames=0 biosdevname=0,文件内容变为如下:

cat /etc/sysconfig/grub

GRUB_TIMEOUT=
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

更新Grub内核配置:

grub2-mkconfig -o /boot/grub2/grub.cfg

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.-.el7.x86_64
Found initrd image: /boot/initramfs-3.10.-.el7.x86_64.img
Found linux image: /boot/vmlinuz--rescue-f034e6e19df748b7a27fad77b06e3179
Found initrd image: /boot/initramfs--rescue-f034e6e19df748b7a27fad77b06e3179.img
done

二、修改/etc/udev/rules.d/90-eno-fix.rules文件

如果文件不存在,则新建。

内容如下:

# This file was automatically generated on systemd update
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:3e:89:a4", NAME="eno-16777xxx"

将Name修改为eth0:

# This file was automatically generated on systemd update
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:3e:89:a4", NAME="eth0"

如果有多个网卡,则会存在多条记录:

# This file was automatically generated on systemd update
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:3e:89:a4", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:3e:89:a6", NAME="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:3e:89:a8", NAME="eth2"

三、重启计算机

reboot

四、修改配置文件

将原本网卡的配置文件修改为新网卡名:

mv /etc/sysconfig/network-scripts/ifcfg-eno16777xxx /etc/sysconfig/network-scripts/ifcfg-eth0

然后修改配置文件:

TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth0"
#UUID="c1f4ef5b-92fb-4037-b8e5-2a252e57e32c"
DEVICE="eth0"
ONBOOT="yes"
IPADDR="192.168.1.180"
PREFIX=""
GATEWAY="192.168.1.1"
DNS1="61.139.2.69"

五、关闭NetworkManager服务

systemctl stop NetworkManager
systemctl disable NetworkManager

六、重启网络服务

systemctl restart network

七、查看网络是否修改成功

[root@centos-base network-scripts]# ifconfig
eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.1.180 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe31: prefixlen scopeid 0x20<link>
inet6 240e::c0:ddc0:20c:29ff:fe31: prefixlen scopeid 0x0<global>
ether :0c:::: txqueuelen (Ethernet)
RX packets bytes (71.5 KiB)
RX errors dropped overruns frame
TX packets bytes (43.4 KiB)
TX errors dropped overruns carrier collisions