實驗說明:
在許多自動化任務中,指令碼往往是通過讀取配置檔案來獲取資訊的,紅帽系的系統自升級之後(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 [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
-
備份系統預設網路卡配置檔案
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
-
重新命名網路卡配置檔名
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
-
修改網路卡配置檔案
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
-
修改/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
-
重新生成GRUB配置並更新核心引數
1 [root@localhost network-scripts]$ grub2-mkconfig -o /boot/grub2/grub.cfg
-
重啟
1 [root@localhost ~]$ reboot now
-
查詢網路卡資訊
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