Creating a bridge for virtual machines using systemd-networkd
Table of Contents
There are plenty of guides out there for making ethernet bridges in Linux to support virtual machines using built-in network scripts or NetworkManager. I decided to try my hand with creating a bridge using only systemd-networkd and it was surprisingly easy.
First off, you’ll need a version of systemd with networkd support. Fedora 20 and 21 will work just fine. RHEL/CentOS 7 and Arch Linux should also work. Much of the networkd support has been in systemd for quite a while, but if you’re looking for fancier network settings, like bonding, you’ll want at least systemd 216.
Getting our daemons in order #
Before we get started, ensure that systemd-networkd will run on a reboot and NetworkManager is disabled. We also need to make a config file director for systemd-networkd if it doesn’t exist already. In addition, let’s enable the caching resolver and make a symlink to systemd’s resolv.conf
:
systemctl enable systemd-networkd
systemctl disable NetworkManager
systemctl enable systemd-resolved
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
mkdir /etc/systemd/network
Configure the physical network adapter #
In my case, the network adapter connected to my external network is enp4s0 but yours will vary. Run ip addr
to get a list of your network cards. Let’s create /etc/systemd/network/uplink.network
and put the following in it:
[Match]
Name=enp4s0
[Network]
Bridge=br0
I’m telling systemd to look for a device called enp4s0 and then add it to a bridge called br0 that we haven’t configured yet. Be sure to change enp4s0 to match your ethernet card.
Make the bridge #
We need to tell systemd about our new bridge network device and we also need to specify the IP configuration for it. We start by creating /etc/systemd/network/br0.netdev
to specify the device:
[NetDev]
Name=br0
Kind=bridge
This file is fairly self-explanatory. We’re telling systemd that we want a device called br0 that functions as an ethernet bridge. Now create /etc/systemd/network/br0.network
to specify the IP configuration for the br0 interface:
[Match]
Name=br0
[Network]
DNS=192.168.250.1
Address=192.168.250.33/24
Gateway=192.168.250.1
This file tells systemd that we want to apply a simple static network configuration to br0 with a single IPv4 address. If you want to add additional DNS servers or IPv4/IPv6 addresses, just add more DNS=
and Address
lines right below the ones you see above. Yes, it’s just that easy.
Let’s do this #
Some folks are brave enough to stop NetworkManager and start all of the systemd services here but I prefer to reboot so that everything comes up cleanly. That will also allow you to verify that future reboots will cause the server to come back online with the right configuration. After the reboot, run networkctl
and you’ll get something like this (with color):
Here’s what’s in the screenshot:
IDX LINK TYPE OPERATIONAL SETUP
1 lo loopback carrier unmanaged
2 enp2s0 ether off unmanaged
3 enp3s0 ether off unmanaged
4 enp4s0 ether degraded configured
5 enp5s0 ether off unmanaged
6 br0 ether routable configured
7 virbr0 ether no-carrier unmanaged
7 links listed.
My ethernet card has four ports and only enp4s0 is in use. It has a degraded status because there is no IP address assigned to enp4s0. You can ignore that for now but it would be nice to see this made more clear in a future systemd release.
Look at br0 and you’ll notice that it’s configured and routable. That’s the best status you can get for an interface. You’ll also see that my other ethernet devices are in the unmanaged state. I could easily add more .network
files to /etc/systemd/network
to configure those interfaces later.
Further reading #
As usual, the Arch Linux wiki page on systemd-networkd is a phenomenal resource. There’s a detailed overview of all of the available systemd-networkd configuration file options over at systemd’s documentation site.