虚拟机克隆完成后自动配置静态IP地址

版权声明:silly8543 https://blog.csdn.net/cen50958/article/details/89817571

自动配置静态IP脚本,需要修改脚本中对应的hostname,ip地址相关信息

#虚拟机克隆自动配置静态IP脚本文件
#!/etc/bash

#hostname
hostname=test
#静态IP
ipaddr=192.168.47.22
#子网掩码
netmask=255.255.255.0
#网关地址
gateway=192.168.47.2
#网卡配置文件地址
fileEth=/etc/sysconfig/network-scripts/ifcfg-eth0 
#hostname配置文件地址
fileNetwork=/etc/sysconfig/network

#删除UUID,HWADDR,ONBOOT,BOOTPROTO,IPADDR,NETMASK,GATEWAY 配置列
sed -i "/UUID/d" $fileEth
sed -i "/HWADDR/d" $fileEth
sed -i "/ONBOOT/d" $fileEth
sed -i "/BOOTPROTO/d" $fileEth
sed -i "/IPADDR/d" $fileEth
sed -i "/NETMASK/d" $fileEth
sed -i "/GATEWAY/d" $fileEth

#添加IP配置项
echo "IPADDR=$ipaddr" >>$fileEth
echo "NETMASK=$netmask" >>$fileEth
echo "GATEWAY=$gateway" >>$fileEth
echo "ONBOOT=yes" >>$fileEth
echo "BOOTPROTO=static" >>$fileEth

#删除rules文件
rm -f /etc/udev/rules.d/70-persistent-net.rules

#配置hostname
sed -i "/HOSTNAME/d" $fileNetwork
echo "HOSTNAME=$hostname">>$fileNetwork

#重启
reboot

猜你喜欢

转载自blog.csdn.net/cen50958/article/details/89817571