Quick Links

You configured your headless Raspberry Pi just the way you want it, it's settled in and running smoothly, but suddenly you want to move it away from its Ethernet tether with a Wi-Fi module. Skip hooking it back up to all the peripherals and quickly add in Wi-Fi support from the command line.

Why Do I Want to Do This?

If you're a Raspberry Pi enthusiast (or quickly becoming one), you know how annoying it can be to realize that your headless Pi project now needs yet another little tweak that likely necessitates hooking up a monitor and keyboard/mouse to the box.

Related: Everything You Need to Know About Getting Started with the Raspberry Pi

The best way to avoid falling into that trap is to configure your Raspberry Pi for remote access. Once you have that configured, however, you still need to know how to do tasks remotely that would previously be handled by a GUI interface (like turning on the Wi-Fi). Today we're going to walk you through the technical (but simple) way to remotely connect to your Pi and activate a Wi-Fi add-on dongle.

What Do I Need?

For this tutorial you'll need the following items:

  • 1 Raspberry Pi unit with Raspbian installed (this technique should work on other distributions, but we're using Raspbian)
  • 1 Ethernet connection to Pi unit (necessary for activating the Wi-Fi functionality remotely)
  • 1 Wi-Fi Dongle (we use this model on all our Pi units with great success)

If you don't use this Wi-Fi dongle model, we strongly recommend researching the model you intend to purchase to see if it is well supported. To that end, the USB Wi-Fi adapter section of the RPi wiki is very helpful.

In addition to the above items, you need to take a moment to check the configuration of the Wi-Fi node you intend to connect your Raspberry Pi unit to: you'll need to make note of the SSID, password, and encryption type/method (e.g. the node is using WPA with TKIP shared-key encryption).

Enabling the Wi-Fi Dongle via the Terminal

To get started, power up your Raspberry Pi unit without the Wi-Fi dongle attached. At this point, the only network device should be the onboard Ethernet NIC (which you have connected to your network via Ethernet cable so you can remotely access the headless device).

Connect to your Pi via SSH to access a remote terminal prompt. (If you have not yet configured your device for remote access in this fashion, please review the following tutorial).

At the prompt, enter the following command:

        sudo nano /etc/network/interfaces
    

In the nano text editor, you'll see something like this:

a

        uto lo
    
        iface lo inet loopback
iface eth0 inet dhcp

That's the very basic configuration that governs your Pi's Ethernet connect (indicated by the eth0 portion). We need to add on a very minor bit to enable the Wi-Fi dongle. Use the arrow keys to move down below the existing entry and add the following lines:

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

Once you've annotated the file, press CTRL+X to save the file and exit the nano editor. At the prompt again, enter the following command:

        sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    

Compare the contents of the file, if it exists, to the following code. If the file is empty, you can use this code to populate it. Take note of the commented lines (indicated by the # marks) to reference which variable you should use based on your current Wi-Fi node configuration.

        ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
        network={
ssid="YOURSSID"
psk="YOURPASSWORD"
        # Protocol type can be: RSN (for WP2) and WPA (for WPA1)
proto=WPA
        # Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise)
key_mgmt=WPA-PSK
        # Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
pairwise=TKIP
        #Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
auth_alg=OPEN

}

When you're done editing the file, press CTRL+X to save and exit the document. Now is the time to unplug the Ethernet cable and plug in the Wi-Fi dongle.

At the command prompt, enter the following command:

        sudo reboot
    

When the device finishes rebooting, it should automatically connect to the Wi-Fi node. If for some reason it fails to appear on the network, you can always plug the Ethernet cable back in to double check the two files and the variables you altered.

Related: How to Turn a Raspberry Pi into a Low-Power Network Storage Device

Have a Raspberry Pi related tip, trick, or a tutorial you'd love for us to write? Sound off in the comments below.