How to use wpa_supplicant to connect to a wireless network

Introduction

There are various ways to connect to a network e.g. using GUI. A GUI lists all the available networks/ SSIDs and we just have to click, enter password(if the connection is secured) and done. The other ways to connect include NetworkManager(a daemon), nmcli(a cli to control NetworkManager), wpa_supplicant(a relatively low level tool).

wpa_supplicant is a cross platform free software implementation of an IEEE 802.11i supplicant. A supplicant is simply an entity at one end of a LAN. The other end of the LAN has an authenticator that authenticates a connection to it.

To use wpa_supplicant, we need to install wpa_supplicant(e.g. for arch linux, pacman -S wpa_supplicant) which installs two more utilities with it i.e. wpa_passphrase and wpa_cli. wpa_cli is the frontend tool of wpa_supplicant while as wpa_passphrase is used to create a configuration file which contains the network details e.g. the password.

Connecting to a network

Before following the steps below, make sure your network interface is up.
sudo ip link
Search for UP in the output corresponding to your network interface(e.g. wlan0).

  • Scan SSIDs/ Networks:
    iw scan wlan0

    Replace wlan0 with your network interface(use ip link to find yours). Note down the SSID name from the above command.
    TIP: iw scan wlan0 | grep SSID will return only SSIDs only which is what we require here.
  • Create passphrase configuration:
    wpa_passphrase myssid | sudo tee /etc/wpa_supplicant.conf, write the SSID password and press enter.

    Replace myssid with the SSID that you want to connect to.
    This will create a file /etc/wpa_supplicant.conf. We will use this file in our wpa_supplicant command to connect to our network. sudo is required with tee command because the file is being created in root owned directory. We can however create this file in any directory. tee simply puts the output of wpa_supplicant in the given.conf file.
  • Start wpa_supplicant daemon:
    sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf

    This says that start wpa_supplicant in the background(B flag) using wlan0 network interface and /etc/wpa_supplicant.conf as the configuration file. With an extra D flag, we can set the driver to be used e.g. sudo wpa_supplicant -B -D nl80211 -i wlan0 -c /etc/wpa_supplicant.conf. This will use nl80211 driver rather than the default being picked.
    At this point, you are connected but no ip address has been assigned. Use iw wlan0 link to confirm
  • Obtain an ip address:
    sudo dhclient wlan0.
    You are now connected to the network. Use ping command to confirm.

Leave a Reply