Setup Wifi on Rapberry Pi from command line

by Remy Pereira on 29th January 2016

You can connect your Raspberry Pi to a WiFi network by plugging in a USB Wifi adapter to the Raspberry Pi's USB port.

Make sure you buy one of the USB adapters that are supported by the built-in drivers to avoid the pain of finding the correct driver and installing it. A list of supported USB Wifi dongles and their performance can be found on the link Supported USB WiFi Dongles

Once you plug in the USB adapter, you can check if your Raspberry Pi OS recognized your dongle, by issuing command:

pi@raspberrypi:~ $ dmesg | grep WLAN
[ 3.322635] usb 1-1.2: Product: 802.11 n WLAN

The above output shows the USB device was recognized by Raspbian.

Now check ifconfig output and locate wlan interface, usually wlan0. In the ifconfig output, eth0 is the built-in wired interface, lo is loopback and the interface name starting with wlan is the USB Wifi interface.

pi@raspberrypi:~ $ ifconfig
wlan0 Link encap:Ethernet HWaddr 00:0f:60:09:29:c0


In my case my wireless interface is wlan0. The wlan0 wireless interface has to be configured for DHCP by editing the /etc/network/interfaces file. Make sure it has the following lines

pi@raspberrypi:~ $ sudo nano /etc/network/interfaces

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Now we need to configure the SSID and password of our WiFi network. Scan for available wireless networks using the command below:

pi@raspberrypi:~ $ sudo iwlist wlan0 scan

The output is rather long and complicated. If you are in the range of your Wifi network it will show up in the scan.

wlan0 Scan completed :

IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK

Now add the SSID and password provided by your Wifi network provider (usually found at the back of the router) to the configuration file /etc/wpa_supplicant/wpa_supplicant.conf

pi@raspberrypi:~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
	ssid="YOUR SSID"


Save by pressing Ctrl+X and y enter.

Now bring the interface down, wait for few seconds and bring it back up again.

pi@raspberrypi:~ $ sudo ifdown wlan0

pi@raspberrypi:~ $ sudo ifup wlan0

The interface is now connected and acquired an IP address. You may verify by running ifconfig again.

pi@raspberrypi:~ $ ifconfig
wlan0 Link encap:Ethernet HWaddr 00:0f:60:09:29:c0
inet addr: Bcast: Mask: 

If the interface is still not connected, you may reboot the machine and check again. In some cases you may need to add extra lines to /etc/wpa_supplicant/wpa_supplicant.conf to specify advanced Wifi configurations as shown in the example below

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

These configuration parameters can be understood from the result of iwlist scan.

  • proto - Protocol type can be RSN or WPA (for WPA2 and WPA1 respectively).
  • pairwise - CCMP or TKIP (for WPA2 and WPA1 respectively).
  • auth_alg - authentication algorithm, can be OPEN for both WPA1/WPA2

After configuration changes, save the file and restart the interface. Check using ifconfig or iwconfig.

pi@raspberrypi:~ $ iwconfig

wlan0 IEEE 802.11bgn ESSID:"YOUR SSID"

Mode:Managed Frequency:2.437 GHz Access Point: 00:64:D9:27:35:68

Sometimes the USB Wifi adapter requires more power than the Pi USB port can provide, specially if your router or Wifi hotspot is far away from your Raspberry Pi and you need to send large amounts of data over a weak connection. In this case you can get better performance by plugging the adapter to a powered USB hub.

Post a comment


Nothing the first to share wisdom.