kvm远程服务器桥接防止失联

变量(一定要改成自己的信息,别乱填,特别注意网卡名字,不带ifcfg)

IP=10.255.68.4
NETMASK=255.255.255.0
GATE=10.255.68.1
DNS=223.5.5.5
NETNAME=enp61s0f0

添加桥接网卡信息

cat > /opt/ifcfg-br0 << OEF
TYPE=Bridge
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=no
PEERROUTES=yes
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=${IP}
NETMASK=${NETMASK}
GATEWAY=${GATE}
DNS1=${DNS}
STP=on
OEF

添加虚拟桥接信息

cat > /opt/ifcfg-${NETNAME} << OEF
TYPE=Ethernet
BOOTPROTO=none
NAME=${NETNAME} 
DEVICE=${NETNAME} 
ONBOOT=yes
BRIDGE=br0
OEF

复制正常网卡信息

\cp -a /etc/sysconfig/network-scripts/ifcfg-${NETNAME} /opt/ifcfg-${NETNAME}.back

配置桥接

\cp /opt/ifcfg-br0 /etc/sysconfig/network-scripts/ifcfg-br0
\cp /opt/ifcfg-${NETNAME} /etc/sysconfig/network-scripts/ifcfg-${NETNAME}

计划任务1-重启网卡

cat > /opt/re.sh << OEF
service network restart
OEF

曲线救国,恢复正常网卡

cat > /opt/ss.sh << OEF
#!/bin/bash
#linuxsamu
# */10 * * * * root /opt/ss.sh >/tmp/sc.log 2>&1
# systemctl reload crond
backfile=/opt/ifcfg-${NETNAME}.back
sysfile=/etc/sysconfig/network-scripts/ifcfg-${NETNAME}
br0file=/etc/sysconfig/network-scripts/ifcfg-br0
backdir=/opt/
mv \${sysfile} \${backdir}ifcfg-${NETNAME}.\`date +%s\`
echo 1 >> /tmp/1.txt

\cp -a \${backfile} \${sysfile}
\mv \${br0file} \${backdir}
service network stop
ifconfig br0 down
brctl delbr br0
service network restart
OEF

添加权限以及安装依赖

chmod +x /opt/*.sh
yum install bridge-utils vim net-tools -y

添加计划任务

echo -e "*/10 * * * * root /opt/ss.sh >/tmp/sc.log 2>&1\n*/2 * * * * root /opt/re.sh >/tmp/sc.log 2>&1" >> /etc/crontab

重载计划任务并且开启"心跳加速"模式

systemctl restart crond
service network restart