Ubuntu Command Line – How To Connect To Wi-Fi WPA/WPA2

 

First and foremost, you will need to have wpa-supplicant installed, otherwise it won’t work. Ubuntu does installs this be default, but to be on the safe side you can install the required package with this command:

 

sudo apt-get install wpasupplicant

 

So far so good, next up we’ll be creating 2 files alongside eachother, the first being:

wireless-wpa.sh

#!/bin/sh
iface=wlan0

#shut down interface
ifconfig $iface down

#set ad-hoc/management of wireless device
iwconfig $iface mode Managed

#enable interface
ifconfig $iface up

#stop any persistent wireless wpa2 sessions
killall wpa_supplicant

#apply WPA/WPA2 personal settings to device
wpa_supplicant -B -Dwext -i $iface -c ./wireless-wpa.conf -dd

#obtain an IP address
dhclient $iface

 

Be extra sure to corrent the have the correct iface value in your script!

Our second file would be:

wireless-wpa.conf

# config file using WPA/WPA2-PSK Personal key.

ctrl_interface=/var/run/wpa_supplicant

network={
ssid=”my_router_id”
scan_ssid=1
key_mgmt=WPA-PSK
psk=”1234567890″
}

 

Should you have absolutely no clue about the name of your Router’s or Access Point’s ESSID, issue this command to have the Wi-Fi card look around for available wireless networks (wlan0 is the name of your wireless card as per ifconfig):

 

sudo iwlist wlan0 scan

 

Now issue the following commands:

 

sudo chmod 755 wireless-wpa.sh
sudo chmod 644 wireless-wpa.conf

 

Finally, now bring up the connection by running:

 

sudo ./wireless-wpa.sh

 

This is pretty useful if you do not wish to deal with /etc/network/interfaces and soforth.

The original guide can be found by clicking here.