What it is
brctl is a command-line utility for controlling and inspecting Ethernet bridges on Linux systems, primarily used for managing network bridging for virtual machines or network segmentation.
Installation
Linux
brctl is typically part of the bridge-utils package.
# Debian/Ubuntu
sudo apt update
sudo apt install bridge-utils
# Fedora/CentOS/RHEL
sudo dnf install bridge-utils
# or
sudo yum install bridge-utils
Mac
brctl is not available on macOS. macOS uses networksetup and other built-in tools for network configuration.
Windows
brctl is not available on Windows. Windows uses its own Network Bridge feature through the GUI or PowerShell cmdlets like New-NetBridgeAdapter.
Core Concepts
- Bridge: A software switch that forwards network traffic between network interfaces (physical or virtual) connected to it. It operates at Layer 2 (Data Link Layer) of the OSI model.
- Interface: A network adapter (e.g.,
eth0,wlan0,tap0) that can be attached to a bridge. - MAC Address Table (Forwarding Database): The bridge learns which MAC addresses are reachable on which of its interfaces and uses this table to forward frames efficiently.
- STP (Spanning Tree Protocol): A network protocol that ensures a loop-free topology by disabling redundant paths.
brctlcan be used to configure STP on a bridge.
Commands / Usage
Creating and Deleting Bridges
-
Create a new bridge:
sudo brctl addbr br0Creates a new bridge device named
br0. -
Delete a bridge:
sudo brctl delbr br0Deletes the bridge device
br0. The bridge must be down and have no interfaces attached for deletion.
Adding and Removing Interfaces
-
Add an interface to a bridge:
sudo brctl addif br0 eth0Attaches the network interface
eth0to the bridgebr0. The interfaceeth0must be up. -
Remove an interface from a bridge:
sudo brctl delif br0 eth0Detaches the network interface
eth0from the bridgebr0.
Bridge Status and Information
-
List all bridges:
brctl showDisplays a list of all active bridges on the system, showing their interfaces.
-
Show detailed information about a specific bridge:
brctl show br0Shows the interfaces attached to bridge
br0and its current configuration (STP status, etc.). -
Show bridge forwarding database (MAC address table):
brctl showmacs br0Displays the MAC addresses learned by bridge
br0, along with the interface they were learned on and their age. -
Show STP status for a bridge:
brctl showstp br0Displays the Spanning Tree Protocol status for bridge
br0, including port states (forwarding, blocking, etc.).
STP Configuration
-
Enable STP on a bridge:
sudo brctl stp br0 onEnables Spanning Tree Protocol on bridge
br0to prevent network loops. -
Disable STP on a bridge:
sudo brctl stp br0 offDisables Spanning Tree Protocol on bridge
br0. This is generally not recommended in complex networks. -
Set bridge priority:
sudo brctl setbridgeprio br0 32768Sets the priority of bridge
br0. Lower values have higher priority in STP election. The default is 32768. -
Set interface path cost:
sudo brctl setpathcost br0 eth0 10Sets the Spanning Tree Protocol path cost for interface
eth0on bridgebr0. Lower costs are preferred. -
Set interface priority:
sudo brctl setportprio br0 eth0 128Sets the Spanning Tree Protocol priority for interface
eth0on bridgebr0. Lower values have higher priority. -
Set hello time:
sudo brctl sethellotime br0 2Sets the STP hello time (in seconds) for bridge
br0. The default is 2 seconds. -
Set forward delay:
sudo brctl setfwdelay br0 15Sets the STP forward delay (in seconds) for bridge
br0. This is the time a port stays in the listening and learning states before transitioning to forwarding. The default is 15 seconds.
Common Patterns
-
Setting up a basic bridge for VMs: This involves creating a bridge, bringing it up, adding a physical interface to it, and then adding virtual interfaces (e.g., from
virt-managerordocker) to the bridge.- Create and bring up the bridge:
sudo brctl addbr br0 sudo ip link set br0 up - Add a physical interface (e.g.,
eth0) to the bridge:sudo brctl addif br0 eth0 - (Optional) Remove the IP address from the physical interface if the bridge will have an IP:
sudo ip addr flush dev eth0 - (Optional) Assign an IP address to the bridge:
sudo ip addr add 192.168.1.100/24 dev br0 sudo ip route add default via 192.168.1.1 - Now, virtual interfaces (like
tap0orvethpairs) can be added tobr0.
- Create and bring up the bridge:
-
Viewing learned MAC addresses on a bridge:
brctl showmacs br0Useful for debugging connectivity issues between VMs or containers connected to the same bridge.
-
Disabling STP on a simple, isolated bridge (e.g., for Docker):
sudo brctl stp br0 offDocker often manages its own network segmentation and may not require STP.
-
Checking which interface is connected to which bridge:
brctl showThis is the go-to command for a quick overview.
Gotchas
- Interface must be UP: You cannot add an interface to a bridge if the interface itself is not in the UP state. Use
sudo ip link set eth0 upbefore adding it. - Bridge must be UP: Similarly, the bridge device itself needs to be brought up using
sudo ip link set br0 upbefore interfaces can be reliably added or traffic passed. - IP Address on Physical Interface: If you add a physical interface (like
eth0) to a bridge, it’s generally recommended to remove the IP address from the physical interface itself (sudo ip addr flush dev eth0) and assign the IP address to the bridge device (br0) instead. Otherwise, you might experience routing conflicts or unexpected behavior. - STP and Loops: While STP prevents network loops, it can introduce latency and complex state management. For simple, contained bridging scenarios (like many Docker setups), disabling STP might be desired, but be aware of the risks in larger, interconnected networks.
- Persistence: Changes made with
brctlare typically not persistent across reboots. You’ll need to use network configuration files (e.g.,/etc/network/interfaceson Debian/Ubuntu,NetworkManagerconfigurations, or systemd-networkd) to make bridge configurations permanent. - Permissions: Most
brctlcommands require root privileges (sudo). - IPv6:
brctlprimarily deals with Layer 2 bridging. IPv6 configuration on bridges is usually handled by separate tools likeip6tablesandradvd.