wpa_supplicant: Wireless Connections
Feb 10, 2021
Updated: May 2, 2024
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.
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.
| |
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=
psk=
}
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=//run/
We can now start wpa_supplicant
using the command below.
-i
option specifies the wireless interface.
The interface name can be obtained using the ifconfig
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.
We can use ping
to make sure that the connection is established.
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 system, and most Linux distributions will use systemctl
.
Here is how to do it on Void Linux:
Eduroam Connections
Eduroam connections require additional authentication information. We can provide it by editing the configuration file manually.
psk="..."
entry, instead of a WPA PSK generated by wpa_passphrase
.
wpa_supplicant
might not be included, but the information should still be enough for a working configuration.We add the following network configuration to the file:
network={
ssid= key_mgmt=WPA-EAP eap=TTLS phase2= identity= anonymous_identity= password= }
The password is now in the configuration file. It is a good idea to make sure that the file can only be read by the root user.
We should now be able to connect to the Eduroam network after restarting the wpa_supplicant
service.
Again, systemctl
will be used instead on most Linux distributions.
Public Networks
It is possible to join public networks without a password. Authentication might then be handled by a captive portal. In this case, setting the key_mgmt
option to NONE
works.
network={
ssid=
key_mgmt=NONE
}
Finding Networks & Connecting to Hidden Networks
If the exact name of the network is not known, a scan may be performed on the wireless interface to detect available networks using the iw
command on Linux.
|
In some cases, the SSID of the network might be hidden.
Hidden networks will obviously not appear in the scan results, but if they are known, wpa_supplicant
can connect to them with the scan_ssid
option.
network={
ssid=
scan_ssid=1
psk=
}
Prioritizing Connections
For times when multiple networks are available, we can set the priority
option to tell wpa_supplicant
which connection to prioritize. The default priority is 0. The network with the higher priority number will be prioritized.
network={
ssid=
psk=
priority=1
}
network={
ssid=
psk=
priority=9
}