Breadcrumbs

Network Bridge (Switch)

A bridge is a piece of software used to unite two or more network segments. A bridge behaves like a virtual network switch, working transparently (the other machines do not need to know about its existence). Any real devices (e.g. eth0) and virtual devices (e.g. tap0) can be connected to it.

This article explains how to create a bridge that contains at least an ethernet device.

Configuring bridging in /etc/network/interfaces

To make your bridge a little more permanent, you will need to edit /etc/network/interfaces. Using our example names, make it look like this and you’re set (if you want to use DHCP):

Bash
auto lo
 iface lo inet loopback
auto can0
 iface can0 inet manual
 pre-up ip link set $IFACE type can bitrate 125000
 up /sbin/ifconfig $IFACE up
 down /sbin/ifconfig $IFACE down
auto can1
 iface can1 inet manual
 pre-up ip link set $IFACE type can bitrate 125000
 up /sbin/ifconfig $IFACE up
 down /sbin/ifconfig $IFACE down

auto eth0
 iface eth0 inet static
 address 1.0.0.0
 netmask 255.255.255.0
auto eth1
 iface eth1 inet static
 address 0.0.0.0
 netmask 255.255.255.0
auto br0
 iface br0 inet dhcp
 pre-up brctl addbr br0; brctl addif br0 eth0 eth1 ; brctl setfd br0 0; brctl stp br0 no;
 post-down brctl delif br0 eth0 eth1 ; brctl delbr br0;
 bridge_stp no
 bridge_waitport 0
 bridge_fd 0
 bridge_ports eth0 eth1


If you like static IP’s, then you can just add the static IP options under the br0 interface setup. For example:

Bash
auto lo
 iface lo inet loopback
auto can0
 iface can0 inet manual
 pre-up ip link set $IFACE type can bitrate 125000
 up /sbin/ifconfig $IFACE up
 down /sbin/ifconfig $IFACE down
auto can1
 iface can1 inet manual
 pre-up ip link set $IFACE type can bitrate 125000
 up /sbin/ifconfig $IFACE up
 down /sbin/ifconfig $IFACE down

auto eth0
 iface eth0 inet static
 address 1.0.0.0
 netmask 255.255.255.0
auto eth1
 iface eth1 inet static
 address 0.0.0.0
 netmask 255.255.255.0

auto br0
 iface br0 inet static
 address 192.168.1.125
 netmask 255.255.255.0
 gateway 192.168.1.254
 pre-up brctl addbr br0; brctl addif br0 eth0 eth1 ; brctl setfd br0 0; brctl stp br0 no;
 post-down brctl delif br0 eth0 eth1 ; brctl delbr br0;
 bridge_stp no
 bridge_waitport 0
 bridge_fd 0
 bridge_ports eth0 eth1

To bring up your bridge, you just have to issue  # ifup br0 and it’ll bring up the other necessary interfaces without anything in your interfaces file about the bridged interfaces.

Or you can restart the network service by calling "service networking restart"


The network interfaces used in the bridge must be integrated in the interfaces file as shown above as an example in the interfaces file. Otherwise they are not active.

Some other useful options:

Option

Description

bridge_stp off

disable Spanning Tree Protocol

bridge_waitport 0

no delay before a port becomes available

bridge_fd 0

no forwarding delay

bridge_ports none

if you do not want to bind to any ports

bridge_ports regex eth*

use a regular expression to define ports

Some useful commands:

Command

Description

brctl addbr br0

To create a bridge named br0

brctl delbr br0

To remove a bridge named br0

brctl addif br0 eth0 brctl addif br0 eth1

To add interfaces eth0 and eth1 to a bridge br0

brctl delif br0 eth0

To remove an interface eth0 to a bridge br0

bridge link

To show the existing bridges and associated interfaces

brctl show

Show current bridges and what interfaces they are connected to

Remarks:

  • You can also use the SystemWebConfig to integrate switch support on your device