How to create a bridge network on Linux with netplan
A network bridge allows you to connect segments of your network together. Jack Wallen shows you how to create a bridge with the help of netplan.
If you work with certain virtual machine or container technologies, such as KVM and LXD, you might need to enable bridge networking in order to expose those services to your network. In simple terms (with regards to virtual machines and containers), a bridge connects a virtual machine or exposes a container to a network using the host computer’s Ethernet adapter.
Many modern Linux distributions, such as Ubuntu and its derivatives, now use netplan as it’s network configuration abstraction. I want to walk you through the process of creating a bridge network, such that it can be used for your virtual machines.
SEE: Implementing DevOps: A guide for IT pros (free PDF) (TechRepublic)
What you’ll need
I’ll be demonstrating with Ubuntu Server 20.04, but you should be able to make this work with any Linux distribution that works with netplan.
How to back up your configuration
The first thing you should do is back up your current netplan configuration. Before you do this, you need to know the name of that configuration. Issue the command:
You should see a filename similar to 50-cloud-init.yaml or 01-netcfg.yaml.
Back that file up with the command:
sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.bak
Make sure to exchange the name above with the name of your configuration file.
How to create the bridge
Now, let’s configure our bridge. Open the configuration file for editing with the command:
sudo nano /etc/netplan/50-cloud-init.yaml
This file might look similar to:
network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: no addresses: - 192.168.1.17/24 gateway4: 192.168.1.1 nameservers: addresses: [126.96.36.199,188.8.131.52]
Remember, this is a YAML file, so consistent indentation is important.
What we want to do first is comment out the lines that configure the address scheme information for our Ethernet device. So in our example above, comment out the lines as you see below:
network: version: 2 renderer: networkd ethernets: enp0s3: # dhcp4: no # addresses: # - 192.168.1.17/24 # gateway4: 192.168.1.1 # nameservers: # addresses: [184.108.40.206,220.127.116.11]
You could also remove those lines, but for the sake of simplicity (and possible debugging later on), we’ll just comment them out. Now we create the bridge, which falls under the netplan directive bridges. We’ll name our bridge br0 and it will use the same network scheme as did the standard network configuration.
So our new 50-cloud-init.yaml file will look like:
network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: false dhcp6: false bridges: br0: interfaces: [enp0s3] addresses: [192.168.1.222/24] gateway4: 192.168.1.1 mtu: 1500 nameservers: addresses: [18.104.22.168] parameters: stp: true forward-delay: 4 dhcp4: no dhcp6: no
The big difference (outside of defining the bridge name) is using the two parameters:
stp – Defines whether the bridge should use Spanning Tree Protocol
forward-delay – Specifies the period of time the bridge will remain in the Listening and Learning states before getting to the Forwarding state
Save and close the file.
How to generate and apply the configuration
Our next step is to convert the netplan YAML into configuration files understood by the backends. To do that, issue the command:
sudo netplan generate
You should see no output from the above command. After that, apply the configuration with the command:
sudo netplan apply
At this point, your bridge has been created. To make sure, issue the command:
ip a | grep br0
The output of the above command should display the configuration information for the bridge (Figure A).
Congratulations, you’ve created your first network bridge with netplan. Next time around, I’ll show you how to use that bridge with LXD containers.