Configure a wireless connection with WPA authentication [23 Dec 2009]

My Machine

  • Amilo Si 2636
  • wireless network card: Intel PRO/Wireless 4965
  • Debian Squeeze (testing)

Configure network card

This procedure is very similar for other network cards of the same family.
First install this package:

# apt-get install firmware-iwlwifi

This is the proprietary firmware for your network card to work. Load the relative kernel module:

# modprobe iwl4965

Then check that wireless-tools package is present in your system.

# dpkg -l wireless-tools

If not, install it with apt-get install.
Now you have to enable your wirelss network card. Check with the ifconfig command your wireless interface name, should be something like wlan[number]. Mine is wlan0. Type (my example is with wlan0):

# ifconfig wlan0 up

Now you have to check if your wireless card works. Type:

# iwconfig wlan0

to get wireless interface infos and

# iwlist wlan0 scan

to get a scan of the available wireless networks on the air.

Configure WPA access

As the man page explains, iwconfig does not support WPA authentication. So you can't connect to a WPA protected wireless LAN just typing

# iwconfig wlan0 essid "myNetwork" key s:passphrase

You have to use the wpa supplicant tool.

# apt-get install wpasupplicant

Now add your key to /etc/wpa_supplicant/wpa_supplicant.conf file.

# wpa_passphrase mynetwork passphrase >> /etc/wpa_supplicant/wpa_supplicant.conf

Remember that if you use spaces or special chars in your passphrase you have to protect them with a backslash each one before (\).

Finally you are ready to connect to your network.
Type:

# iwconfig wlan0 essid "mynetwork"

to set the right ESSID, and

# wpa_supplicant -iwlan0 -c/etc/wpa_supplicant/wpa_supplicant.conf

to let wpa_supplicant authenticate you and connect to the access point (without spaces between the two options -i and -c).

A simple script

Here a simple script I wrote to connect/disconnect to my WPA networks. Just change 'wlan0' to your own interface and place it somewhere like /usr/local/bin (or wherever in your $PATH list).

#!/bin/bash
# (C) 2009 Daniele Zanotelli
#
# This is free software. Licence: GPLv3>=

# A simple script to connect to my WPA wireless lans.

IFACE='wlan0'
ESSID="$1"
ACTION="$2"

if [ -n $ESSID ]; then
    if [ -f /etc/wpa_supplicant/wpa_supplicant.conf ]; then
	TEST=$(cat /etc/wpa_supplicant/wpa_supplicant.conf|grep \"$ESSID\")
    else
	echo "Error: Cannot find /etc/wpa_supplicant/wpa_supplicant.conf file."
	echo "Exit."
	exit 1
    fi
else
    TEST=
fi

if [ -z $TEST ]; then
    echo "Error: Missing ESSID or bad command line."
    echo "Usage: $0 essid up|down"
    exit 1
fi

case $ACTION in
    up)
    
    # Enabling wlan interface
    ifconfig $IFACE up

    echo 'Attempting to connect to "$ESSID"...'
    
    # set up wlan essid and run wpa_supplicant in background
    iwconfig $IFACE essid "$ESSID"
    wpa_supplicant -B \
	           -i$IFACE \
	           -c/etc/wpa_supplicant/wpa_supplicant.conf
    # check for DHCP to get an IP address
    dhclient $IFACE
    ;;
    
    down)

    echo "Attempting to switch off wpa_supplicant..."
    wpaPID=$(ps ax|grep wpa_supplicant|cut -d' ' -f2|head -1)
    if [ -n $wpaPID ] ; then
	kill -15 $wpaPID
	echo "done."
    else
        echo "No wpa_supplicant istances found. Exit."
    fi
    ;;
    
    *)
    
    echo "Error: Usage: $0 essid up|down"
    exit 1
    ;;
esac

exit