Monday, September 7, 2015

Setting up Wifi using Command Line–Raspberry Pi2 (Ubuntu 14.04)

Note: This tutorial is based on Ubuntu.14.04 (For RaspberryPi2, It’s the ARM version of Ubuntu14.04. The installation procedure is detailed here).

Though we can rely on Network Manager GUI, setting up Wifi using command line having some obvious advantages in certain scenarios. For e.g. Bridging your Ethernet and wifi interfaces.

The below tutorial will discuss about setting up the Wifi using command line along. Before moving further, Hope you have setup your WIfi Card in Ubuntu, with necessary firmware and driver. This tutorial is highly recommended, if you’ve not.

The first thing you’ve to do is to install certain packages, that makes the wifi setup a breeze under Debian environment. Install the following packages.

sudo apt-get install wireless-tools
sudo apt-get install wpa_supplicant

Once done, lets find the wifi interface which is ready to be used from command line. Issue the below command.

iwconfig

A sample output is given below:

br0       no wireless extensions.

eth0      no wireless extensions.

lo        no wireless extensions.

wlan0 unassociated  Nickname:"rtl_wifi"
          Mode:Managed  Access Point: Not-Associated   Sensitivity:0/0 
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality:0  Signal level:0  Noise level:0
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

The result shows that wlan0, is the wireless interface that  can be used. Now lets scan for wireless networks using any of the below command. The former will scan for every available wireless networks in the range. If you specifically know the SSID of the Wifi, to which you want to connect, to you can use the latter command, which specifies the exact SSID to connect. 

iwlist wlan0 scanning
sudo iwlist wlan0 scanning essid "Your Wifi SSID"

The below is a sample output.

wlan0     Scan completed :
                   Cell 01 - Address: AC:F1:DF:CD:2B:D4
                    ESSID:"My Wifi SSID"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
                    Encryption key:on
                    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:wpa_ie=dd1a0050f20101000050f20202000050f2020050f20401000050f202
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    Extra:rsn_ie=30180100000fac020200000fac02000fac040100000fac020000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    Signal level=58/100
 

Identify and note the words which are marked in green. We require those details to setup ‘wpa_supplicant’ configurations, which we will discuss shortly.

Now we’ve all details which are necessary to build our ‘wpa_supplicant.conf’, except one. We’ve to now generate the passphrase psk, using the Wifi SSID and the secret passphrase used to connect to your wifi router. The below command generate it for you.

wpa_passphrase "YourWifiSSID" "YourWifiSecretPassPhrase"
 
This will generate the below output, that can be copied to your ‘wpa_supplicant.conf’ file.
 
network={
    ssid="YourWifiSSID"
    #psk="YourWifiSecretPassPhrase"
     psk=5f7cad3eed901ebb12345f5b2f307680a758bfa264ab13cbd374095a34f80e25
}

Now using the scan result and the passphrase psk details, fill in the ‘/etc/wpa_supplicant.conf’ file. Use the below commands.

sudo leafpad /etc/wpa_supplicant.conf

Now update this with the above details. A sample is given below;

ap_scan=1
ctrl_interface=/var/run/wpa_supplicant

network={
        ssid="YourWifiSSID"
        #psk="YourWifiSecretPassPhrase"
        psk=5f7cad3eed901ebb12345f5b2f307680a758bfa264ab13cbd374095a34f80e25
        scan_ssid=1
        mode=0
        key_mgmt=WPA-PSK
        proto=RSN
        pairwise=CCMP
        group=TKIP
}

To know about the different configurations options in ‘wpa_supplicant.conf’, refer this FreeBSD reference manual.

Save the file. Now we’ve all set, and issue the below commands to test connecting to our Wifi router.

sudo wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf

(or use this command if the above driver not supported by your device
sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplica.conf)

You can use the –d or –dd switch with the above command to get the detailed log of the connection. If everything went will, you can see the logs on the console, which says association got succeeded. Now you’re connected to your wifi router. Now from another terminal issue the below command.

sudo dhclient wlan0

This will initiate DHCP requests to the router, and wifi interface (wlan0) will be configured with a dynamic IP addressed issued by the wifi router. Once done, try accessing internet, other machines in your network to test whether you’re actually connected.

To make this settings permanent (So that you are automatically connected on bootup), edit the network interfaces file and add the below content.

sudo leafpad /etc/network/interfaces

Append the below configuration.

auto wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant.conf

Now during every boot, your machine will automatically get connected to wifi. Happy surfing!

 

 

 

No comments:

Post a Comment