Friday, July 8, 2016

How to configure a Linux bridge interface

http://xmodulo.com/how-to-configure-linux-bridge-interface.html

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.
A Linux bridge can be created with a user-space command-line tool called brctl which allows you to create, remove and administer Linux Ethernet bridges.
To use brctl command, you need to install the following package.
On Ubuntu or Debian:
$ sudo apt-get install bridge-utils
On 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 the 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 permanent 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 
    netmask 
    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 permanent 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=
NETMASK=
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
Download this article as ad-free PDF (made possible by your kind donation):  Download PDF

No comments:

Post a Comment