Ubuntu: Connect To An Access Point From The Command Line

Running a headless server presents several challenges, networking is not one of them. In this case I have a headless server with a USB wireless adapter and I wish to connect to the nearest access point.

The wireless interface has to be activated, in this case I am using Ubuntu and the interface name is wlan0, keep in mind that the name may change on Backtrack the same wireless adapter appears under a different name rusb0.

Activate the wireless interface

Let’s bring the interface wlan0 up.

ifconfig wlan0 up

Scan for the wireless network you wish to connect.

sudo iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: 00:19:5B:0C:30:CA
                    Frequency:2.437 GHz (Channel 6)
                    Quality=47/100  Signal level:-76 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              12 Mb/s; 24 Mb/s; 36 Mb/s; 9 Mb/s; 18 Mb/s
                              48 Mb/s; 54 Mb/s
                    Extra: Last beacon: 28ms ago

After we take note of the target ESSID we associate our wireless adapter to the AP. In this case the AP has no encryption.

iwconfig wlan0 essid "wireless"

Last but no least we need to request an IP address from the server, if using static IP ignored this command.

sudo dhclient wlan0
There is already a pid file /var/run/dhclient.pid with pid 9738
killed old client process, removed PID file
Internet Systems Consortium DHCP Client V3.1.1
Copyright 2004-2008 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

wmaster0: unknown hardware address type 801
wmaster0: unknown hardware address type 801
Listening on LPF/wlan0/00:1f:c6:18:66:fd
Sending on   LPF/wlan0/00:1f:c6:18:66:fd
Sending on   Socket/fallback
DHCPREQUEST of on wlan0 to port 67
DHCPACK of from
bound to -- renewal in 40459 seconds.

In case the AP requires a key, issue the following.

iwconfig wlan0 key ABCD-ABCD-ABCD-ABCD

Now just check the wireless adapter status.

  • Mingo

    Thanks alot for this quick how-to.

    VERY helpful

  • mahanth

    This was helpful indeed. Is there a way to dissociate with an AP permanently. Whenever I connect to a new access point, my computer automatically reconnects to the old access point.

  • Josh

    Connect to an access point one-liner:
    iwconfig wlan0 essid [ESSID] key [KEY] && dhclient wlan0