2021-05-12 14:32:11
RHEL 7/CentOS 7/Fedora 28 重新命名網絡卡名稱[附可行性指令碼]
實驗說明:
在許多自動化任務中,指令碼往往是通過讀取組態檔來獲取資訊的,紅帽系的系統自升級之後(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}'
ifcfg-enp0s10
ifcfg-enp0s3
ifcfg-enp0s8
ifcfg-enp0s9
2.備份系統預設網絡卡組態檔
[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
3.重新命名網絡卡組態檔名
[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
4.修改網絡卡組態檔
[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
5.修改/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
6.重新生成GRUB設定並更新核心引數
[root@localhost network-scripts]$ grub2-mkconfig -o /boot/grub2/grub.cfg
7.重新啟動
[root@localhost ~]$ reboot now
8.查詢網絡卡資訊
[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)
16 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
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址:https://www.linuxidc.com/Linux/2018-08/153762.htm
相關文章