码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-07 14:45:35      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:os   io   使用   ar   for   art   sp   on   c   

虚拟机克隆完后,通常我使用这两个脚本来帮助快速部署,一个是修改主机名,一个是修改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."

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

标签:os   io   使用   ar   for   art   sp   on   c   

原文地址:http://www.cnblogs.com/scwanglijun/p/3960385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!