Connect to a wireless network via command line

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.)

Read also:  Wireshark 2.4 released

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.

Please share this article

Facebooktwittergoogle_plusredditlinkedinmail



Responses to Connect to a wireless network via command line

  1. RogueSpear April 14, 2009 at 8:33 pm #

    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.

  2. FitzgeraldReno April 14, 2009 at 10:10 pm #

    There is a way to do something like this on windows?

  3. RogueSpear April 14, 2009 at 10:17 pm #

    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.

  4. RSWRC April 15, 2009 at 9:25 am #

    Script for WinXP would be great.

  5. gB April 17, 2009 at 9:56 am #

    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)

  6. jack April 19, 2009 at 9:03 pm #

    gB: Thanks for pointing that out. I made the correction in the article.

  7. max April 24, 2009 at 8:58 pm #

    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.

  8. DaVince May 10, 2009 at 7:14 pm #

    Thanks a lot, this will definitely help me in times of need or running in console-only. :)

  9. hohoboy August 12, 2009 at 11:57 pm #

    This was a great tip. Thanks for writing it up.

  10. sree September 28, 2009 at 2:35 pm #

    Any one know how to restart the wireless router through command prompt?

    • LauritzT June 29, 2016 at 12:36 pm #

      You can't do that from another device.

  11. Tom December 26, 2009 at 7:47 am #

    Thanks a lot! I use a usb wifi nic and this helped a lot :D

  12. Josh January 23, 2010 at 9:52 pm #

    this saved my bacon. thank you, thank you, thank you.

  13. vev February 25, 2010 at 3:57 pm #

    Thanks! I did however needed to modify the script a little bit, for the low signal connections:

    before we 'dhclient wlan0' I added a 'sleep 10' that gave the wireless card enough time to establish a connection. I hope it helps someone

  14. root9885 April 13, 2010 at 7:19 am #

    [email protected]:~# iwconfig wlan1 essid dlink key s:2208tzzgist
    Error for wireless request "Set Encode" (8B2A) :
    SET failed on device wlan1 ; Invalid argument.

    how to get it?

  15. root9885 April 13, 2010 at 7:21 am #

    [email protected]:~# iwlist wlan1 scan
    wlan1 Scan completed :
    Cell 01 - Address: 1C:AF:F7:1A:75:C4
    Channel:6
    Frequency:2.437 GHz (Channel 6)
    Quality=55/70 Signal level=-55 dBm
    Encryption key:on
    ESSID:"dlink"
    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
    18 Mb/s; 36 Mb/s; 54 Mb/s
    Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
    Mode:Master
    Extra:tsf=00000002c718a153
    Extra: Last beacon: 476ms ago
    IE: Unknown: 0005646C696E6B
    IE: Unknown: 010882848B961224486C
    IE: Unknown: 030106
    IE: Unknown: 2A0104
    IE: Unknown: 32040C183060
    IE: Unknown: 2D1AEE1117FF000000010000000000000000000000000C0000000000
    IE: Unknown: 3D1606070700000000000000000000000000000000000000
    IE: Unknown: 3E0100
    IE: WPA Version 1
    Group Cipher : TKIP
    Pairwise Ciphers (2) : TKIP CCMP
    Authentication Suites (1) : PSK
    IE: IEEE 802.11i/WPA2 Version 1
    Group Cipher : TKIP
    Pairwise Ciphers (2) : TKIP CCMP
    Authentication Suites (1) : PSK
    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
    IE: Unknown: 0B05020084127A
    IE: Unknown: 7F0101
    IE: Unknown: DD07000C4304000000
    IE: Unknown: 0706474220010D10
    IE: Unknown: DD1E00904C33EE1117FF000000010000000000000000000000000C0000000000
    IE: Unknown: DD1A00904C3406070700000000000000000000000000000000000000

  16. JB June 3, 2010 at 2:59 pm #

    how to find wifi devive module name

  17. Chris June 7, 2010 at 10:14 pm #

    * iwlist: List the available wireless access points.
    * iwconfig: Configure your wireless connection

    Sounds great! Where do I find these?

    S50:/home/chris# iwlist
    bash: iwlist: command not found
    S50:/home/chris# iwconfig
    bash: iwconfig: command not found
    .

  18. DaVince June 7, 2010 at 10:21 pm #

    http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html

    Took me 30 seconds to find. Seriously...

  19. Chris June 7, 2010 at 11:10 pm #

    Oops! I had installed wireless tools, but...... not on this machine! Hence the '...not found'.

  20. muoiba December 29, 2010 at 5:07 am #

    step iwlist wlan0 scan
    mycomputer result :

    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

    Would you please explain to me?

    Thank you .

    Best regards
    battran

  21. Avinash March 13, 2011 at 6:34 am #

    Thx bro.. this worked for me..

  22. Polonium July 12, 2011 at 8:54 pm #

    Hi!
    Excellent guide. I tried to do the final iwconfig, but the result is iwconfig: unknown command (my Essie). I tried using the s: prefix as well. What is wrong?

  23. Brian August 27, 2011 at 1:52 pm #

    After I type 'dhclient wlan0' I get some text and it looks like its trying to connect, but isn''t working. It says "DHCPDISCOVER on wlan0 to 255.255.255.255 port 67 interval 3" and this text repeats every few seconds with a different interval number. After 5 times, it says "No DHCPOFFERS received" and "No working leases in persistent database - sleeping".

    Any suggestions how to connect to the wireless router?

    Thanks!

  24. Brian August 27, 2011 at 1:56 pm #

    for me this shows text that indicates that its trying to connect:

    after 5 trys it times out and says:

    "No DHCOFFERS received"
    "No working leases in persistent database - sleeping"j

    ???

    Any suggestions on how to connect to my wireless router?

    Thanks!!!

  25. Brian August 27, 2011 at 1:57 pm #

    oh, yeah, it also says: "wmaster0: unknown hardware address type 801"

  26. viv September 29, 2011 at 1:43 pm #

    Do you also know how to enable Ad-hoc wireless on HP TouchPad?

    There is workaround all over precentral.net but that is not a solutiion.

  27. that one guy October 16, 2011 at 3:13 pm #

    So, just taking a look at this and going off of experience....this script shouldn't work in this example. Correct me if I'm wrong, but iwconfig can only authenticate an interface using open or WEP encryption. I believe you'd need to modify wpa_supplicant.conf to contain the relevant data for your network (essid, encryption/key) and then use wpa_supplicant to authenticate...

  28. Val June 16, 2012 at 1:41 am #

    You're awesome! Thank you so much, it worked perfectly.

  29. abellong July 15, 2012 at 7:52 pm #

    Very good article! Thank you very much!

  30. meta July 31, 2012 at 2:55 pm #

    Hi,
    I'm on Imagination Meta platform (embedded linux), and dhclient is not available. Is there any other command to get the ip address?

    • xouns November 2, 2012 at 5:19 pm #

      Sometimes the command dhcpcd works also, or you could try installing the pakkage usingyour package manager.

  31. Threeck August 15, 2012 at 12:07 pm #

    "If no output is reported there are no errors. You should now be up and running."

    --this is sort of misleading, because after typing [dhclient wlan0] there will definitely be an output, and some users (especially those new to linux) might think that there is an error. It should be changed to something like:

    "If the output says something like [bound to 192.168.x.x -- renewal in xx seconds] there are no errors and you have successfully connected to the wireless network."

    Thanks! this just might help some people who are starting out with linux. Cheers!

  32. Sujay M November 1, 2012 at 9:05 pm #

    Hi when i do iwlist wlan0 scan i get

    Cell 04 - Address: 4C:60:DE:CD:CA:6A
    Channel:1
    Frequency:2.412 GHz (Channel 1)
    Quality=34/70 Signal level=-76 dBm
    Encryption key:off
    ESSID:"DOTARDS"
    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
    9 Mb/s; 12 Mb/s; 18 Mb/s
    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
    Mode:Master
    Extra:tsf=0000000118bb3176
    Extra: Last beacon: 12848ms ago
    IE: Unknown: 0007444F5441524453
    IE: Unknown: 010882848B960C121824
    IE: Unknown: 030101
    IE: Unknown: 050401020000
    IE: Unknown: 2A0104
    IE: Unknown: 32043048606C
    IE: Unknown: DD0600E04C020160
    IE: Unknown: DD0E0050F204104A0001101044000102

    [email protected]:~$ iwconfig wlan0 essid DOTARDS
    Error for wireless request "Set ESSID" (8B1A) :
    SET failed on device wlan0 ; Operation not permitted.

    Please help me in connecting to the network

    • adhi November 12, 2013 at 2:33 am #

      You should issue the iwconfig command with sudo

  33. bruno December 30, 2013 at 11:47 pm #

    Thankd you helped me a lot!

  34. charles April 13, 2014 at 5:22 am #

    So I did the, dhclient, and got,,,,ls: cannot access /var/run/netconfig//wlan0/: no such file or directory. Is there a file or directory I need to add to my system.

  35. khilio May 8, 2014 at 10:36 pm #

    nice work thxx :D

  36. asd September 8, 2015 at 8:43 am #

    to scan for essid
    iwlist wlan0 scanning | grep -i 'essid'

  37. Marta January 19, 2017 at 12:59 am #

    thank you for the nice article. Its pretty good an easy to follow for an beginner.
    But can you please write somewhere that it isnt working with WPA/WPA2
    (an article about that would quite nice)

Leave a Reply