This article discusses how to setup a network bridge in order to connect two portions of a network together.
A network bridge can be used to connect two independent network segments at layer 2 level (much like a network switch). Common applications include transparent proxying, transparent filtering (using iptables) and saving money on hardware as some mainboards come with two PHY interfaces. In this article,
enp2s0 will be the network interfaces used but of course they can be replaced by whatever interface names are present on a system (such as
In order to create a bridge on Linux a special bridging device is created (brX) that contains at least two network devices as ports (e.g. ethX or pppX). As the bridge works on layer 2, no IP addresses are needed on the port devices — on a typical setup, the bridging device itself will receive the IP (e.g. via DHCP).
[*] Networking support ---> Networking options ---> <M> 802.1d Ethernet Bridging
With net-misc/netifrc >=0.4.0, the installation of net-misc/bridge-utils has been deprecated. The network bridge is set up simply by configuring it in /etc/conf.d/net (cf. #See also).
But if you have older version of netifrc, or need it for some other reasons, install the net-misc/bridge-utils package to have access to the utilities needed to manage the bridge device:
emerge --ask bridge-utils
You need to do this with a console connection. You'll probably lose the ability to ssh into the box, if you are working on one of the ports being affected.
Make certain the physical Ethernet interfaces you are working on are not in /etc/init.d/ as symbolic links as part of your original install:
rc-update delete net.enp1s0 boot
rc-update delete net.enp2s0 boot
It's always best to learn how to do things first by hand, then you can automate it. This is a layer 2 connection you are creating, and as such, you do not need IP addresses assigned to the physical ports. The bridged physical interfaces (enp1s0 and enp2s0 in the below example) are put into promiscuous mode, so they will not be able to receive an IP address (e.g. via dhcp). The bridge will also not function properly if static IP addresses are forced on the interfaces.
Now create a bridge with no interfaces assigned (yet):
ip link add br0 type bridge
Add the two interfaces to the bridge:
ip link set dev enp1s0 master br0
ip link set dev enp2s0 master br0
See what you've done:
ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 3: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff 4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
Note that stp does not get turned on, unless you specify that that is what you want.
First, the bridge device must be added to the /etc/conf.d/net file. As an example, bridge configuration with static addresses:
# Set up the initial layer 2 bridge interface bridge_br0="enp1s0 enp2s0" # bridge config_br0="192.168.26.199 netmask 255.255.255.0" routes_br0="default via 192.168.26.254" bridge_forward_delay_br0=0 bridge_hello_time_br0=1000
It is important to include
bridge_hello_time_br0=1000in the /etc/conf.d/net file in order to bring the bridge interface up quickly. Other values will cause network packets to be dropped for the first 30 seconds after the bridge has become active. This, in turn, could prevent DHCP from working as intended.
More documentation can be found by reading /usr/share/doc/netifrc-*/net.example.bz2, for example: less /usr/share/doc/netifrc-0.5.1/net.example.bz2
Next, create the init script by linking net.lo to net.br0 and start the interface as follows:
ln -s /etc/init.d/net.lo /etc/init.d/net.br0
rc-service net.br0 start
Finally, to make sure the bridge is automatically set up on subsequent boots add the newly generated init script to the system's default run level:
rc-update add net.br0 default
As of systemd 210 and up, a special service called systemd-networkd is available for network configuration. This service can handle bridge construction.
The basic procedure of creating a network configuration with systemd-networkd is creating several .network and .netdev files.
First, create a bridge. With systemd-networkd this is as simple as creating a new .netdev file:
[NetDev] Name=br0 Kind=bridge
After the bridge definition is created, assign the interfaces to the bridge:
[Match] Name=eth* [Network] Bridge=br0
Multiple interfaces can be matched and attached to the bridge.
Notice that this bridge is still not active. Activation can be achieved by creating a .network definition to use the bridge.
[Match] Name=br0 [Network] DHCP=ipv4
[Match] Name=br0 [Network] DNS=192.168.1.1 Address=192.168.1.2/24 Gateway=192.168.1.1
Defining a gateway is only necessary if one intends to use the physical network interface as access to another network. When using the bridge as a private network, omit it as systemd-networkd will add the bridge as a default route when the Gateway option is set.
Do remember to enable and start the systemd-networkd service.