Configure & Connect wireless network through the command line in Ubuntu 12.04

35,376

This short article helped me connect my 12.04 LTS server to my WPA2 PSK network: prupert @ WordPress . I run the server without desktop, so it demanded all cmd line.

I am paraphrasing a quick breakdown of the steps, but please click on the link for the full article:

(You Need to be connected to the network at this point)

Install The Software:

(You only need WPASupplicant if it is not an open network) sudo apt-get install wireless-tools wpasupplicant

Activate your wireless network:

sudo ifconfig wlan0 up

To Check The Wireless Run:

iwconfig

Then:

sudo iwlist scan

(The sacn command should return visible wireless networks, but if it does not it is a sign that there are none visible or something is wrong with your HW/SW outside the scope of the linked article)

Run: (To recieve you WiFi Key)

wpa_passphrase YOURSSID YOURWIFIPASSWORD

A Sample of The Resulting Text: (As Provided by the Linked Article)

network={
ssid="YOURSSID"
#psk="YOURWIFIPASSWORD"
psk=fe727aa8b64ac9b3f54c72432da14faed933ea511ecab1 5bbc6c52e7522f709a
}

Copy the "psk" somewhere accessible, this is going to allow you to connect to your network.

Edit your interfaces file:

sudo nano /etc/network/interfaces

Append your WiFi network to the end of this file using the folling options and syntax:

auto wlan0     #change this to the name of your WiFi interface
iface wlan0 inet dhcp     #this is normally fine, if you want a static IP address replace “dhcp” with “static”
netmask 255.255.255.0     #change this as appropriate for your network, this value is usually right
gateway 192.168.1.1     #change this as appropriate for your network
address 192.168.1.100     #only needed for a static IP address
dns-nameservers 192.168.1.1     #only needed for a static IP address
wpa-driver wext     #you shouldn’t need to change this
wpa-ssid YOURSSID     #just type the name of your SSID here
wpa-ap-scan 1     #if the name of your SSID is hidden usually, type 2 instead of 1
wpa-proto WPA    #if you use WPA1 type WPA, if you use WPA2 type RSN
wpa-pairwise CCMP     #if you use AES type CCMP, if you use TKIP type TKIP
wpa-group CCMP     #if you use AES type CCMP, if you use TKIP type TKIP
wpa-key-mgmt WPA-PSK     #usually WPA-PSK (if you share a key) but sometimes WPA-EAP (for enterprises)
wpa-psk YOURHEXKEYFROMABOVE     #the hex key that you generated earlier

Example from Author of the linked article:

auto wlan0
iface wlan0 inet dhcp
netmask 255.255.255.0
gateway 192.168.1.1
wpa-driver wext
wpa-ssid MYPLACE
wpa-ap-scan 1
wpa-proto WPA
wpa-pairwise CCMP
wpa-group CCMP
wpa-key-mgmt WPA-PSK
wpa-psk 71c81a844973ae7bb1243141e5caa7b6bb0e2d7eetcetcetc

You may now comment out the top of the Interfaces file, which will disable the ethernet. The author of the article reccommends this to prevent conflicts, but I decided not to as the only way that I can access my server interface is via PuTTY SSH, so I want to have a method of connection if the wireless is down, where I don't need to break out a mouse and monitor: (Remove the "#" later if you need the ethernet connections to work again)

#auto eth0
#iface eth0 inet dhcp

Write Out the file and Save your Changes Reboot your Machine

If this doesn't work, you may need to edit the Config file for the WPASupplicant program:

sudo nano /etc/wpa_supplicant.conf

You edit this file with much of the same information, except the wpa- piece: (A sample below from the author of the linked document)

ap_scan=1
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="MYPLACE"
scan_ssid=0
psk=71c81a844973ae7bb1243141e5caa7b6bb0e2d7eetcetcetc
key_mgmt=WPA-PSK
proto=WPA
pairwise=CCMP
group=CCMP
}

"As far as I am aware, the options are the same. So, edit this file as necessary, make sure you add the ctrl_interface and network={ at the beginning and the } part at the end. Save it and try restarting again. If it still doesn’t work, then kick your PC, wish you had installed Windows 7 instead and go off and do some Googling. You’ll find the answer on the Ubuntu forums and you’ll be happy again.

Share:
35,376

Related videos on Youtube

Arjun Kanti
Author by

Arjun Kanti

Updated on September 18, 2022

Comments

  • Arjun Kanti
    Arjun Kanti over 1 year

    The wireless networks are detected, but I am unable to connect to them.

    Here is the code I've tried:

    sudo iwlist wlan0 scan (Working)
    iwconfig wlan0 essid "Network name" key s:"key" (Error for wireless request "Set Encode" (8B2A)) SET failed on device wlan0 ; Invalid argument
    
    wpa_passphrase essid password > /etc/wpa_supplicant.conf
    wpa_supplicant -B -Dwext -iwlan0 -c/etc/wpa_supplicant.conf
    
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    

    Then i tried by adding ctrl_interface=/var/run/wpa_supplicant at the beginning of file wpa_supplicant.conf and running the same wpa_supplicant command but result is

    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ctrl_iface exists and seems to be in use - cannot override it
    Delete '/var/run/wpa_supplicant/wlan0' manually if it is not used anymore
    Failed to initialize control interface '/var/run/wpa_supplicant'.
    You may have another wpa_supplicant process already running or the file was
    left by an unclean termination of wpa_supplicant in which case you will need
    to manually remove this file before starting wpa_supplicant again.
    

    How do I connect to a specified wireless network through command line in Ubuntu 12.04?

    • chili555
      chili555 over 10 years
      key s:"key" implies that your key is WEP and ASCII. Your wpa_supplicant.conf implies it is WPA. Which is it? Are you able to use vim to amend /etc/network/interfaces?
    • Arjun Kanti
      Arjun Kanti over 10 years
      It is WPA2. 'vi amend /etc/network/interfaces' contains 'auto lo iface lo inet loopback'. Manually its connecting but i want to connect through command line.(DHCP Client)
    • chili555
      chili555 over 10 years
      Please explain 'manually it's connecting.' Is Network Manager running or is this a server without a desktop?
  • Burly Ares
    Burly Ares about 10 years
    Arjun. If you are to attempt to use this answer for your case, then make sure you take into account any changes that you have already made to various conf files as stated in your original question
  • McLan
    McLan over 9 years
    thanks for this answer , but how can I set the operation mode to ad-hoc in this configuration ?
  • PRS
    PRS over 5 years
    this answer was exactly what I needed to move my headless ubuntu server over from wired to wireless. thank you!