复制生成 virtual box 虚拟机会保留原来的网卡信息,这些信息在 /etc/udev/rules.d/70-persistent-net.rules
文件中,需要删除掉原来的网卡信息,并做些修改。
这个脚本也会提示让你输入新的 IP 和 主机名,自动完整网络配置更新。
#!/bin/bash
#
# Init for virtual box replication.
#
# modify /etc/udev/rules.d/70-persistent-net.rules,
# set new IP, MAC in ifcfg-eth0
#
NET_RULES="/etc/udev/rules.d/70-persistent-net.rules"
IF_ETH0="/etc/sysconfig/network-scripts/ifcfg-eth0"
HOST_CNF="/etc/sysconfig/network"
# delete eth0 from replication
if [ $(grep '^SUBSYSTEM' $NET_RULES | wc -l) -eq 2 ]; then
sed -i '/eth0/d' $NET_RULES
# set new info: NAME='eth1' -> NAME='eth0'
sed -i 's/eth1/eth0/' $NET_RULES && echo "set eht1 -> eth0"
echo "old info deleted"
else
echo "need no delete"
fi
# get NEW_MAC
NEW_MAC=$(grep 'eth0' $NET_RULES | cut -d',' -f4 | cut -d'=' -f3)
if [ -z "$NEW_MAC" ]; then
exit 3;
fi
# set HWADDR
sed -i "s/HWADDR=.*/HWADDR=$NEW_MAC/" $IF_ETH0
# reload net drive module
modprobe -r e1000; modprobe e1000
# set new IP
read -p "please input a new IP: " NEW_IP
NETWORK=`echo $NEW_IP | cut -d'.' -f1-3`.0
GATEWAY=`echo $NEW_IP | cut -d'.' -f1-3`.1
sed -i "s/IPADDR=.*/IPADDR=$NEW_IP/" $IF_ETH0
sed -i "s/NETWORK=.*/NETWORK=$NETWORK/" $IF_ETH0
sed -i "s/GATEWAY=.*/GATEWAY=$GATEWAY/" $IF_ETH0
# set new DNS
DNS=`echo $NEW_IP | cut -d'.' -f1-3`.1
sed -i "s/nameserver.*/nameserver $DNS/" $IF_ETH0
echo "new MAC, IP is set to: "
grep -E "IPADDR|HWADDR" $IF_ETH0
echo "New DNS:"
cat /etc/resolv.conf
# set new hostname
read -p "please input a new hostname: " NEW_HOST
sed -i "s/HOSTNAME=.*/HOSTNAME=$NEW_HOST/" $HOST_CNF
echo "new hostname is set to :"
grep "HOSTNAME" $HOST_CNF
echo "hostname update will be in effective after restart"
# restart network
echo "restart network"
service network restart
# restart ?
read -p "restart now? [yes|no]" RESTART
if [ "yes" = $RESTART ]; then
shutdown -r now
else
echo "init work done, a restart is needed for new HOSTNAME in effective"
fi