How to configure a Linux bridge interface

Last updated on November 3, 2020 by Dan Nanni

In computer networking, a bridge is a network device that interconnects more than one LAN segment at Layer-2. Bridges can filter traffic between different segments, thereby reducing the amount of traffic on LAN, even with many LAN segments. This bridge functionality is built into the Linux kernel, so one can set up a software bridge interconnecting multiple network interfaces.

In this post, I will describe how to configure a Linux bridge interface.

Preparation

In a server environment, a Linux bridge can be created with a user-space command-line tool called brctl. This tool allows you to create, remove and administer Linux Ethernet bridges.

To use brctl command, you need to install the following package.

For Ubuntu or Debian:

$ sudo apt-get install bridge-utils

For CentOS, RHEL and Fedora:

$ sudo yum install bridge-utils

Create a Linux Bridge from the Command Line

To create a bridge named br0:

$ sudo brctl addbr br0

To remove a bridge named br0:

$ sudo brctl delbr br0

To add interfaces eth0 and eth1 to a bridge br0:

$ sudo brctl addif br0 eth0
$ sudo brctl addif br0 eth1

To remove an interface eth0 to a bridge br0:

$ sudo brctl delif br0 eth0

It is worthwhile to note that a Linux bridge created by brctl is not persistent, meaning that any bridge created by brctl will automatically be destroyed upon boot. If you would like to have a permanent bridge configuration, you need to use a separate configuration file in /etc.

.

Create a Linux Bridge Permanently

In the rest of the tutorial, I will describe how to create a permanent Linux bridge interface from the command line using /etc configuration. If you want to use GUI-based Network Manager to configure a Linux bridge, refer to this tutorial instead.

As an example, I will create a Linux bridge called br0 and add eth0 and eth1 interfaces to the bridge.

Configure a Permanet Bridge Interface on Ubuntu or Debian

You need to edit /etc/network/interfaces as follows.

If the bridge br0 is to be assigned an IP address by DHCP:

auto eth0
iface eth0 inet manual

auto eth1
iface eth1 inet manual

auto br0
iface br0 inet dhcp
    bridge_ports      eth0 eth1

If the bridge br0 is to be assigned a static IP address:

auto eth0
iface eth0 inet manual

auto eth1
iface eth1 inet manual

auto br0
iface br0 inet static
    bridge_ports      eth0 eth1
    address <static_IP_address>
    netmask <netmask>
    gateway <gateway>

If you want to set up a transparent bridge between eth0 and eth1, you don't need to assign any IP address to the bridge. In that case, the following will do.

auto eth0
iface eth0 inet manual

auto eth1
iface eth1 inet manual

auto br0
iface br0 inet manual
    bridge_ports      eth0 eth1

Configure a Permanet Bridge Interface on CentOS, RHEL or Fedora

You need to update existing eth0/eth1 configuration in /etc/sysconfig/network-scripts/ifcfg-eth[0-1], and add bridge configuration in /etc/sysconfig/network-scripts/ifcfg-br0.

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
BRIDGE=br0
$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
BRIDGE=br0

If the bridge br0 is to be assigned an IP address by DHCP:

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
DELAY=0
BOOTPROTO=dhcp
ONBOOT=yes

If the bridge br0 is to be assigned a static IP address:

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
IPADDR=<static_IP_address>
NETMASK=<netmask>
GATEWAY=<gateway>
ONBOOT=yes

To set up a transparent bridge between two interfaces:

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=none
ONBOOT=yes

If you want to customize or inspect the bridge settings (e.g., MAC learning), refer to this tutorial.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean