Managing Wireless with systemd-networkd and wpa-supplicant

Tango-document-new.png

Tango-document-new.png

This article is a stub.

Notes: Duplication. Combining DHCP client with Wi-Fi management tools is trivial, no need to have separate pages for every possible combination. (Discuss)

Tango-two-arrows.png

Tango-two-arrows.png

This article or section is a candidate for merging with systemd-networkd.

Notes: Provide links to the above forum thread and redirect this page to systemd-networkd. (Discuss)

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.

Note: This page assumes that you are using DHCP to acquire an IP address and that the persistent name of your wireless interface is wlp2s0.

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

Note: The wpa_supplicant@.service uses the configuration file /etc/wpa_supplicant/wpa_supplicant-<interface>.conf rather than the standard /etc/wpa_supplicant/wpa_supplicant.conf. Be aware of this difference when using generic wpa_supplicant guides and reference materials.

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

Warning: In November, 2014 concerns were raised about potential poisoning of systemd-resolved's DNS cache. See this thread.

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

See also