首页 > 代码库 > 分享两个常用的脚本,适合快速部署虚拟机

分享两个常用的脚本,适合快速部署虚拟机

虚拟机克隆完后,通常我使用这两个脚本来帮助快速部署,一个是修改主机名,一个是修改IP地址(IP地址修改比较适合只用一块网卡的虚拟机,修改eth0的IP地址)。


适合Redhat 6及CentOS 6的操作系统。

修改主机名:

#!/bin/bash

# Ivan Liao - Version 0.1

clear

# ---- prompt for new hostname, current hostname is default ----
oldName=`hostname -s`
read -p "Enter new Hostname, do not enter DNS suffix [$oldName]: " newName

# ---- confirm change ----
read -p "Change hostname to $newName? [ y or n ]: " ok

case $ok in
[Yy]*)
       echo "Start to change hostname..."
       ;;
[Nn]*)
       echo exiting without changes
     exit 1
       ;;
esac

# ---- make changes to /etc/hosts ----
# If it is a softlink, this will change it into a real file
[ ! -h /etc/hosts ] && perl -i -p -e "s/$oldName/$newName/g" /etc/hosts

# ---- make changes to /etc/sysconfig/network ----
[ ! -h /etc/hosts ] && perl -i -p -e "s/$oldName/$newName/g" /etc/sysconfig/network
[ ! -h /etc/hosts ] && perl -i -p -e "s/.localdomain//g" /etc/sysconfig/network

# ---- make changes to /etc/sysconfig/network-scripts files ----
for hostsFile in `find /etc/sysconfig/network-scripts -type f -print`
do
   perl -i -p -e "s/$oldName/$newName/g" $hostsFile
done

# ---- move or create /var/crash directory ----
if [ -d /var/crash/$oldName ]; then
   mv /var/crash/$oldName /var/crash/$newName
else
   mkdir /var/crash/$newName
   chmod 700 /var/crash/$newName
fi

# ---- finally, change hostname ----
hostname $newName

# ---- confirm changes ----
echo -n "Hostname has been changed to: "
hostname
echo ""
#echo -n "Now FQDN is: "
#hostname -f


修改IP地址:
#!/bin/bash

# Ivan Liao - Version 0.1

clear

rm -f /etc/sysconfig/network-scripts/ifcfg-eth0
rm -f /etc/sysconfig/networking/devices/ifcfg-eth0 
rm -f /etc/sysconfig/networking/profiles/default/ifcfg-eth0 

macaddress=`ifconfig -a|grep eth1|awk ‘{print $NF}‘`

[ ! -h /etc/udev/rules.d/70-persistent-net.rules ] && perl -i -p -e "s/SUBSYSTEM.*eth0"//g" /etc/udev/rules.d/70-persistent-net.rules
[ ! -h /etc/udev/rules.d/70-persistent-net.rules ] && perl -i -p -e "s/eth1/eth0/g" /etc/udev/rules.d/70-persistent-net.rules

read -p "Enter new IP address: " ipaddress

# ---- confirm change ----
read -p "Change IP address to $ipaddress? [ y or n ]: " ok

case $ok in
[Yy]*)
       echo "Start to change IP address..."
       ;;
[Nn]*)
       echo exiting without changes
     exit 1
       ;;
esac


echo "HWADDR=$macaddress" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPADDR=$ipaddress" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "PREFIX=24" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "DEFROUTE=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPV4_FAILURE_FATAL=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPV6INIT=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "NAME=eth0" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0

hn=`hostname`

echo "$ipaddress  $hn.localdomain $hn" >> /etc/hosts
echo "Please reboot system."

分享两个常用的脚本,适合快速部署虚拟机