[ 实验需求 ]

1、修改主机的网卡名称为eth0

2、将 host_ip.txt 文件中的主机配置成为指定的静态IP地址

1
2
3
4
5
6
7
8
9
10
[root@Rocky8-mini ~]# cat host_ip.txt
Centos7 192.168.119.166
Centos7-mini2 192.168.119.147
Centos7-mini3 192.168.119.158
Rocky8-mini 192.168.119.128
Rocky8-mini3 192.168.119.129
Rocky8-mini2 192.168.119.138
Rocky8-mini5 192.168.119.146
Rocky8-mini4 192.168.119.150
Centos8-mini 192.168.119.148
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@Rocky8-mini ~]# cat test.sh 
#!/bin/bash

cd /etc/sysconfig/network-scripts/

HOST=`hostname`
IP=`grep -i "\<$HOST\>" /root/host_ip.txt | cut -d' ' -f2`
version_id=`sed -nr '/VERSION_ID/s#.*"(.*)"#\1#p' /etc/os-release`
IPADDR=$IP
GATEWAY=192.168.119.2
DNS=192.168.119.2

if [ $version_id = "7" ];then
UUID=`sed -nr '/\<UUID\>/s/.*"(.*)"/\1/p' /etc/sysconfig/network-scripts/ifcfg-ens33`
elif [ $version_id = "8.5" ] || [ $version_id = "8" ];then
UUID=`awk -F'=' '/UUID/{print $2}' /etc/sysconfig/network-scripts/ifcfg-ens33`
fi

sed -Ei.bak '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1 net.ifnames=0\2/' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
TYPE=Ethernet
BOOTPROTO=static
IPADDR=$IPADDR
NETMASK=255.255.255.0
GATEWAY=$GATEWAY
DNS1=$DNS
DEFROUTE=yes
NAME=eth0
UUID=$UUID
DEVICE=eth0
ONBOOT=yes
EOF

mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak

reboot