簡介
- 網橋是一種軟體配置,用于連結兩個或更多個不同網段。網橋的行為就像是一台虛拟的網絡交換機,工作于透明模式(即其他機器不必關注網橋的存在與否)。任意的真實實體裝置(例如 eth0)和虛拟裝置(例如 tap0)都可以連接配接到網橋。
- 橋接:把一台機器上的若幹個網絡接口“連接配接”起來。其結果是,其中一個網口收到的封包會被複制給其他網口并發送出去。以使得網口之間的封包能夠互相轉發。網橋就是這樣一個裝置,它有若幹個網口,并且這些網口是橋接起來的。與網橋相連的主機就能通過交換機的封包轉發而互相通信。
如下圖:主機A發送的封包被送到交換機S1的eth0口,由于eth0與eth1、eth2橋接在一起,故而封包被複制到eth1和eth2,并且發送出去,然後被主機B和交換機S2接收到。而S2又會将封包轉發給主機C、D
建立網橋
可以通過多種方式實作,這裡介紹通過nmcli和bridge實作。
通過nmcli
1)添加一個名為br0的網橋,并且手工指定ip為192.168.8.100
[root@CentOS7 ~]# brctl show
bridge name bridge id STP enabled interfaces
[root@CentOS7 ~]# nmcli con add con-name br0 type bridge ifname br0
Connection 'br0' (77a81012-0760-441a-a90a-8f4b357c5dfb) successfully added.
[root@CentOS7 ~]# nmcli con modify br0 ipv4.addresses 192.168.8.100/24 ipv4.method m
anual
2)添加eth0、eth1裝置到網橋br0
[root@CentOS7 ~]# nmcli con add con-name br0-port0 type bridge-slave ifname eth0 master br0
Connection 'br0-port0' (60961b5d-312c-49dd-b9ff-287f8236436b) successfully added.
[root@CentOS7 ~]# nmcli con add con-name br0-port1 type bridge-slave ifname eth1 master br0
Connection 'br0-port1' (e4b436f2-8bc5-4696-ad6d-bdcb3ec40a0f) successfully added.
3)啟動網橋并且檢視資訊
[root@CentOS7 ~]# nmcli con up br0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/32
[root@CentOS7 ~]# nmcli device show br0
GENERAL.DEVICE: br0
GENERAL.TYPE: bridge
GENERAL.HWADDR: E6:CF:41:C0:93:74
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: br0
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/32
IP4.ADDRESS[1]: 192.168.8.100/24
IP4.GATEWAY: --
IP4.ROUTE[1]: dst = 192.168.8.0/24, nh = 0.0.0.0, mt = 425
IP6.GATEWAY: --
4)檢視其生成的配置檔案
[root@CentOS7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
[root@CentOS7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0-port0
[root@CentOS7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0-port1
通過brctl
該工具需要安裝bridge-utils包
[root@CentOS7 ~]# rpm -qf `which brctl`
bridge-utils-1.5-9.el7.x86_64
[root@CentOS7 ~]# yum -y install bridge-utils
1)建立一個名為br0的網橋
[root@CentOS7 ~]# brctl addbr br0
2)添加eth0、eth1裝置到br0網橋
[root@CentOS7 ~]# brctl addif br0 eth0
[root@CentOS7 ~]# brctl addif br0 eth1
[root@CentOS7 network-scripts]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29082a73 no eth0
eth1
[root@CentOS7 network-scripts]# ip link set up dev br0
[root@CentOS7 network-scripts]# ip link set dev br0 down
[root@CentOS7 network-scripts]# brctl delbr br0
[root@CentOS7 network-scripts]# brctl show
bridge name bridge id STP enabled interfaces