Managing Wireless with systemd-networkd and wpa-supplicant
Related articles
While systemd-networkd is primarily intended for managing networks for containers, it is easy enough to combine it with wpa_supplicant
to manage a wireless network.
Contents
Create your network configuration file
In the directory /etc/systemd/network
create a file named wlp2s0
with the following contents:
[Match] Name=wlp2s0 [Network] # DHCP can be one of v4 (configure IPv4 only), v6 (configure IPv6 only), all (both IPv4 and IPv6) or none DHCP=v4
Create your wpa_supplicant@<interface>.conf file
In the directory /etc/wpa_supplicant
create the file wpa_supplicant-wlp2s0.conf
and put in the relevant information for the networks you'll want to connect to:
# Work network={ ssid="MyOfficeNetwork" key_mgmt=WPA-PSK psk="01234567" } # Home network={ ssid="MyHomeNetwork" key_mgmt=WPA-PSK psk="89abcdef" }
See the wpa_supplicant page for information on how to set up your configuration file.
Set up systemd-resolved
If you wish, you can set up systemd-resolved. If you don't systemd-networkd will happily use whatever's in your /etc/resolv.conf
.
First, create your /etc/systemd/resolved.conf
file, inserting your favorite public DNS servers in the DNS=
line.
# This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # # See resolved.conf(5) for details [Resolve] # Modify the line below to add your favorite public DNS servers DNS=208.67.222.222 8.8.8.8 #FallbackDNS=8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 #LLMNR=yes
Then start and enable systemd-resolved.service.
#systemctl start systemd-resolved.service #systemctl enable systemd-resolved.service
Finally, back up your existing /etc/resolv.conf
file and create a symlink from /etc/resolv.conf
to /var/run/systemd/resolve/resolv.conf
.
#cp /etc/resolv.conf /etc/resolv.conf.bak #ln -s /var/run/systemd/resolve/resolv.conf /etc/resolv.conf
Stop other network managers and start systemd-networkd, systemd-resolvd and wpa_supplicant@<interface>.service
#systemctl stop netctl-auto #systemctl start systemd-networkd #systemctl start systemd-resolvd #systemctl start wpa_supplicant@wlp2s0
Now, if all is well, disable the other network manager and enable networkd, resolvd and wpa_supplicant:
#systemctl disable netctl-auto #systemctl enable systemd-networkd #systemctl enable systemd-resolvd #systemctl enable wpa_supplicant@wlp2s0