CentOS6中如何搭建网桥?

by LauCyun Aug 8,2016 13:05:18 9,585 views

首先,安装bridge-utils

[root@localhost ~]# yum install -y bridge-utils

检查一下是否安装成功:

[root@localhost ~]# brctl --version
bridge-utils, 1.2

查看一下当前的网卡信息:

[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:83:D9:2B
          inet addr:192.168.0.163  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe83:d92b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:71757 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40145 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:106907729 (101.9 MiB)  TX bytes:2869292 (2.7 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

接着创建一个名为br0的网桥:

[root@localhost ~]# brctl addbr br0

接着,把网卡eth0绑定在网桥br0上:

[root@localhost ~]# brctl stp br0 off
[root@localhost ~]# brctl addif br0 eth0
[root@localhost ~]# ifconfig br0 up

查看一下网桥br0的信息:

[root@localhost ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.08002783d92b	no		eth0

好了,网桥br0已经创建好了,那么接下来配置网桥br0

[root@localhost ~]# touch /etc/sysconfig/network-scripts/ifcfg-br0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.230
NETMASK=255.255.255.0
GATEWAY=192.168.0.1

接下来配置网卡eth0

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BRIDGE=br0

注意:请勿添加其它配置项(如GATEWAYBOOTPROTO等),否则会导致桥接配置失败!

然后,重启网络服务:

[root@localhost ~]# service NetworkManager restart

如果重启网卡不起作用的话,就重启系统reboot

查看网卡信息:

[root@localhost ~]# ifconfig
br0       Link encap:Ethernet  HWaddr 08:00:27:83:D9:2B
          inet addr:192.168.0.230  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe83:d92b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1965 errors:0 dropped:0 overruns:0 frame:0
          TX packets:171 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:440135 (429.8 KiB)  TX bytes:51639 (50.4 KiB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:83:D9:2B
          inet6 addr: fe80::a00:27ff:fe83:d92b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1973 errors:0 dropped:0 overruns:0 frame:0
          TX packets:185 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:468125 (457.1 KiB)  TX bytes:52965 (51.7 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

OK,大功告成!

最后,附上删除网桥的方法:

[root@localhost ~]# brctl delif br0 eth0  # 解除和eth0的绑定
[root@localhost ~]# ifconfig br0 down     # 停止网桥br0
[root@localhost ~]# brctl delbr br0       # 删除网桥br0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0  # 重置网卡eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp

(全文完)

Tags