每天一个新知识之CentOS7更改默认网卡名称为eth0

在CentOS7中,默认的网卡名称变成了ens这种命名方式,下面介绍下如何将其修改为我们习惯使用的eth的命名方式。
1、修改网卡配置文件名

cp -a /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/bak_ifcfg-ens32
mv /etc/sysconfig/network-scripts/ifcfg-ens32 /etc/sysconfig/network-scripts/ifcfg-eth0

2、修改网卡配置文件ifcfg-eth0

[root@centos195 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=eth0			#将ens32修改为eth0
DEVICE=eth0			#将ens32修改为eth0
ONBOOT=yes
IPADDR=10.10.19.6
PREFIX=24
GATEWAY=10.10.19.253
DNS1=223.5.5.5
DNS2=223.6.6.6
IPV6_PRIVACY=no

3、修改grub配置文件

[root@centos195 ~]# 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="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto rhgb quiet net.ifnames=0 biosdevname=0"
#GRUB_CMDLINE_LINUX后新增参数“net.ifnames=0 biosdevname=0”关闭一致性命名规则

[root@centos195 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg 
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-ff192b4d3bf44d0e957a52a4880cd156
Found initrd image: /boot/initramfs-0-rescue-ff192b4d3bf44d0e957a52a4880cd156.img
done
#更新grub配置文件并加载新参数
[root@centos195 ~]# shutdown -r now
#重启操作系统

4、使用shell脚本更改网卡名称

[root@centos195 ~]# cat set_eth.sh 
#!/bin/bash
enname=`ls /etc/sysconfig/network-scripts/ | grep ifcfg-en | awk -F "-" {'print $2'}`
cp /etc/sysconfig/network-scripts/ifcfg-$enname /etc/sysconfig/network-scripts/networkcard.bak
oldname=NAME\=$enname
newname=NAME\=eth0
sed -i "s/$oldname/$newname/" /etc/sysconfig/network-scripts/ifcfg-$enname
olddname=DEVICE\=$enname
newdname=DEVICE\=eth0
sed -i "s/$olddname/$newdname/" /etc/sysconfig/network-scripts/ifcfg-$enname
mv /etc/sysconfig/network-scripts/ifcfg-$enname /etc/sysconfig/network-scripts/ifcfg-eth0
cp -r /etc/default/grub /etc/default/grub.bak
strb=\GRUB_CMDLINE_LINUX\=\"rd.lvm.lv\=centos\/root\ rd.lvm.lv\=centos\/swap\ crashkernel\=auto\ rhgb\ quiet\ net.ifnames\=0\ biosdevname\=0\"
sed -i 's/^.*GRUB_CMDLINE_LINUX/\#&/g'  /etc/default/grub
echo $strb >> /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg

你可能感兴趣的:(Linux)