Sevenforge Sevenforge by Curtis Spencer home

The Pain of Connecting to WPA Wi-Fi on the Linux Command Line

July 28, 2009

It is sometimes useful to be able to connect to a Wi-Fi access point without a heavyweight GUI, especially when installing packages before you have a desktop environment installed or getting a lonely home server on the grid without powerline networking. I spent a while culling all the best information from various blog posts and howtos about connecting to wireless access points from the command line. I think I have come across a good mix. Once you have a GUI, it is quite a bit easier to just use wicd or Gnome's Network Manager.

I did this on Ubuntu Jaunty, but it should work on any distro with wpa_supplicant.

First you want to figure out your network hardware configuration

$ sudo lshw -C network

For example, eth1 showed up as my wireless network card's logical address. So for the future examples, eth1 is synonymous with your wireless network interface.

Next, you can get a list of the local access points using the following command:

$ sudo iwlist eth1 scan 

One example result is:

eth1 Scan completed :
Cell 01 - Address: 00:19:E3:FA:38:95
Frequency:2.462 GHz (Channel 11)
Quality:4/5  Signal level:-63 dBm  Noise level:-92 dBm
IE: IEEE 802.11i/WPA2 Version 1
    Group Cipher : TKIP
    Pairwise Ciphers (2) : CCMP TKIP
    Authentication Suites (1) : PSK
IE: WPA Version 1
    Group Cipher : TKIP
    Pairwise Ciphers (1) : TKIP
    Authentication Suites (1) : PSK
Encryption key:on
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
          11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
          48 Mb/s; 54 Mb/s

Now, we need to generate a wpa_supplicant configuration file based on the WPA access point we want to use

$ wpa_passphrase "MyEssid"

Take the resulting output and put it into /etc/wpa_supplicant.conf The file should look something like this

# reading passphrase from stdin

Now, you need to start running wpa_supplicant using that configuration file as input

$ sudo wpa_supplicant -D wext -c /etc/wpa_supplicant.conf -i eth1

Once wpa_supplicant is running you need to run dhclient to get an ip address from the access point.

$ sudo dhclient eth1

Now, if you want to save this so you don't have to do it again, put the following into your /etc/network/interfaces file:

auto eth1
  iface eth1 inet static
  wireless-essid MyEssid
  pre-up wpa_supplicant -Bw -Dwext -ieth1 -c/etc/wpa_supplicant.conf
  post-down killall -q wpa_supplicant

This is something that is a lot more complex than it should be... In writing it down, I hope that the next time I need it, I should be able to reproduce it

Fork me on GitHub