Linux brctl 详解

1、brctl

1.1 brctl 介绍

brctl 用来管理以太网桥,在内核中建立、维护、检查网桥配置。一个网桥一般用来连接多个不同的网络,这样这些不同的网络就可以像一个网络那样进行通讯。

网桥是一种在链路层实现中继,对帧进行转发的技术,根据MAC分区块,可隔离碰撞,将网络的多个网段在数据链路层连接起来的网络设备。网桥工作在数据链路层,将两个LAN连起来,根据MAC地址来转发帧,可以看作一个“底层的路由器”。

在网桥上每个以太网连接可以对应到一个物理接口,这些以太网接口组合成一个大的逻辑的接口,这个逻辑接口对应于桥接网络。

1.2 brctl 前提

# modprobe bridge                             ## 加载 bridge 模块
# echo "1">/proc/sys/net/ipv4/ip_forward      ## 开启转发,多个网卡之间进行数据交互

2、brctl 安装及常用命令介绍

2.1 安装 bridge-utils

Linux 的相关网桥管理命令是由 网桥管理工具包 bridge-utils 来提供的。所以如果需要实现 Linux 网桥的相关配置,需要首先安装此工具包。

[root@Tang ~]# yum info bridge-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Available Packages
Name        : bridge-utils
Arch        : x86_64
Version     : 1.5
Release     : 9.el7
Size        : 32 k
Repo        : base/7/x86_64
Summary     : Utilities for configuring the linux ethernet bridge
URL         : http://www.linuxfoundation.org/collaborate/workgroups/networking/bridge
License     : GPLv2+
Description : This package contains utilities for configuring the linux ethernet
            : bridge. The linux ethernet bridge can be used for connecting multiple
            : ethernet devices together. The connecting is fully transparent: hosts
            : connected to one ethernet device see hosts connected to the other
            : ethernet devices directly.
            : 
            : Install bridge-utils if you want to use the linux ethernet bridge.
[root@Tang ~]# yum info bridge-utils
Installed:
  bridge-utils.x86_64 0:1.5-9.el7                                                                                                    

Complete!

2.2 brctl 相关命令介绍

2.2.1 命令帮助查看 1

[root@Tang ~]# brctl --help
Usage: brctl [commands]
commands:
	addbr     	<bridge>		add bridge
	delbr     	<bridge>		delete bridge
	addif     	<bridge> <device>	add interface to bridge
	delif     	<bridge> <device>	delete interface from bridge
	hairpin   	<bridge> <port> {on|off}	turn hairpin on/off
	setageing 	<bridge> <time>		set ageing time
	setbridgeprio	<bridge> <prio>		set bridge priority
	setfd     	<bridge> <time>		set bridge forward delay
	sethello  	<bridge> <time>		set hello time
	setmaxage 	<bridge> <time>		set max message age
	setpathcost	<bridge> <port> <cost>	set path cost
	setportprio	<bridge> <port> <prio>	set port priority
	show      	[ <bridge> ]		show a list of bridges
	showmacs  	<bridge>		show a list of mac addrs
	showstp   	<bridge>		show bridge stp info
	stp       	<bridge> {on|off}	turn stp on/off

2.2.2 命令帮助查看 2

[root@Tang ~]# man brctl

2.2.3 常用命令

# brctl addbr <name>     ## 创建一个名为 name 的桥接网络接口
# brctl delbr <name>     ## 删除一个名为 name 的桥接网络接口,桥接网络接口必须先 down 掉后才能删除
# brctl show             ## 显示目前所有的桥接接口

#  brctl addif <brname> <ifname>  
	## 把一个物理接口 ifname 加入桥接接口 brname 中,所有从 ifname 收到的帧都将被 brname 处理
	## 就像网桥处理的一样。所有发往 brname 的帧,ifname 就像输出接口一样。当物理以太网接口加入网桥后,处于混杂模式了,所以不需要配置IP
# brctl delif <brname> <ifname>    ## 从 brname 中脱离一个ifname接口
# brctl show <brname>              ## 显示一些网桥的信息
# brctl stp <bridge> <state>
	## STP 多个以太网桥可以工作在一起组成一个更大的网络,利用 802.1d 协议在两个网络之间寻找最短路径
	## STP 的作用是防止以太网桥之间形成回路,如果确定只有一个网桥,则可以关闭 STP
	## 控制网桥是否加入 STP 树中
	## <state> 可以是'on'或者'yes'表示加入 stp 树中,这样当 lan 中有多个网桥时可以防止回环
		##'off'表示关闭stp。
# brctl setbridgeprio <bridge> <priority>
	## 设置网桥的优先级,<priority> 的值为0-65535,值小的优先级高,优先级最高的是根网桥。

# brctl setfd <bridge> <time> 
	## 设置网桥的'bridge forward delay'转发延迟时间,时间以秒为单位

# brctl sethello <bridge> <time> 
	## 设置网桥的'bridge hello time'存活检测时间

# brctl setmaxage <bridge> <time>
	## 设置网桥的'maximum message age'时间

# brctl setpathcost <bridge> <port> <cost>
	## 设置网桥中某个端口的链路花费值

# brctl  setportprio  <bridge>  <port> <priority>
	## 设置网桥中某个端口的优先级
# brctl addbr brneo           ## 创建新网桥 brneo
# brctl delbr brneo           ## 删除网桥 brneo
# brctl addif brneo eth0      ## 将接口 eth0 加入网桥 brneo
# brctl delif brneo eth0      ## 将接口 eth0 从网桥 brneo 中删除
# brctl show                  ## 查看网桥信息
# brctl show brneo            ## 查看网桥 brneo 的信息  
# brctl stp brneo on          ## 开启网桥 brneo 的 STP,避免成环
发布了158 篇原创文章 · 获赞 7 · 访问量 9746

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/103138223