Skip to content

Wireless

Managing WiFi using systemd-networkd

systemd-networkd is a system daemon that manages network configurations. It detects and configures network devices as they appear.
systemd-networkd's functionality can be useful for both wireless and wired networks.
This guide describes how to use systemd-networkd to configure wireless networks.

Enabling and disabling WiFi

To enable WiFi run

# networkctl up wlan0

To disable WiFi run

# networkctl down wlan0

Configuring WiFi Client

Scanning for available WiFi APs

If WiFi is enabled you can get the list of available APs by running

# iw dev wlan0 scan | grep SSID

Connecting to a protected WiFi network

Create /etc/systemd/network/80-wifi-station.network as following:

# cp /lib/systemd/network/80-wifi-station.network.example /etc/systemd/network/80-wifi-station.network

Append the following content to /etc/systemd/network/80-wifi-station.network:

[DHCPv4]
RouteMetric=9
[IPv6AcceptRA]
RouteMetric=9

Create /etc/wpa_supplicant/wpa_supplicant-wlan0.conf with the following content:

ctrl_interface=/var/run/wpa_supplicant
eapol_version=1
ap_scan=1
fast_reauth=1

To set your network's SSID and password:

# wpa_passphrase <SSID> <PASSWORD> >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

Enable Wi-Fi interface:

# networkctl up wlan0

Restart the services:

# systemctl restart systemd-networkd.service
# systemctl restart [email protected]

Wait a few seconds and then check if wlan0 is up and has an assigned IP:

# ifconfig wlan0

Check if the gateway and the DNS server are reachable:

# ping -I wlan0 192.168.1.1
# ping -I wlan0 8.8.8.8

To make the Wi-Fi connection permanent, enable the wpa_supplicant service:

# systemctl enable [email protected]

If wlan0 fails to connect, the following commands may be helpful for debugging:

# journalctl -u [email protected]
# journalctl -u systemd-networkd.service
# wpa_cli

Configuring WiFi Access Point with Hostapd

hostapd is a versatile option for setting up a WiFi access point. It offers more options and flexibility compared to other tools.
For instance, hostapd can even enable the creation of a WiFi 6 access point.

udhcpd is a suitable option for providing DHCP services alongside hostapd. It's a lightweight DHCP server that can be easily integrated with hostapd.

The following steps describe how to create an access point using hostapd and udhcpd.

Create /etc/hostapd.conf

The next step is to create /etc/hostapd.conf. The following table shows how to configure 802.11bgn, 802.11ac, and 802.11ax access points:

# AP Net Interface
interface=wlan0

# 2.4 GHz
hw_mode=g

# Enable 802.11n (Wi-Fi 4) standard
ieee80211n=1
wmm_enabled=1

# Demo was run in the US
country_code=US

# Our SSID
ssid=VARI_AP

channel=1

Configure DHCP server

# Sample udhcpd configuration file (/etc/udhcpd.conf)
# The start and end of the IP lease block
start 192.168.5.20 #default: 192.168.0.20
end 192.168.5.25 #default: 192.168.0.254
# The interface that udhcpd will use
interface wlan0

opt dns 8.8.8.8 8.8.4.4 # public google dns servers
option subnet 255.255.255.0
opt router 192.168.5.1
option lease 864000 # 10 days of seconds

Then, start hostapd and udhcpd:

ifconfig wlan0 192.168.5.1
sleep 1
systemctl restart hostapd
sleep 1
udhcpd /etc/udhcpd.conf

At this point, devices can connect and dhcp an ip address using the access point on wlan0.

Optionally configure NAT between wlan0 and eth0:

# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
# iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

and allow ip forwarding:

echo 1 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding

Now, devices connecting to the access point on wlan0 will have network access through eth0.

Testing WiFi throughput

Establish connection to WiFi network and use iperf3 tool on target and another host:

iperf3 server (on Target/Host):

# iperf3 -s

iperf3 client (on Host/Target):

Run UDP test for 30 seconds
# iperf3 -c <IP_ADDRESS_OF_IPERF_SERVER> -t 30 -u -b 0
Run TCP test for 30 seconds
# iperf3 -c <IP_ADDRESS_OF_IPERF_SERVER> -t 30