Configuring WiFi on Linux using CLI

You may have always connected your laptop to WiFi using the ubiquitous little WiFi icon. How about connecting it through your terminal?
TECH ·

You may have always connected your laptop to WiFi using the ubiquitous little WiFi icon. How about connecting it through your terminal?

I had to do this after my fresh Arch install. During the installation, the packages required for wireless connections are already available in the live environment. However, you have to be careful that you install them before you exit the live environment.

Things you will need :

  1. A linux machine
  2. A terminal app
  3. wpa_supplicant and iw packages
  4. WiFi SSID (or the WiFi network name) and the password

First check the status of your wireless card -

iw dev

This should tell you the name of your wireless interface (it can be something like wlan0 or wlo1). Note the wireless interface name, you would need it when you attempt to connect. If it's already connected, you can see the SSID of the network you are connected to.

There are two ways you can connect -

  1. Pre-saving the connection details using wpa_passphrase
$ wpa_passphrase MY_WIFI_NETWORK my_passw0rd > /path/to/connection/file
$ wpa_supplicant -B -i wlan0 -c /path/to/connection/file
  1. Entering the connection details manually with wpa_cli
$ echo 'ctrl_interface=/run/wpa_supplicant' > /path/to/connection/file
$ echo 'update_config=1' >> /path/to/connection/file
$ wpa_supplicant -B -i wlan0 -c /path/to/connection/file
$ wpa_cli
	> scan
	> add_network
	> set_network 0 ssid "MY_WIFI_NETWORK"
	> set_network 0 psk "my_passw0rd"
	> enable_network 0

You may need to run dhcpcd wlan0 for IP address assignment. And then you should be connected.