CentOS7.4 系统安装好后,默认网卡名称为为ensXX,如ens33,如果要修改为eth0开头,之前文章描述手动更新配置步骤,此文章添加自动更新脚本:
1.创建自动化更新脚本
[root@centos7 ~]# vi centos7_changeNetName.sh
#!/bin/bash
name=$(ifconfig|grep ens|awk -F":" '{print $1}')
#echo $name
netName=ifcfg-$name
newName='eth0'
newNetName=ifcfg-$newName
####change ifcfg-xxx name
cd /etc/sysconfig/network-scripts
if [ -f $netName ];
then
echo "$netName is exist."
sed -i "s/NAME=$name/NAME=$newName/g" $netName
echo $newNetName
mv $netName $newNetName
else
echo "$netName is not exist!"
fi
#### change grub
cd /etc/default
if [ -f grub ];
then
sed -i "s/quiet/quiet net.ifnames=0 biosdevname=0/g" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
else
exit 1
fi
[root@centos7 ~]#
2.执行自动化更新脚本
[root@centos7 ~]# sh centos7_changeNetName.sh
ifcfg-ens33 is exist.
ifcfg-eth0
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-c15c0efab776454daa1c4da36ec768f6
Found initrd image: /boot/initramfs-0-rescue-c15c0efab776454daa1c4da36ec768f6.img
done
[root@centos7 ~]#
3.手动重启
因为实际环境不一定可以修改后直接重启,建议重启手动操作:
[root@centos7 ~]# reboot
4.验证网卡名
重启之后验证网卡名称:
[root@centos7 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.155.104 netmask 255.255.255.0 broadcast 192.168.155.255
inet6 fe80::df24:a2a1:5733:b35f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c4:34:bd txqueuelen 1000 (Ethernet)
RX packets 113 bytes 11255 (10.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 152 bytes 17725 (17.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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (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
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:1a:02:e7 txqueuelen 1000 (Ethernet)
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@centos7 ~]#