nmcli 一键设置网桥为 br0 脚本

#!/bin/bash

BRIDGE="br0"
# ip -o route get to 8.8.8.8|awk  '{ print "gateway:" $3 " netname:" $5"  IP:" $7}'  
NET_IFACE=$(ip -o route get to 8.8.8.8|awk '{ print $5}')  #根据外网 ip 来判断网卡方法
#NET_IFACE=$(nmcli -t -f DEVICE,STATE device | grep ':connected' | head -n 1 | cut -d: -f1)   #从第一个网卡的方式获取网卡名 

#mv  /etc/sysconfig/network-scripts/ifcfg-$NET_IFACE  /etc/sysconfig/network-scripts/ifcfg-$NET_IFACE.bak
IP4_ADDRESS=$(nmcli device show "$NET_IFACE" |grep "IP4.ADDRESS\[1\]" | awk '{ print $2}')
IP4_GATEWAY=$(nmcli -g IP4.GATEWAY device show "$NET_IFACE")
IP6_ADDRESS=$(nmcli device show "$NET_IFACE" |grep "IP6.ADDRESS\[1\]" | awk '{ print $2}')
IP6_GATEWAY=$(nmcli -g IP6.GATEWAY device show "$NET_IFACE"|sed 's/\\//g')

nmcli conn add type bridge con-name "$BRIDGE" ifname "$BRIDGE"

if [ $IP4_ADDRESS ]  ; then
nmcli conn mod "$BRIDGE" ipv4.addresses $IP4_ADDRESS
nmcli conn mod "$BRIDGE" ipv4.gateway $IP4_GATEWAY
nmcli conn mod "$BRIDGE" ipv4.dns '8.8.8.8,1.1.1.1,1.2.4.8'
nmcli conn mod "$BRIDGE" ipv4.method manual
else
nmcli conn mod "$BRIDGE" ipv4.method auto
fi
if [ $IP6_ADDRESS ]  ; then
nmcli conn mod "$BRIDGE" ipv6.addresses $IP6_ADDRESS
nmcli conn mod "$BRIDGE" ipv6.gateway $IP6_GATEWAY
nmcli conn mod "$BRIDGE" ipv6.method manual
else
nmcli conn mod "$BRIDGE" ipv6.method auto
fi
nmcli connection modify "$BRIDGE" stp no
nmcli connection delete $NET_IFACE
nmcli conn add type ethernet slave-type bridge con-name "$NET_IFACE"-to-"$BRIDGE" ifname "$NET_IFACE" master "$BRIDGE"
systemctl restart NetworkManager

 

设置完成后网桥为 br0

原本的网卡名会变成类似eth0-to-br0

最好开着 ipmi 玩, 掉线了会比较蛋疼

添加新评论 »