How to connect a Raspberry Pi to a Wifi network from the command line

It is often useful to connect a Raspberry Pi to a Wifi network, and with the latest models, there is no need for a Wifi dongle anymore, as it is integrated into the board. It can however be tricky to set up the Wifi when there is no desktop interface. In this tutorial we’re going to learn how to configure a Raspberry Pi to connect to a Wifi network, with the command line interface.

raspberry pi

Modify the wpa_supplicant.conf file

There are of course multiple ways to do this, but in this example we’re going to modify the wpa_supplicant.conf file. It is a file used to configure wireless networks. Note that this is equivalent to typing commands in the wpa_cli interactive mode. It is in my opinion, easier to modify the wpa_supplicant.conf file if you don’t need to add/delete networks very often.

First step, make sure you know, or can find, your Wifi SSID and password. For the SSID, you can type the command sudo iwlist wlan0 scan . It will list all the wireless networks that the Raspberry Pi can detect. For each network, you’ll find a ESSID, which is the SSID you will need for the configuration file. You’ll also find the encryption type used by the router (in my case, it is IEEE 802.11i/WPA2 Version 1 ). As for the password, it is often found on your wireless router.

Open your wpa_supplicant.conf file for modification: sudo nano /etc/wpa_supplicant/wpa_supplicant.conf . Add the following information at the end of the file:

Close the editor and save by hitting Ctrl+X, then Y.

Hide the real password

You’re probably thinking, now I have my Wifi password hard coded into a file, and anyone that could potentially break into my Raspberry Pi will be able to see it. There’s a solution for this. Instead of using a plain text version of the password, we’ll use a 32-byte encrypted number pass key. To generate this key, you can use the wpa_passphrase  command. It will generate the key from a combination of the SSID and the password. For example, for the SSID “YOUR_SSID” and the password “YOUR_PASSWORD”, here is the output of the command:

Example of wpa_passphrase
Example of wpa_passphrase

You can replace the content of the network field in your wpa_supplicant.conf file with this one. Of course, you should really remove the line that’s commented out, otherwise your password would still be written in plain text.

Modify your network interfaces file

Now the modification shown above should be enough for your Raspberry Pi to connect to the Wifi network entered. In some cases it is not (it didn’t work for me). Then you’re going to have to modify the interfaces file: nano /etc/network/interfaces . The base content of this file should be:

Close the editor and save by hitting Ctrl+X, then Y. This will configure both eth0 (Ethernet) and wlan0 (Wifi) interfaces to use DHCP to get an IP address, and let the system know that wlan0 is configured by the wpa_supplicant.conf file.

Test it

We configured everything. Let’s confirm that wlan0 is actually connected to our wireless router. Type the command ifconfig wlan0 . If it shows an IP address for inet, then your Raspberry Pi is connected to the Internet via your Wifi network.

Checking the connectivity of the interface wlan0
Checking the connectivity of the interface wlan0

If wlan0 shows no IP address, try rebooting the Raspberry Pi. In most cases the changes made to the configuration files are detected immediately, but it’s sometimes possible that you need a reboot.


And that’s it! Your Raspberry Pi is now connected to your Wifi network, and we’ve made it more secure by encrypting the password so that it is not written in plain text in the configuration file.

Be the first to comment

%d bloggers like this: