Linux_Centos 配置网桥的方法

 

配置yum

cat > /etc/yum.repos.d/dvd.repo << EOF

[dvd]

name=dvd

gpgcheck=0

enabled=1

baseurl=file:///mnt/iso/

EOF

 

 

yum clean all && yum repolist && yum makecache && yum -y install bash-com* 
yum -y install bridge-utils

 

临时配置bridge-br0,会断网,请勿拿IDC机房设备进行试验此种方法 

请准备好备用脚本

例如ping外部网络多久不通

去执行另外一个脚本"ifconfig down interface,brctl delbr br0,ifconfig 192.168.1.111/24 eth0"或设定重启去恢复此故障。
 

  brctl addbr br0

  ip addr add 192.168.1.111/24 dev br0

  ip addr del 192.168.1.111/24 dev eth0

  brctl addif br0 eth0

  ifconfig br0 up 

  systemctl restart network

 

linux网桥永久配置脚本

#!/bin/bash

#Centos 7 or Redhat 7 Create br0

#此脚本用于创建br0桥接。

#环境变量部分需注意,根据自己实际需要去修改ip、dns、gw、netmask。

#设置环境变量
interface_Name=eth0
bridge_Name=br0
bridge_ip="192.168.1.200"
bridge_Nm="255.255.255.0"
bridge_Gw="192.168.1.1"
dns1="114.114.114.114"
dns2="8.8.8.8"

#设置桥接配置
function createNetworkDevBr0 () {
cd /etc/sysconfig/network-scripts/ && cp ifcfg-$interface_Name ifcfg-${bridge_Name}
cat > ifcfg-${bridge_Name} < DEVICE=br0  
TYPE=Bridge
BOOTRPOTO=static
IPADDR=${bridge_ip}
NETMASK=${bridge_Nm}
GATEWAY=${bridge_Gw}
DNS1=${dns1}
DNS2=${dns2}
ONBOOT=yes
EOF
}

#修改本地接口配置
function modifyNetDevConf () {
sed -i '/IPADDR\|GATEWAY\|DNS1\|DNS2\|PREFIX\|NETMASK/d' /etc/sysconfig/network-scripts/ifcfg-${interface_Name}  
sed -n '$p' /etc/sysconfig/network-scripts/ifcfg-${bridge_Name} | egrep "\b${bridge_Name}\b"
if [ $? -eq 0 ];then
echo "BridgeConfig in ifcfg-${bridge_Name}"
exit
else
echo "Add BridgeConfig into ifcfg-${bridge_Name}" 
sed -i '$a BRIDGE=br0' /etc/sysconfig/network-scripts/ifcfg-eth0 
fi
}
echo "U HAVE 3 TIMES TO INPUT YOUR CHOICES"

i=1
while [ $i -le 3 ]
do
read -p "Now, U have 3 time to input a number or other somethins>>>___<<<: " -t 30 bridgeNums
case $bridgeNums in
1)
createNetworkDevBr0
modifyNetDevConf 
systemctl restart network
;;

2)
echo "WAIT A MOMENT TO INSTALL"
echo "Will Modify Variables,like IPADDR=??"
exit 2
;;
*)
echo "Ploease input 1 | 2 ,not else!!!"
let i++
continue
;;
esac
let i++
done

你可能感兴趣的:(shell)