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
Replacewlan0with your network interface(useip linkto find yours). Note down the SSID name from the above command.
TIP:iw scan wlan0 | grep SSIDwill 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.
Replacemyssidwith theSSIDthat you want to connect to.
This will create a file/etc/wpa_supplicant.conf. We will use this file in ourwpa_supplicantcommand to connect to our network.sudois required withteecommand because the file is being created in root owned directory. We can however create this file in any directory.teesimply puts the output ofwpa_supplicantin the given.conffile. - Start wpa_supplicant daemon:
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
This says that startwpa_supplicantin the background(Bflag) usingwlan0network interface and/etc/wpa_supplicant.confas the configuration file. With an extraDflag, 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 usenl80211driver rather than the default being picked.
At this point, you are connected but no ip address has been assigned. Useiw wlan0 linkto confirm - Obtain an ip address:
sudo dhclient wlan0.
You are now connected to the network. Usepingcommand to confirm.