wpa_supplicant 1: Basic Wireless Connections

Feb 10, 2021

 

It is possible, and fairly easy, to manage wireless connections on a daily basis using only wpa_supplicant. Besides, it is worthwhile to know how to use wpa_supplicant, as it is part of the base system on all Linux distributions, as well as on NetBSD and on FreeBSD, and it is usually the most reliable way of managing wireless connections on devices like the Raspberry Pi, or on certain installation media.


Usually, the network SSID and the WPA password are required to connect to a password-protected Wi-Fi network. We can either edit the /etc/wpa_supplicant/wpa_supplicant.conf file manually to provide the required information, or we can use wpa_passphrase to generate a WPA PSK from the password, and append it to the configuration file. Let's try connecting to a network called "Network" with the password "password". First, we need to make the /etc/wpa_supplicant directory, if it doesn't exist. The commands are executed as root, but doas or sudo can be used instead.

[root@void: ~]# mkdir -p /etc/wpa_supplicant

Then, we need to save our network information to the configuration file. The wpa_passphrase program requires the ssid and the wpa password. The wpa_passphrase program writes to standard output. We can take this output, remove the line containing the actual password with sed, and write (or, in most cases, append in order to avoid overwriting the existing configuration) it to the configuration file by piping it into tee. Below is the command to do it.

[root@void: ~]# wpa_passphrase "Network" "password" | sed '3d' | tee -a /etc/wpa_supplicant/wpa_supplicant.conf

This is actually all the configuration needed to connect to a wireless network. This process can be repeated when it is needed to connect to another network.

The configuration file looks like this right now:

network={
    ssid="Network"
    psk="..."
}
Note: wpa_cli is a text-based frontend for interacting with wpa_supplicant, which I typically do not use. In order to be able to use it when needed (presumably as a user in the "wheel" group), the following lines should be added to the beginning of the configuration file:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
update_config=1

We can now start wpa_supplicant using the command below.

[root@void: ~]# wpa_supplicant -B -i wlp0s20f3 -c /etc/wpa_supplicant/wpa_supplicant.conf
Note: The -i option specifies the wireless interface. The interface name can be obtained using the iwconfig command. Wireless interface names on Linux typically start with "w".

Then we run a DHCP client to obtain an IP address. In this guide, dhcpcd is used.

[root@void: ~]# dhcpcd

We can use ping to make sure that the connection is established.

[root@void: ~]# ping openbsd.org

We can also enable the wpa_supplicant service (and the DHCP client, if it has not already been enabled) to start wireless networking on boot. This process depends on the distribution or the OS. Here is how to do it on Void Linux:

[root@void: ~]# ln -s /etc/sv/wpa_supplicant /var/service/
[root@void: ~]# ln -s /etc/sv/dhcpcd /var/service/