WIFI from the CLI in Linux using wpa_supplicant
I rarely need to configure a wireless connection from the CLI, but when I do it seems like I can't find a concise set of notes on how to it. This page shows some notes I've found on how to successfully configure wifi using the linux CLI.
References
- Connect to a WPA2 Enterprise network with wpa_supplicant... [gist.github.com]
- Using WPA_Supplicant to Connect to WPA2 Wi-fi from Terminal on Ubuntu 16.04 Server [linuxbabe.com]
- Setting WiFi up via the command line [raspberrypi.org]
Prerequisites
-
Ensure wpasupplicant is installed
-
Ensure rfkill is installed
-
Run
rfkill list
to check if your wireless card is blocked- If it comes back with
Soft blocked: yes
run this:rfkill unblock wifi
- If it comes back with
-
Run
iwconfig
to find your wifi interface name (wlan0
in my case) -
Ensure your wifi card is functional by performing a wireless network scan:
iwlist wlan0 scan
(This should return a list of wifi network names detected in the vicinity)
At this point your system should be ready to connect to either a WPA/WPA2 or WPA2-Enterprise wireless network.
Setup WPA/WPA2 with preshared key
This is the most common configuration for home wireless routers and likely applies if you are not connecting to a school or work/corporate network. Replace WIFINETWORK
with the name of your wireless network and WIFIPASSWORD
with your network password. You'll also want to replace wlan0
with the name of your WIFI interface name:
- wpa_passhprase WIFINETWORK WIFIPASSWORD | sudo tee /etc/WIFINETWORK.conf
- wpa_supplicant -B -c /etc/WIFINETWORK.conf -i wlan0
- Wait a few seconds up to a minute...
- iwconfig
You should see that your wlan interface is connected to ESSID:"WIFINETWORK"
(assuming all went well)
While you are associated with a wireless network AP, you don't get an IP Address automatically to get an IP Address:
dhclient wlan0
- ip a
- This should show an IP Address
Setup a WPA2-Enterprise connection
To connect to an enterprise network (one that gives you both a username and password instead of just a password), follow these steps:
- Be sure you know your wifi network interface name (mine is
wlan0
, find yours using iwconfig) - Create a configuration file (for example: /etc/wificonnection.conf) and place these contents within it:
## Connect to a WPA2 Enterprise network using this command: ## wpa_supplicant -B -i wlan0 -c /etc/wificonnection.conf ## Then run dhclient wlan0 to get an IP Address network={ scan_ssid=1 eap=PEAP ssid="YOUR_SSID" key_mgmt=WPA-EAP identity="YOUR_USERNAME" password="YOUR_PASSWORD" phase1="peaplabel=0" phase2="auth=MSCHAPV2" }
- wpa_supplicant -B -i wlan0 -c /etc/wificonnection.conf
- Wait a few seconds to a minute for the connection to take place...
- dhclient wlan0
At this point you should be connected to the network with an IP Address