linux-CentOS7修改默认的网卡名称为eth0
CentOS7修改默认的网卡名称为eth0
1.问题引入:
1)从CentOS/RHEL7开始,默认网卡的命名规则进行了优化升级,在新的规则下,网卡接口名称会基于固件,拓扑结构和位置信息来确定,常见的有eno16777736,ens33。
2)在添加或移除网络设备,克隆虚拟机和虚拟化相关配置中,接口名称仍然保持固定,而无需手动修改或者重新生成。
3)在大规模集群和虚拟化应用中,这样的配置显然是有很大优势的,避免了克隆虚拟机后修改网卡配置的操作,但对于个人测试或者从CentOS6继承过来的习惯问题,可能会不太舒服,可以通过以下方法进行修改
2.解决方法:
1)重命名网卡配置文件
cd /etc/sysconfig/network-scripts/
mv ifcfg-ens33 ifcfg-eth0
-------------------------------
[root@1085-CentOS75 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.85 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:9e:10:86 txqueuelen 1000 (Ethernet)
RX packets 90211 bytes 5769400 (5.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 284 bytes 54362 (53.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-------------------------------
2)修改网卡配置文件内容
# 将NAME和DEVICE改为与网卡文件相同的名称eth0,同时可以去掉UUID,HWADDR等配置参数
vim ifcfg-eth0
--------------------------
NAME=eth0
DEVICE=eth0
ONBOOT=yes
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.1.85
PREFIX=24
GATEWAY=192.168.1.1
DNS1=202.106.0.20
DNS2=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=no
IPV6_DEFROUTE=no
IPV6_PEERDNS=no
IPV6_PEERROUTES=no
IPV6_FAILURE_FATAL=no
PROXY_METHOD=none
BROWSER_ONLY=no
--------------------------
3)禁用系统内核自动命名配置
# 编辑/etc/default/grub文件,在启动时传递“net.ifnames=0 biosdevname=0”的内核参数到GRUBCMDLINELINUX变量可以实现
vim /etc/default/grub
--------------------------------
GRUB_CMDLINE_LINUX="rhgb quiet net.ifnames=0 biosdevname=0"
--------------------------------
------------- 实例演示 -------------------
[root@1085-CentOS75 network-scripts]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rhgb quiet net.ifnames=0 biosdevname=0"
GRUB_DISABLE_RECOVERY="true"
-------------------------------------------
4)更新grub配置参数
# 以下命令可以更新该内核参数
grub2-mkconfig -o /boot/grub2/grub.cfg
---------------- 实例演示 -------------------
[root@1085-CentOS75 network-scripts]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-862.11.6.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.11.6.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-862.3.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.3.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-c930c3954b614a1e90188017cb35fb80
Found initrd image: /boot/initramfs-0-rescue-c930c3954b614a1e90188017cb35fb80.img
done
-----------------------------------
5)重启检查配置
reboot
-----------------------------
[root@1085-CentOS75 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.85 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:9e:10:86 txqueuelen 1000 (Ethernet)
RX packets 60 bytes 6528 (6.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 38 bytes 7063 (6.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6)修改udev网卡规则(非必要操作)
# 根据Centos 官方WIKI的FAQ中得知,如果你有多个接口,并且想要控制其设备名,而不是让内核以它自己的方式命名,创建,/etc/udev/rules.d/XXX-net.rules规则是必要的
# 对于CentOS6网卡的命名规则是/etc/udev/rules.d/70-persistent-net.rules,修改该文件或者直接删除可以解决重启不生效的问题,
# 对于CentOS7,命名规则的文件存放在/usr/lib/udev/rules.d/目录下,可以修改60-net.rules这个文件进行配置
cd /usr/lib/udev/rules.d/
vim 60-net.rules
-------------------------------
ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", ATTR{address}=="00:0c:29:9e:10:86", RESULT=="?*", NAME="eth0"
-------------------------------
3.拓展
# 立即生效的修改网卡名称(测试了暂未生效)
ip link set dev eth0 name eth1
[root@1085-CentOS75 rules.d]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.85 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:9e:10:86 txqueuelen 1000 (Ethernet)
RX packets 2693 bytes 194701 (190.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 348 bytes 78189 (76.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@1085-CentOS75 rules.d]# ip link set dev eth0 name eth1
RTNETLINK answers: Device or resource busy
[root@1085-CentOS75 rules.d]# ip link set dev eth0 name eth1
RTNETLINK answers: Device or resource busy
作者:天生帅才 www.zuiyoujie.com
本文版权归作者和博客园共有,如果感觉有用可以随意打赏,感谢支持,欢迎转载