CentOS 9 Stream的固定IP配置

HostName="hbase01"
NetworkConfPath="/etc/NetworkManager/system-connections"
NetCardName=$(ip a | grep "2: e" | grep -oE "e.*(n*).*:")
NetCardName=$(echo  ${NetCardName%*:})
NetworkConfFilePath="${NetworkConfPath}/${NetCardName}.nmconnection"
IPV4ST="192.168"
IPV4MD="121"
IPV4ED="139"
IPV4="${IPV4ST}.${IPV4MD}.${IPV4ED}"
GatwayED="2"
Gatway="${IPV4ST}.${IPV4MD}.${GatwayED}"
Address1="${IPV4}/${Gatway}"
DNS1="114.114.114.114"
Possition1="\[ipv4\]"
Label1="address1="
Label2="DNS="

sed -i "/${Possition1}/a${Label1}${Address1}" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label2}${Address1}" ${NetworkConfFilePath}
sed -i "/uuidn=/cuuidn=`uuidgen`" ${NetworkConfFilePath}

nmcli c reload
nmcli c up ens33

hostnamectl set-hostname $HostName

在hbase01上克隆两台虚拟机
vi /root/test.sh
chmod +x /root/test.sh

HostName="hbase02"
NetworkConfPath="/etc/NetworkManager/system-connections"
NetCardName=$(ip a | grep "2: e" | grep -oE "e.*(n*).*:")
NetCardName=$(echo  ${NetCardName%*:})
NetworkConfFilePath="${NetworkConfPath}/${NetCardName}.nmconnection"
IPV4ST="192.168"
IPV4MD="121"
IPV4ED="140"
IPV4="${IPV4ST}.${IPV4MD}.${IPV4ED}"
GatwayED="2"
Gatway="${IPV4ST}.${IPV4MD}.${GatwayED}"
Address1="${IPV4}/${Gatway}"
DNS1="114.114.114.114"
Possition1="\[ipv4\]"
Label1="address1="
Label2="DNS="

sed -i "/${Label1}/d" ${NetworkConfFilePath}
sed -i "/${Label2}/d" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label1}${Address1}" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label2}${DNS1}" ${NetworkConfFilePath}
sed -i "/uuidn=/cuuidn=`uuidgen`" ${NetworkConfFilePath}

nmcli c reload
nmcli c up ens33

hostnamectl set-hostname $HostName
HostName="hbase03"
NetworkConfPath="/etc/NetworkManager/system-connections"
NetCardName=$(ip a | grep "2: e" | grep -oE "e.*(n*).*:")
NetCardName=$(echo  ${NetCardName%*:})
NetworkConfFilePath="${NetworkConfPath}/${NetCardName}.nmconnection"
IPV4ST="192.168"
IPV4MD="121"
IPV4ED="141"
IPV4="${IPV4ST}.${IPV4MD}.${IPV4ED}"
GatwayED="2"
Gatway="${IPV4ST}.${IPV4MD}.${GatwayED}"
Address1="${IPV4}/${Gatway}"
DNS1="114.114.114.114"
Possition1="\[ipv4\]"
Label1="address1="
Label2="DNS="

sed -i "/${Label1}/d" ${NetworkConfFilePath}
sed -i "/${Label2}/d" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label1}${Address1}" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label2}${DNS1}" ${NetworkConfFilePath}
sed -i "/uuidn=/cuuidn=`uuidgen`" ${NetworkConfFilePath}

nmcli c reload
nmcli c up ens33

hostnamectl set-hostname $HostName
点击查看代码
HostName=""  //主机名称
NetworkConfPath="/etc/NetworkManager/system-connections"   //配置文件所在路径
NetCardName=$(ip a | grep "2: e" | grep -oE "e.*(n*).*:")  //网卡名称
NetCardName=$(echo  ${NetCardName%*:})
NetworkConfFilePath="${NetworkConfPath}/${NetCardName}.nmconnection" 
IPV4ST="192.168"    //私人IP
IPV4MD=""           //网段
IPV4ED=""           //IP
IPV4="${IPV4ST}.${IPV4MD}.${IPV4ED}"  
GatwayED="2"                          //网关
Gatway="${IPV4ST}.${IPV4MD}.${GatwayED}"
Address1="${IPV4}/${Gatway}"
DNS1="114.114.114.114"         //DNS
Possition1="\[ipv4\]"          //定位
Label1="address1="             //头部
Label2="DNS="                  

sed -i "/${Label1}/d" ${NetworkConfFilePath}
sed -i "/${Label2}/d" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label1}${Address1}" ${NetworkConfFilePath}
sed -i "/${Possition1}/a${Label2}${DNS1}" ${NetworkConfFilePath}
sed -i "/uuidn=/cuuidn=`uuidgen`" ${NetworkConfFilePath}

nmcli c reload
nmcli c up ens33

hostnamectl set-hostname $HostName
posted @ 2025-03-06 05:24  基础狗  阅读(126)  评论(0)    收藏  举报