6

Systemd's systemd-networking can replace tbe used to replace the existing networking system on Raspbian.

How does it work with Raspbian on a Raspberry Pi 3 and it's two interfaces for ethernet and wlan? Can I also realize dynamic failover for them?

8

Using systemd-networkd instead of default dhcpcd is of course possible. But it is not meaningful in all cases.

networkd is a small and lean service to configure network interfaces, designed mostly for server use cases in a world with hotplugged and virtualized networking. Its configuration is similar in spirit and abstraction level to ifupdown, but you don't need any extra packages to configure bridges, bonds, vlan etc. It is not very suitable for managing WLANs yet; NetworkManager is still much more appropriate for such Desktop use cases. [1]

But for a raspi laying near by a TV or amplifier and doing its work 24/7 for streaming audio or video or for a camera etc., systemd-networkd is a good choise. But you have to do a complete switch. There is no way to mix up with networking and/or dhcpcd.

Step 1: Preparation

For reference I use a new flashed SD Card from a 2017-11-29-raspbian-stretch-lite.img with systemd 232.

I will have attention to a headless installation only with ssh. If you using this, double check typos or so otherwise you are lost with a broken connection. Only if you want to enable ssh and haven't done it yet, use:

pi@raspberrypi: ~$ sudo touch /boot/ssh

First enable persistent logging in journald for messages from systemd [1]. It is not needed for networkd but its messages.

pi@raspberrypi: ~$ sudo -Es
root@raspberrypi: ~# mkdir -p /var/log/journal
root@raspberrypi: ~# systemd-tmpfiles --create --prefix /var/log/journal

Disable the old stuff. This will take effect on next boot. Don't stop them!

In /etc/resolvconf.conf insert these lines.

# Set to NO to disable resolvconf from running any subscribers. Defaults to YES.
resolvconf=NO

Then disable this:

root@raspberrypi: ~# systemctl disable networking
root@raspberrypi: ~# systemctl disable dhcpcd

And enable systemd-networkd:

root@raspberrypi: ~# systemctl enable systemd-networkd
root@raspberrypi: ~# systemctl enable systemd-resolved
root@raspberrypi: ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

Step 2: Setup ethernet interface

Create this file with your settings:

root@raspberrypi: ~# cat >/etc/systemd/network/04-eth.network <<EOF
[Match]
Name=e*
[Network]
DHCP=ipv4
IPv6AcceptRA=no
LinkLocalAddressing=no
# to use static IP uncomment these instead of DHCP
#Address=192.168.1.86/24
#Gateway=192.168.1.1
[DHCP]
RouteMetric=10
UseDomains=yes
EOF

root@raspberrypi: ~# chown systemd-network:adm /etc/systemd/network/04-eth.network
root@raspberrypi: ~# exit
pi@raspberrypi: ~$

Reboot, but only if you have an ethernet interface ;-)

Step 3: Setup wlan interface

Create this file with your settings:

pi@raspberrypi:~ $ sudo -Es
root@raspberrypi:~ # cat >/etc/systemd/network/08-wifi.network <<EOF
[Match]
Name=wl*
[Network]
DHCP=ipv4
IPv6AcceptRA=no
LinkLocalAddressing=no
# to use static IP uncomment these instead of DHCP
#Address=192.168.1.87/24
#Gateway=192.168.1.1
[DHCP]
RouteMetric=20
UseDomains=yes
EOF

root@raspberrypi: ~# chown systemd-network:adm /etc/systemd/network/08-wifi.network
root@raspberrypi:~ #

Setup wpa_supplicant with this file and your settings and enable it:

root@raspberrypi:~ # cat >/etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="wlan@hoeft-online.de"
    psk="realyNotMyPassword"
}
EOF

root@raspberrypi:~ # chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
root@raspberrypi:~ # systemctl disable wpa_supplicant.service
root@raspberrypi:~ # systemctl enable wpa_supplicant@wlan0.service

Reboot if you have a wifi interface. Most of you will have. Good luck ...

Step 4: Bonding ethernet and wlan interface for failover

You should have both interfaces setup and running as described above. It is no problem when both interfaces are up. The kernel will use the interface with the lowest RouteMetric first. Here the ethernet interface will be used first. But this has a great disadvantage. As you can see with ~$ ip addr each interface has it's own ip-address. If the kernel switches the interface because one is gone down it also uses its new source ip-address. This will break any established communication, e.g. ssh, streaming and so on. You have to reboot to take effect. But that isn't really what we want.

The solution of this problem is bonding. We create an interim interface bond0 that does not change its settings. Ethernet and wifi will switch to bond0.

First disable the single ethernet and wifi network files:

pi@raspberrypi:~ $ sudo -Es
root@raspberrypi:~ # cd /etc/systemd/network/
root@raspberrypi:~ # mv 04-eth.network 04-eth.network~
root@raspberrypi:~ # mv 08-wifi.network 08-wifi.network~

Then setup bonding with this five files:

root@raspberrypi:~ # cat >/etc/systemd/network/12-bond0.netdev <<EOF
[NetDev]
# status: cat /proc/net/bonding/bond0
Name=bond0
Kind=bond
EOF

root@raspberrypi:~ # cat >/etc/systemd/network/16-bond0-add-eth.network <<EOF
[Match]
Name=e*
[Network]
Bond=bond0
EOF

root@raspberrypi:~ # cat >/etc/systemd/network/20-bond0-add-wifi.network <<EOF
[Match]
Name=wl*
[Network]
Bond=bond0
EOF

root@raspberrypi:~ # cat >/etc/systemd/network/24-bond0-up.network <<EOF
[Match]
Name=bond0
[Network]
DHCP=ipv4
IPv6AcceptRA=no
LinkLocalAddressing=no
# to use static IP uncomment these instead of DHCP
#Address=192.168.1.86/24
#Gateway=192.168.1.1
EOF

root@raspberrypi:~ # cat >/etc/modprobe.d/bonding.conf <<EOF
options bonding mode=active-backup
options bonding primary=eth0
options bonding miimon=500
options bonding updelay=1000
options bonding downdelay=1000
options bonding min_links=1
EOF

root@raspberrypi: ~# chown systemd-network:adm /etc/systemd/network/*
root@raspberrypi:~ # exit
pi@raspberrypi:~ $

It should be said that there are also options for bonding in systemd-networkd [2] but they are buggy in systemd 232. They are working since systemd 234. So we setup the bonding driver direct with modprobe. It's parameter can you get with ~$ modinfo bonding.

Now it's time to reboot.

Then you can check and test bonding with your settings:

pi@raspberrypi:~ $ cat /proc/net/bonding/bond0

pi@raspberrypi:~ $ ip addr
pi@raspberrypi:~ $ sudo ip link set eth0 down
pi@raspberrypi:~ $ sudo ip link set eth0 up
pi@raspberrypi:~ $ sudo ip link set wlan0 down
pi@raspberrypi:~ $ sudo ip link set wlan0 up

But don't down both interfaces together ;-)

Step 5: Clean up

Purge the old stuff:

pi@raspberrypi:~ $ sudo apt install deborphan
pi@raspberrypi:~ $ sudo apt --autoremove purge openresolv
pi@raspberrypi:~ $ sudo apt --autoremove purge ifupdown
pi@raspberrypi:~ $ sudo apt --autoremove purge dhcpcd5
pi@raspberrypi:~ $ sudo apt --autoremove purge isc-dhcp-client isc-dhcp-common
pi@raspberrypi:~ $ sudo apt --autoremove purge $(deborphan)
pi@raspberrypi:~ $ sudo apt --autoremove purge $(deborphan) #two times


references:
[1] /usr/share/doc/systemd/README.Debian.gz
[2] man systemd.netdev
[3] man systemd.network
[4] https://wiki.debian.org/Bonding
[5] https://www.kernel.org/doc/Documentation/networking/bonding.txt k

  • Note with regard to the first quote up top: Raspbian never used NetworkManager. It is more an artifact of Fedora and derived systems (which was the first place that systemd, a Redhat supported project, was deployed). – goldilocks Feb 10 '18 at 12:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.