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.)
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
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)
https://www.linux.com/learn/how-configure-wireless-any-linux-desktop
to scan for essid
iwlist wlan0 scanning | grep -i ‘essid’
nice work thxx :D
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.
Thankd you helped me a lot!
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
You should issue the iwconfig command with sudo
“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!
Hi,
I’m on Imagination Meta platform (embedded linux), and dhclient is not available. Is there any other command to get the ip address?
Sometimes the command dhcpcd works also, or you could try installing the pakkage usingyour package manager.
Very good article! Thank you very much!
You’re awesome! Thank you so much, it worked perfectly.
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…
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.
oh, yeah, it also says: “wmaster0: unknown hardware address type 801”
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!!!
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!
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?
Thx bro.. this worked for me..
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
Oops! I had installed wireless tools, but…… not on this machine! Hence the ‘…not found’.
http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html
Took me 30 seconds to find. Seriously…
* 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
.
how to find wifi devive module name
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
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?
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
this saved my bacon. thank you, thank you, thank you.
Thanks a lot! I use a usb wifi nic and this helped a lot :D
Any one know how to restart the wireless router through command prompt?
You can’t do that from another device.
This was a great tip. Thanks for writing it up.
Thanks a lot, this will definitely help me in times of need or running in console-only. :)
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.
gB: Thanks for pointing that out. I made the correction in the article.
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)
Script for WinXP would be great.
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.
There is a way to do something like this on windows?
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.