Connect to a wireless network via command line

Jack Wallen
Apr 14, 2009
Updated • Nov 28, 2012
Network
|
51

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.

Advertisement

Previous Post: «
Next Post: «

Comments

  1. Marta said on January 19, 2017 at 12:59 am
    Reply

    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)

  2. asd said on September 8, 2015 at 8:43 am
    Reply

    to scan for essid
    iwlist wlan0 scanning | grep -i ‘essid’

  3. khilio said on May 8, 2014 at 10:36 pm
    Reply

    nice work thxx :D

  4. charles said on April 13, 2014 at 5:22 am
    Reply

    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.

  5. bruno said on December 30, 2013 at 11:47 pm
    Reply

    Thankd you helped me a lot!

  6. Sujay M said on November 1, 2012 at 9:05 pm
    Reply

    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

    aa@aa:~$ 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

    1. adhi said on November 12, 2013 at 2:33 am
      Reply

      You should issue the iwconfig command with sudo

  7. Threeck said on August 15, 2012 at 12:07 pm
    Reply

    “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!

  8. meta said on July 31, 2012 at 2:55 pm
    Reply

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

    1. xouns said on November 2, 2012 at 5:19 pm
      Reply

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

  9. abellong said on July 15, 2012 at 7:52 pm
    Reply

    Very good article! Thank you very much!

  10. Val said on June 16, 2012 at 1:41 am
    Reply

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

  11. that one guy said on October 16, 2011 at 3:13 pm
    Reply

    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…

  12. viv said on September 29, 2011 at 1:43 pm
    Reply

    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.

  13. Brian said on August 27, 2011 at 1:57 pm
    Reply

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

  14. Brian said on August 27, 2011 at 1:56 pm
    Reply

    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!!!

  15. Brian said on August 27, 2011 at 1:52 pm
    Reply

    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!

  16. Polonium said on July 12, 2011 at 8:54 pm
    Reply

    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?

  17. Avinash said on March 13, 2011 at 6:34 am
    Reply

    Thx bro.. this worked for me..

  18. muoiba said on December 29, 2010 at 5:07 am
    Reply

    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

  19. Chris said on June 7, 2010 at 11:10 pm
    Reply

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

  20. DaVince said on June 7, 2010 at 10:21 pm
    Reply

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

    Took me 30 seconds to find. Seriously…

  21. Chris said on June 7, 2010 at 10:14 pm
    Reply

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

  22. JB said on June 3, 2010 at 2:59 pm
    Reply

    how to find wifi devive module name

  23. root9885 said on April 13, 2010 at 7:21 am
    Reply

    root@ubtplug:~# 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

  24. root9885 said on April 13, 2010 at 7:19 am
    Reply

    root@ubtplug:~# 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?

  25. vev said on February 25, 2010 at 3:57 pm
    Reply

    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

  26. Josh said on January 23, 2010 at 9:52 pm
    Reply

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

  27. Tom said on December 26, 2009 at 7:47 am
    Reply

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

  28. sree said on September 28, 2009 at 2:35 pm
    Reply

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

    1. LauritzT said on June 29, 2016 at 12:36 pm
      Reply

      You can’t do that from another device.

  29. hohoboy said on August 12, 2009 at 11:57 pm
    Reply

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

  30. DaVince said on May 10, 2009 at 7:14 pm
    Reply

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

  31. max said on April 24, 2009 at 8:58 pm
    Reply

    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.

  32. jack said on April 19, 2009 at 9:03 pm
    Reply

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

  33. gB said on April 17, 2009 at 9:56 am
    Reply

    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)

  34. RSWRC said on April 15, 2009 at 9:25 am
    Reply

    Script for WinXP would be great.

  35. RogueSpear said on April 14, 2009 at 10:17 pm
    Reply

    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.

  36. FitzgeraldReno said on April 14, 2009 at 10:10 pm
    Reply

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

  37. RogueSpear said on April 14, 2009 at 8:33 pm
    Reply

    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.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.