How to connect to a wireless network from the Ubuntu command line

Why use a friendly GUI when there’s cryptic shell commands out there?

Phoenix Coffee offers free wireless access without an access key and they broadcast their ESSID. Here’s how I connect:

$ sudo iwlist eth1 scan # eth1 is my wireless card.
eth1 Scan completed :
Cell 01 - Address: 0A:1D:19:15:C2:C1
ESSID:"bestcoffee"
Mode:Master
Channel:5
Frequency:2.432 GHz (Channel 5)
Quality=88/100 Signal level=-44 dBm Noise level=-81 dBm
Encryption key:off
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; 24 Mb/s; 36 Mb/s
48 Mb/s; 54 Mb/s
Extra:tsf=0000003befa89182

That scan lists every available access point. The important piece of information to extract here is the ESSID. Now I set my wireless card up to connect to that ESSID:

$ sudo iwconfig eth1 essid "bestcoffee"

And finally, I tell my card to request an IP address from the router:

$ sudo dhclient eth1 # gimme an IP address plz!
There is already a pid file /var/run/dhclient.pid with pid 6911
killed old client process, removed PID file
Internet Systems Consortium DHCP Client V3.0.6
Copyright 2004-2007 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/eth1/00:1c:bf:96:7e:21
Sending on LPF/eth1/00:1c:bf:96:7e:21
Sending on Socket/fallback
DHCPREQUEST of 192.168.1.112 on eth1 to 255.255.255.255 port 67
DHCPACK of 192.168.1.112 from 192.168.1.1
bound to 192.168.1.112 -- renewal in 251284 seconds.

I can type these three commands way faster than waiting for the GUI to fire up. Here they are again, without all the output:

$ sudo iwlist eth1 scan
$ sudo iwconfig eth1 essid "bestcoffee"
$ sudo dhclient eth1

Sometimes, I need to connect to a network with a hidden ESSID. That’s just as easy. I just configure the ethernet card to connect to anything:

$ sudo iwconfig eth1 essid any # any is a keyword, not the name of an ESSID.

Finally, some networks require an access key. Sometimes, people can give you the human-friendly version, and you can type that in like this:

$ sudo iwconfig eth1 key s:password # translates to the hex for me.

Note the s: in front. That translates what I type into the hex jibberish.

Other times, people insist on giving you the goofy string of hex digits, so you can set it like this:

$ sudo iwconfig eth1 key ACDB-1234-1234-EFG2

46 thoughts on “How to connect to a wireless network from the Ubuntu command line

  1. i am trying to connect to my wireless network using the command line.
    the steps that are followed by me are given bellow

    current status : connected to the wlan0 with ESSID “mydesk”
    (where mydesk is the name of the network that i am connected to)

    use the commands

    #ifconfig wlan0 down ///////make the wlan0 interface down, now a this stage i am disconnected from any network available
    #ifconfig wlan0 uo ///////make the wlan0 interface up

    now my interface is up, but i am not connected to any netwrok, there are many wireless lans available and i want to connect to a particular one wireless lan using the console, so i use the following command

    #iwconfig wlan0 essid “mydesk”
    (where mydesk is the name of the network that i want to connected to)

    now at this stage i expect my interface to connect to the wireless lan with the ESSID “mydesk”, but it is not the case, my browser still shows “no network connection”

    can you please guide me how can i connect to a particular wireless network using the command prompt only.

    i have used the dhclient wlan0 command as well and it gives the following output.

    [asheikh ~]# ifconfig wlan0 down
    [asheikh ~]# ifconfig wlan0 up
    [asheikh ~]# iwconfig wlan0 essid “default”
    [asheikh ~]# dhclient wlan0
    PING 192.168.1.1 (192.168.1.1) from 192.168.1.37 wlan0: 56(84) bytes of data.

    — 192.168.1.1 ping statistics —
    4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 3001ms
    pipe 3

    after a while performing the same sequence of steps it gives the following result

    [saheikh ~]# ifconfig wlan0 down
    [asheikh ~]# ifconfig wlan0 up
    [asheikh ~]# iwconfig wlan0 essid any
    [asheikh ~]# dhclient wlan0
    dhclient(8834) is already running – exiting.

    This version of ISC DHCP is based on the release available
    on http://ftp.isc.org. Features have been added and other changes
    have been made to the base software release in order to make
    it work better with this distribution.

    Please report for this software via the Red Hat Bugzilla site:
    http://bugzilla.redhat.com

    exiting.

    i hope i have explained my problem well
    any help will be appreciates.

  2. Hi — I think that you'll get better feedback if you post your problem
    to some mailing lists.

    Also, after you run ifconfig wlan0 up, run iwconfig wlan0 to see what
    ESSID you are pointing at.

    Good luck!

  3. aargh, Ive got the same problem.. Installed Ubuntu Server edition 8.10 using no gui, and cant connect to the wireless network. I have tried the things explained over, and get the same results..

    If you find it out, please tell 🙂

  4. Hi Andreas, use sudo iwlist eth1 scan to find out if your wireless
    card can see the network. Then use sudo iwconfig eth1 to see what
    your card's essid and key settings are, then set them.

    Some bad news — I haven't been able to connect to a
    WPA2-authenticated wireless network yet.

    Good luck!

  5. how do i connect to a point if it dosnt have an essid i can connect to it in windows but ubuntu wont connect without a essid but it still picksit up in a servey

  6. Hi there,

    I was very interested to test out your tutorial on my home laptop that connects to my wireless router. My router requires that you enter in a long numerical wep key. I typed in the following:

    sudo iwlist wlan0 scan (it found the ESSID for my wireless router. That was nice)
    sudo iwconfig wlan0 essid “myssid” key 12345678912345678912345 (obviously not my key. When I typed this in it gave me no complaints)
    sudo dhclient wlan0

    The dhclient command started looking for DHCPs using the netmask 255.255.255.255 and port 67. I thought this was weird so I tried to tell dhclient to look for a dhcp using my normal netmask 255.255.255.0. I typed in 'sudo dhclient -s 255.255.255.0'. It still wouldn't work!

    I also tried setting up iwconfig to translate my numeric key into hex by typing in 'sudo iwconfig wlan0 key s:12345678912345678912345' after telling it my SSID. That still didn't work.

    Oh well, maybe some day i'll get wireless working from the command line without too much hassle!

  7. Hi there,

    I was very interested to test out your tutorial on my home laptop that connects to my wireless router. My router requires that you enter in a long numerical wep key. I typed in the following:

    sudo iwlist wlan0 scan (it found the ESSID for my wireless router. That was nice)
    sudo iwconfig wlan0 essid “myssid” key 12345678912345678912345 (obviously not my key. When I typed this in it gave me no complaints)
    sudo dhclient wlan0

    The dhclient command started looking for DHCPs using the netmask 255.255.255.255 and port 67. I thought this was weird so I tried to tell dhclient to look for a dhcp using my normal netmask 255.255.255.0. I typed in 'sudo dhclient -s 255.255.255.0'. It still wouldn't work!

    I also tried setting up iwconfig to translate my numeric key into hex by typing in 'sudo iwconfig wlan0 key s:12345678912345678912345' after telling it my SSID. That still didn't work.

    Oh well, maybe some day i'll get wireless working from the command line without too much hassle!

  8. I will tell you something that I observed from my past experience, but please do not take this as an offence: people that are Linux users probably know how to do it anyway and people that don't know, probably have Windows:)))
    ________________________________
    3GPP Diameter

  9. Thank you for this blog post. The commenter, then, becomes the critic who shapes the writer's next content. So I want to encourage you to keep on blogging in the same style as you writing style is really perfect.

    _________________________
    SEO Armenia

  10. Very good article mate !

    For those who can't get it to work … remember to set your wireless card to the correct channel on wich the accespoint is running at.

    iwconfig wlan0 channel 6 (example)

  11. How to connect to a wireless network from the Ubuntu command line using a WPA/WPA2 Personal key.
    For this to work.. you need to have WPA-Supplicant installed … Ubuntu comes with WPA-Supplicant installed by default.
    if not present, install it using this command: sudo apt-get install wpasupplicant
    Ubuntu doesn't use a root password by default …so we need to use the sudo command here.

    Now we need to create 2 files: wireless-wpa.sh + wireless-wpa.conf using the vi command. For those who are not familiar with vi , I will guide you through this.

    OK ..lets start: first we gonna create the wireless-wpa.sh
    type: sudo vi wireless-wpa.sh
    press the “i” button ones and insert text below.
    #!/bin/sh
    iface=eth1

    #shut down interface
    ifconfig $iface down

    #set ad-hoc/management of wireless device
    iwconfig $iface mode Managed

    #enable interface
    ifconfig $iface up

    #stop any persistent wireless wpa2 sessions
    killall wpa_supplicant

    #apply WPA/WPA2 personal settings to device
    wpa_supplicant -B -Dwext -i $iface -c ./wireless-wpa.conf -dd

    #obtain an IP address
    dhclient $iface

    Now we need to save this file and quit vi…. press “esc” button ones … then type.. :wq ( you need to type the : also )
    Thats done … now we gonna make the wireless-wpa.conf

    type: sudo vi wireless-wpa.conf
    press the “i” button ones and insert text below.

    # config file using WPA/WPA2-PSK Personal key.

    ctrl_interface=/var/run/wpa_supplicant

    network={
    ssid=”my_router_id”
    scan_ssid=1
    key_mgmt=WPA-PSK
    psk=”1234567890″
    }

    Now we need to save this file and quit vi…. press “esc” button ones … then type.. :wq ( you need to type the : also )
    That's also done … now we need to set the permissions for these 2 files.

    sudo chmod 755 wireless-wpa.sh
    sudo chmod 644 wireless-wpa.conf

    Make sure you put these 2 files in the same directory anywhere you like…else it will not work.
    Run the following command to make a connection to your wireless Router/Accespoint/
    sudo ./wireless-wpa.sh

    That's all …. have fun!

  12. Another Linux Noob here – just wanted to say what I've found here has been very helpful.

    Thanks

    Mike G

  13. Aaahhh… hacking with a cup of coffee, this is life! Haha good blog post, easy to read and straightforward.. Thanks for posting it

  14. Now that was perfect – There are so many who are good with linux but cannot communicate – Thankfully, it's not you. Please write a linux basics book. It will sell. Very well. 

  15. Pingback: Random Thoughts

  16. Thank you so much. I’ve struggled to get wireless working on ubuntu for days. This worked like a charm!!

  17. Grazie mille…l’ho utilizzata su raspberry pi. Perfetta!!
    Thank you so much … I used on raspberry pi. Perfect!!

  18. Tried this. It spat out a load of text, the last line of which was: Daemonize..
    and then it just hung. I Ctrl-C’ed out and I of course was still not connected.

Comments are closed.