[提供可行性脚本] RHEL 7/CentOS 7/Fedora28 重命名网卡名称

时间:2021-08-21 20:32:22

实验说明:

在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(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

实验步骤:

  1. 查看当前网卡设备数目及名称

    1 [root@localhost ~]$ ls -l /etc/sysconfig/network-scripts | awk  '/ifcfg-e/ {print $9}'
    2 ifcfg-enp0s10
    3 ifcfg-enp0s3
    4 ifcfg-enp0s8
    5 ifcfg-enp0s9
  2. 备份系统默认网卡配置文件

    1 [root@localhost ~]$ cd /etc/sysconfig/network-scripts/
    2 [root@localhost network-scripts]$ nic_list=`ls -l /etc/sysconfig/network-scripts|awk  '/ifcfg-e/ {print $9}'`
    3 [root@localhost network-scripts]$ for nic_name in $nic_list; do 
    4  > cp $nic_name $nic_name.bak; done
  3. 重命名网卡配置文件名

    1 [root@localhost network-scripts]$ i=0 
    2 [root@localhost network-scripts]$ nic_list=`ls -l /etc/sysconfig/network-scripts|awk  '/ifcfg-e/ {print $9}'`
    3 [root@localhost network-scripts]$ for nic_name in $nic_list; do  
    4 > mv $nic_name ifcfg-eth${i}; 
    5 > i=$(expr $i + 1); 
    6 > done
  4. 修改网卡配置文件

    1 [root@localhost network-scripts]$ for new_nic_name in $new_nic_list; do 
    2 > name=$(echo $new_nic_name|cut -b 7-);
    3 > sed -i "s/NAME=.*/NAME=$name/g" $new_nic_name;
    4 > sed -i "s/DEVICE=.*/DEVICE=$name/g" $new_nic_name;
    5 > sed -i 's/ONBOOT=no/ONBOOT=yes/g' $new_nic_name
    6 > done
  5. 修改/etc/default/grub文件,禁用biosdevname程序

    1 [root@localhost network-scripts]$ sed -i '/GRUB_CMDLINE_LINUX=/d' /etc/default/grub
    2 [root@localhost network-scripts]$ echo 'GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root \
    3 > rd.lvm.lv=centos/swap crashkernel=auto \
    4 > net.ifnames=0 biosdevname=0 rhgb quiet"' \
    5 > >> /etc/default/grub
  6. 重新生成GRUB配置并更新内核参数

    1 [root@localhost network-scripts]$ grub2-mkconfig -o /boot/grub2/grub.cfg
  7. 重启

    1 [root@localhost ~]$ reboot now
  8. 查询网卡信息

    1 [root@localhost ~]$ ip a | grep eth[0-9]
    2 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    3     inet 192.168.56.114/24 brd 192.168.56.255 scope global dynamic eth0
    4 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    5     inet 192.168.56.115/24 brd 192.168.56.255 scope global dynamic eth1
    6 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    7     inet 192.168.56.116/24 brd 192.168.56.255 scope global dynamic eth2
    8 5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    9     inet 192.168.56.117/24 brd 192.168.56.255 scope global dynamic eth3

PS:CentOS7 修改默认网卡名称脚本 (change_default_nic_name.sh),直接拷贝就可使用。

 1 #!/usr/bin/bash
 2 #this script will change default ifcfg-enoxxxx/ifcfg-enpxxxx to ifcfg-ethx 
 3 
 4 ls -l /etc/sysconfig/network-scripts|awk  '/ifcfg-eno[0-9]*/ {print $9}' > default_nic_name.txt
 5 i=0
 6 cat default_nic_name.txt| while read line
 7 do
 8     cd /etc/sysconfig/network-scripts
 9     name=$(echo $line|cut -b 7-)
10     cp $line ${line}.bak
11     sed -i "s/$name/eth${i}/g" $line
12     sed -i 's/ONBOOT=no/ONBOOT=yes/g' $line
13     mv $line ifcfg-eth${i}
14     i=$(expr $i + 1)
15
16 done
17 
18 sed -i '/GRUB_CMDLINE_LINUX=/d' /etc/default/grub
19 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 
20 grub2-mkconfig -o /boot/grub2/grub.cfg
21 reboot