实验说明:
在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各不相同。为了统一网卡名称,方便配置文件的书写,不得不修改网卡名称。
实验环境:
宿主机系统 :Fedora 28 WorkStation
虚拟机管理器 :Virtual-Box 5.2.16
虚拟机配置 :系统:CentOS 7.2 1511 (minimal)
网络:enp0s3 Host-Only 192.168.56.0/24
enp0s8 Host-Only 192.168.56.0/24
enp0s9 Host-Only 192.168.56.0/24
enp0s10 Host-Only 192.168.56.0/24
实验步骤:
-
查看当前网卡设备数目及名称
[root@localhost ~]$ ls -l /etc/sysconfig/network-scripts | awk '/ifcfg-e/ {print $9}'
ifcfg-enp0s10
ifcfg-enp0s3
ifcfg-enp0s8
ifcfg-enp0s9 -
备份系统默认网卡配置文件
[root@localhost ~]$ cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]$ nic_list=`ls -l /etc/sysconfig/network-scripts|awk '/ifcfg-e/ {print $9}'`
[root@localhost network-scripts]$ for nic_name in $nic_list; do
> cp $nic_name $nic_name.bak; done -
重命名网卡配置文件名
[root@localhost network-scripts]$ i=0
[root@localhost network-scripts]$ nic_list=`ls -l /etc/sysconfig/network-scripts|awk '/ifcfg-e/ {print $9}'`
[root@localhost network-scripts]$ for nic_name in $nic_list; do
> mv $nic_name ifcfg-eth${i};
> i=$(expr $i + 1);
> done -
修改网卡配置文件
[root@localhost network-scripts]$ for new_nic_name in $new_nic_list; do
> name=$(echo $new_nic_name|cut -b 7-);
> sed -i "s/NAME=.*/NAME=$name/g" $new_nic_name;
> sed -i "s/DEVICE=.*/DEVICE=$name/g" $new_nic_name;
> sed -i 's/ONBOOT=no/ONBOOT=yes/g' $new_nic_name
> done -
修改/etc/default/grub文件,禁用biosdevname程序
[root@localhost network-scripts]$ sed -i '/GRUB_CMDLINE_LINUX=/d' /etc/default/grub
[root@localhost network-scripts]$ echo 'GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root \
> rd.lvm.lv=centos/swap crashkernel=auto \
> net.ifnames=0 biosdevname=0 rhgb quiet"' \
> >> /etc/default/grub -
重新生成GRUB配置并更新内核参数
[root@localhost network-scripts]$ grub2-mkconfig -o /boot/grub2/grub.cfg
-
重启
[root@localhost ~]$ reboot now
-
查询网卡信息
[root@localhost ~]$ ip a | grep eth[0-9]
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.56.114/24 brd 192.168.56.255 scope global dynamic eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.56.115/24 brd 192.168.56.255 scope global dynamic eth1
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.56.116/24 brd 192.168.56.255 scope global dynamic eth2
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.56.117/24 brd 192.168.56.255 scope global dynamic eth3
PS:CentOS7 修改默认网卡名称脚本 (change_default_nic_name.sh),直接拷贝就可使用。
#!/usr/bin/bash
#this script will change default ifcfg-enoxxxx/ifcfg-enpxxxx to ifcfg-ethx ls -l /etc/sysconfig/network-scripts|awk '/ifcfg-eno[0-9]*/ {print $9}' > default_nic_name.txt
i=0
cat default_nic_name.txt| while read line
do
cd /etc/sysconfig/network-scripts
name=$(echo $line|cut -b 7-)
cp $line ${line}.bak
sed -i "s/$name/eth${i}/g" $line
sed -i 's/ONBOOT=no/ONBOOT=yes/g' $line
mv $line ifcfg-eth${i}
i=$(expr $i + 1) done sed -i '/GRUB_CMDLINE_LINUX=/d' /etc/default/grub
echo 'GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto net.ifnames=0 biosdevname=0 rhgb quiet"' >> /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot