手动创建桥接网卡

2022-01-25 19:13:43

现在我这里主机有如下两块网卡:

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.1.201  netmask 255.255.255.0  broadcast 10.0.1.255
        inet6 fe80::460b:4f00:49d1:2865  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:2f  txqueuelen 1000  (Ethernet)
        RX packets 62  bytes 8044 (7.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 9130 (8.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.201  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::5091:4f3a:ff97:359d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:39  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 141 (141.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21  bytes 1632 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (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

桥接设备的管理依赖于 brctl 工具,先安装软件包:

$ yum install bridge-utils -y

以使用 eth0 创建桥接网卡为例,有如下几种方式。

注意:务必关闭 NetworkManager 服务。

方式一:手动配置

1、创建桥接网卡配置文件:

$ cd /etc/sysconfig/network-scripts 
$ cp ifcfg-eth0 ifcfg-br0

2、编辑桥接网卡配置文件:

$ vim ifcfg-br0
# 虚拟桥设备充当主机网卡
NAME=br0
DEVICE=br0
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Bridge
IPADDR=10.0.1.201
PREFIX=24
GATEWAY=10.0.1.2
DNS1=10.0.1.2

3、修改 eth0 配置文件:

$ vim ifcfg-eth0
# 原物理网卡充当交换机,不需要 IP、网关及 DNS。
NAME=eth0
DEVICE=eth0
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
# 明确指定桥到 br0
BRIDGE=br0

4、重启网卡,查看网卡信息如下:

$ systemctl restart network
$ ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.1.201  netmask 255.255.255.0  broadcast 10.0.1.255
        inet6 fe80::20c:29ff:fec4:32f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:2f  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 98 (98.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1624 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:c4:03:2f  txqueuelen 1000  (Ethernet)
        RX packets 1103  bytes 125183 (122.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 663  bytes 147661 (144.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.201  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::20c:29ff:fec4:339  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:39  txqueuelen 1000  (Ethernet)
        RX packets 2228  bytes 245167 (239.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1333  bytes 515291 (503.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (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

可通过 brctl 查看桥设备:

$ brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c29c4032f       no              eth0

方式二:命令行配置

1、添加桥接设备:

$ brctl addbr br0
# 可通过 ip a 或 ifconfig -a 查看到新添加的桥设备

2、拆掉 eth0 的地址:

$ ifconfig eth0 0 up

3、把桥设备关联到 eth0:

$ brctl addif br0 eth0

4、给桥设备添加地址并启用:

$ ifconfig br0 10.0.1.201/24 up

5、查看网卡信息如下:

$ ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.1.201  netmask 255.255.255.0  broadcast 10.0.1.255
        inet6 fe80::20c:29ff:fec4:32f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:2f  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 516 (516.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::ae3d:c19c:1bdf:10df  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:2f  txqueuelen 1000  (Ethernet)
        RX packets 5943  bytes 8638758 (8.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1698  bytes 104117 (101.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.201  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::5091:4f3a:ff97:359d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:c4:03:39  txqueuelen 1000  (Ethernet)
        RX packets 405  bytes 45762 (44.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 283  bytes 68554 (66.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (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

建议启动 stp:brctl stp br0 on