In this post we will see how to set up our Raspberry Pi to connect via Wifi either through the graphical interface or through the command console.
Connecting the Raspberry Pi via LAN is useful in many cases, but logically having Wifi will allow us a freedom when working with it, installing it in our projects, or using it in IoT applications.
In the early versions of Raspbian, setting up Wifi could be a headache. But in the modern versions, the process has been greatly simplified and it is currently very easy both through GUI and Terminal.
What hardware do we need?
If we are talking about a Raspberry Pi 3 or 3+, or Zero W, you are in luck. These models have integrated Wifi so we won’t need any additional components and we have the guarantee that it will work correctly.
In the rest of the models we will have to use a USB adapter (USB dongle). Fortunately, they are a very cheap device that we can find for 1-2€ in international sellers on AliExpress or eBay.
However, not all USB Wifi adapters are compatible with Raspberry Pi. Although in recent times there have been important improvements and most of them are compatible, it is advisable to check the compatibility before purchasing. In case of doubt, go for the safe option and choose one that specifically indicates that it is compatible.
Set up Wifi via GUI
If you have access to the graphical interface on the Raspberry Pi, either with a keyboard, mouse, and monitor or through VPN, in the latest versions of Raspbian setting up Wifi is very simple.
In the upper right corner, we have a Wifi icon. We simply have to click on it and select “Turn on Wifi”. Now, in the list of available networks, we select the Wifi we want and click on it to connect.
That’s how simple it is! Now we can browse and access our Raspberry Pi via Wifi.
Set up Wifi via terminal
If we do not have access to the graphical interface, for example because we access via SSH, it is always possible to configure Wifi through the command console.
Setting up Wifi through the command console is terribly useful and frequent, as in many cases we do not want to use the graphical interface at all.
It is even possible to preconfigure Wifi when installing the operating system, and leave it ready to be accessed via SSH or VPN, without ever having connected a keyboard or a monitor. We will see this in a post, later on.
List available Wifi networks
Although in many cases the network we want is our own Wifi and we know its name (SSID) perfectly, it is also possible to list the available networks from the terminal.
For this we use the command
sudo iwlist wlan0 scan
The result is a long list (quite long) with all the data and information of the available Wifi networks.
Set up the Wifi network
Once we know the network and password we want to connect to, we must edit the file /etc/wpa_supplicant/wpa_supplicant.conf
To do this, we use the command:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
In this file, we add the following at the end of the file, changing the data for our wifi.
network={
ssid="your-wifi-name"
psk="your-wifi-password"
key_mgmt=WPA-PSK
}```
So the file would look something like this:
```bash
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=ES
network={
ssid="your-wifi-name"
psk="your-wifi-password"
key_mgmt=WPA-PSK
}
And we restart the Raspberry Pi with the following command:
sudo reboot
Check connection
To check that we have connected correctly and obtain the IP that the router has provided us, we can use the following command
ifconfig wlan0
If everything has gone correctly we will see that we have been assigned an IP. In the example image, it has assigned us 192.168.1.44. Great, we already have Wifi set up!
In previous versions
In case you have to deal with an older version of Raspbian, or to avoid confusion if you find tutorials that are not updated on the internet, it is worth mentioning that in previous versions the Wifi configuration was done by editing the file /etc/network/interfaces
To do this, the command was used:
sudo nano /etc/network/interfaces
The content of the file after the modification looked something like this:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid "your-wifi-name"
wpa-psk "your-wifi-password"
In the current versions of Raspbian this method is not used. In fact, if you open it you will see that it is practically empty, because of the way the configuration files work.
## interfaces(5) file used by ifup(8) and ifdown(8)
## Please note that this file is written to be used with dhcpcd
## For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
## Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
In the next Raspberry Pi tutorials we will see how to configure a static IP, how to access via VPN and SSH, and how to configure Raspbian without connecting a monitor and a keyboard.