arrow_back Back to Learn
Author: Raspberry Street Press

Connect a Raspberry Pi to Wi-Fi

Updated: May, 2021

Connecting to Wi-Fi is easy!

We are going to take the more universal approach and use the command line. However, if you have Raspbian/Raspberry Pi OS desktop you can use the upper right-hand connection control icon to enable Wi-Fi. If you are using the command line only distro, you can utilize raspi-config and you'll find Wi-Fi settings under Network Options.

Our universal method does not use raspi-config or the connection control icon in the GUI. This will also work over SSH.

Just show it to me!

$ sudo rfkill unblock wifi
$ sudo sh -c "wpa_passphrase 'ssid' 'password' >> /etc/wpa_supplicant/wpa_supplicant.conf"
$ sudo wpa_cli -i wlan0 reconfigure

Done! Seriously, you are done. You can stop reading if you're busy and need to get on with your day.

Wait! What just happened? Let's break it down.

First we enable the wireless interface (wlan0)
$ sudo rfkill unblock wifi
We can check this with the ifconfig command
$ ifconfig
You'll see an entry for the wlan0 interface. We unblocked wifi because it may have been blocked either by this command
$ sudo rfkill block wifi
or Wi-Fi may have been disabled in the GUI (upper right-hand connection control icon). By issuing this command, we ensure the interface will always be enabled.

Our next command does all the magic to get connected to Wi-Fi in a single statement.

$ sudo sh -c "wpa_passphrase 'ssid' 'password' >> /etc/wpa_supplicant/wpa_supplicant.conf"

The file /etc/wpa_suplicant/wpa_supllicant.conf simiply contains our Wi-Fi connections information. We use wpa_passphrase 'ssid' 'password' to generate the proper entry needed for this file. Replace 'ssid' and 'password' with your Wi-Fi ssid and password. The output is appended to the file /etc/wpa_supplicant/wpa_supplicant.conf

Take a look at the file with the command
$ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
You'll see something like this at the end of the file

network={
ssid="my_network_id"
#psk="password"
psk=8a9b456b28ef0707987622421...
}

You can see our new entry. Our pre-shared key has been encrypted! You can edit this file and remove the un-encrypted (commented out) psk if you'd like.

At this point we are done. After some time has passed the Wi-Fi changes we made will take effect. To get these changes working right now we issue the command
$ sudo wpa_cli -i wlan0 reconfigure

To see it in live, check out the video below from our YouTube channel!


Share this

Test drive for free today

See how Raspberry Street is revolutionizing the world of enterprise IoT – one Raspberry Pi at a time.

Dashboard

Your Cart

close
Subtotal $0.00