在linux上添加网桥、删除网桥的操作

1. 配置网桥

首先修改网卡配置文件

[root@virt-server ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-xenbr0
[root@virt-server ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@virt-server ~]# vim /etc/sysconfig/network-scripts/ifcfg-xenbr0
[root@virt-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens33
DEVICE=ens33
ONBOOT=yes
#IPADDR=192.168.245.131
#GATEWAY=192.168.245.2
PEERDNS=no
#PREFIX=24
BRIDGE=xenbr0
[root@virt-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-xenbr0
TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=xenbr0
DEVICE=xenbr0
ONBOOT=yes

IPADDR=192.168.245.131
GATEWAY=192.168.245.2
PEERDNS=no
PREFIX=24
[root@virt-server ~]# systemctl restart network
[root@virt-server ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet6 fe80::20c:29ff:fe28:bf21  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 2503  bytes 231340 (225.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1599  bytes 178134 (173.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 22  bytes 2294 (2.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 2294 (2.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

xenbr0: flags=4163  mtu 1500
        inet 192.168.245.131  netmask 255.255.255.0  broadcast 192.168.245.255
        inet6 fe80::20c:29ff:fe28:bf21  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 22  bytes 1282 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30  bytes 2928 (2.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

到这里网桥就配置好了,我们可以用brctl命令查看一下

[root@virt-server ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
xenbr0		8000.000c2928bf21	no		ens33

2. 恢复原状(删掉网桥)

首先恢复网卡配置文件

[root@virt-server ~]# mv /etc/sysconfig/network-scripts/ifcfg-xenbr0 /etc/sysconfig/network-scripts/ifcfg-ens33
mv: overwrite ‘/etc/sysconfig/network-scripts/ifcfg-ens33’? y
[root@virt-server ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@virt-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens33
DEVICE=ens33
ONBOOT=yes

IPADDR=192.168.245.131
GATEWAY=192.168.245.2
PEERDNS=no
PREFIX=24

现在虽然修改了网卡配置文件,但是网络没有重启,所以现在还一切正常。

接下来需要把xenbr0上的地址拆掉,即设置ifconfig xenbr0 0
拆掉之后,要把网桥上面的网卡删掉(即把xenbro0上搭的物理桥拆掉,也就是不让它搭了),即brctl delif xenbr0 ens33
然后把网桥关掉(因为如果它的状态是UP的话是没办法删掉的),即ip link set xenbr0 down
之后把网卡设备删掉,即brctl delbr xenbr0
然后需要把网络重启一下,即systemctl restart network

[root@virt-server ~]# ifconfig xenbr0 0;brctl delif xenbr0 ens33;ip link set xenbr0 down;brctl delbr xenbr0;systemctl restart network
[root@virt-server ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.245.131  netmask 255.255.255.0  broadcast 192.168.245.255
        inet6 fe80::20c:29ff:fe28:bf21  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 4465  bytes 417732 (407.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2510  bytes 272640 (266.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 22  bytes 2294 (2.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 2294 (2.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3. 用命令行创建网桥(不修改配置文件,且重启后恢复原状)

首先添加网桥,然后将其状态设为UP状态。然后把ens33的IP地址拆掉,并把该IP地址加到网桥上面,并把网桥状态设为UP。然后需要在虚拟网桥上,把我们的物理网卡桥接上去

# 首先添加网桥
[root@virt-server ~]# brctl addbr xenbr0
[root@virt-server ~]# ifconfig -a
ens33: flags=4163  mtu 1500
        inet 192.168.245.131  netmask 255.255.255.0  broadcast 192.168.245.255
        inet6 fe80::20c:29ff:fe28:bf21  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 5281  bytes 488974 (477.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3131  bytes 321224 (313.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 22  bytes 2294 (2.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 2294 (2.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

xenbr0: flags=4098  mtu 1500
        ether ca:63:ce:3d:b9:81  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 然后将其状态设为UP状态,可看到其状态显示DOWN
# 后来set up后可能是因为没有配置完成,所以显示UNKNOWN状态
[root@virt-server ~]# ip link show xenbr0
5: xenbr0:  mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ca:63:ce:3d:b9:81 brd ff:ff:ff:ff:ff:ff
[root@virt-server ~]# ip link set xenbr0 up
[root@virt-server ~]# ip link show xenbr0
5: xenbr0:  mtu 1500 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/ether ca:63:ce:3d:b9:81 brd ff:ff:ff:ff:ff:ff
    
# 然后把ens33的IP地址拆掉,并把该IP地址加到网桥上面,并把网桥状态设为UP。
# 然后需要在虚拟网桥上,把我们的物理网卡桥接上去
[root@virt-server ~]# ifconfig ens33 0;ifconfig xenbr0 192.168.245.131/24 up;brctl addif xenbr0 ens33

# 查看一下现在的网卡状态
[root@virt-server ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet6 fe80::20c:29ff:fe28:bf21  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 5746  bytes 524821 (512.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3392  bytes 345982 (337.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 22  bytes 2294 (2.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 22  bytes 2294 (2.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

xenbr0: flags=4163  mtu 1500
        inet 192.168.245.131  netmask 255.255.255.0  broadcast 192.168.245.255
        inet6 fe80::c863:ceff:fe3d:b981  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 19  bytes 1144 (1.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26  bytes 2188 (2.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 查看下网桥状态
[root@virt-server ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
xenbr0		8000.000c2928bf21	no		ens33

这种手动命令行方法配置的特点是,一旦reboot重启,一切会恢复原状

[root@virt-server ~]# reboot
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(Virtualization) at 14:45:22.
[root@virt-server ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.245.131  netmask 255.255.255.0  broadcast 192.168.245.255
        inet6 fe80::20c:29ff:fe28:bf21  prefixlen 64  scopeid 0x20
        ether 00:0c:29:28:bf:21  txqueuelen 1000  (Ethernet)
        RX packets 72  bytes 10619 (10.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 73  bytes 12376 (12.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@virt-server ~]# brctl show
bridge name	bridge id		STP enabled	interfaces

你可能感兴趣的:(linux,网络)