I know, the first thing you are asking is “Why would I want to have to connect to a wireless network from the command line?” To that question I can give you a simple answer…What if you want to create a script to run that will bring up your wireless network as soon as your desktop is loaded? You could get really complex and create a start up script to load a desktop based on what wireless access point you needed to join. That’s one of the beauties of Linux, if you can dream it, you can do it.
But I am getting too far ahead of myself. Let’s get back to the basics shall we? First I am going to assume that your wireless card was detected by your distribution and has the proper drivers loaded. With that accomplished you will need to have the following tools:
- ifconfig: Enable your wireless device.
- iwlist: List the available wireless access points.
- iwconfig: Configure your wireless connection.
- dhclient: Get your IP address via dhcp.
The first command you need to use is ifconfig. With this command you are going to enable your wireless device. Most likely your device will be called wlan0. So in order to enable this you would enter the command (as root):
ifconfig wlan0 up
You won’t see any feedback unless there is a problem.
The next step is to scan for your wireless network to make sure it is available. Do this with the following command:
iwlist wlan0 scan
With this command you will see output like the following:
Cell 01 - Address: 00:21:43:4E:9B:F0
ESSID:"HAIR STROBEL"
Mode:Master
Channel:5
Frequency:2.432 GHz (Channel 5)
Quality=100/100? Signal level:-45 dBm? Noise level=-95 dBm
Encryption key:on
IE: WPA Version 1
Group Cipher : TKIP
Pairwise Ciphers (1) : TKIP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : TKIP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 22 Mb/s
6 Mb/s; 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s
36 Mb/s; 48 Mb/s; 54 Mb/s
Extra:tsf=000002f1d9be01b7
So you know this network is available. From the above output you can also see this network is employing WPA2, so you will need a passkey. If you don’t know that passkey, you are out of luck (which would be the case no matter if you were using a front end in Linux, Windows, or Mac.)
Now it’s time to configure your connection. To do this issue the command:
iwconfig wlan0 essid NETWORK_ID key WIRELESS_KEY
Where NETWORK_ID is the ESSID of the network with which you want to connect and WIRELESS_KEY is the security key needed to connect to the wireless access point.
Note: iwconfig defaults to using a HEX key. If you want to use an ascii key you will have to add the “s:” prefix to your key like so:
iwconfig wlan0 essid NETWORK_ID key s:WIRELESS_KEY
Now that you have your configuration set, it’s time to get an IP address with the help of dhclient. Issue the command:
dhclient wlan0
If no output is reported there are no errors. You should now be up and running.
Make it a script
Of course who wants to type out all of those commands. Instead of doing this you could create a script for this like so:
#! /bin/bash
ifconfig wlan0
iwconfig wlan0 essid NETWORK_ID key WIRELESS_KEY
dhclient wlan0
Where NETWORK_ID is the actually essid of the network and WIRELESS_KEY is the security key for that network. Save this script with the filename wireless_up.sh and then make this script executable with the command:
chmod u+x wireless_up.sh
You can make this a global command by placing this script in /usr/local/bin. You can now issue the command wireless_up.sh from anywhere in your directory structure and it will run, connecting you to the configured wireless access point.
If you frequent many wireless access points you can create a script for each one giving them each unique names. By doing this, when you need to connect to a specific access point, just run the script associated with that access point and you’re good to go.

Burn CDs From Command Line
Easy Debian Wireless Connections with Wifi-Wiz
Wireless Network Scanner inSSIDer
As always, very nice article. I’ve been Googling for quite some time now on a couple of issues and unfortunately am not coming up with answers. Hoping someone may be able to offer some guidance. First issue is that I have an Intel 4965agn card. Both at work and home I only connect to a 5Ghz 802.11n network. In Windows I’m able to turn off the 2.4Ghz radio, but I’ve yet to find a way to do this in Linux. The second issue is that at work I use the wireless on one network and my wired on a completely separate network. I can’t seem to find a way to keep both interfaces up at the same time. All of my search efforts on this particular issue inevitably result in a bunch of hits revolving around automatic switching from one to the other or turning your computer into a router.
Any help or even just a link to something would be greatly appreciated.
There is a way to do something like this on windows?
Well to be a little more precise, the Intel wireless driver for Windows allows you to do this. I did download the latest build of Intel’s 4965 driver for Linux, and I think I implemented it properly, but I just don’t see how to do this. I hope it isn’t something completely obvious that I should have noticed immediately.
Script for WinXP would be great.
Just for somebody reading this who may not know any better and come along later and copy and paste the commands as is and then it doesn’t work, or you get an error saying command not found. The last command should read chmod u+x wireless_up.sh
(No disrespect meant. Just trying to help people who maybe new to linux. I still have a long ways to go myself)
gB: Thanks for pointing that out. I made the correction in the article.
holy crap thank you for writing this, i’ve been trying to figure out for too long how to connect via command line (downloaded something that messed up too many configs and couldn’t get updates cause i couldn’t connect to net). also, just to mention, if the line:
Encryption key
is off
you don’t have to load a password. Just a heads up. Thanks again.
Thanks a lot, this will definitely help me in times of need or running in console-only. :)
This was a great tip. Thanks for writing it up.
Any one know how to restart the wireless router through command prompt?