In my honest opinion, sometimes it’s not easy to gather hardware just for accessing settings on newly bought Raspberry Pi. That’s why today I will be telling you how to set up Wi-fi connection on a Raspberry Pi without the need for a monitor, keyboard or mouse. Just a quick note before we begin exploring this article, this tutorial is mainly about initializing Wi-Fi on Raspberry Pi 3, however, for other versions, all you need is a supported Wi-Fi adapter with correct drivers in place.

How to connect to Wi-Fi on Raspberry Pi using Command Line:

Basic Set-up:

First of all, you need to have is a Linux-based Raspi supported operating system – preferrable Ubuntu/Debian based OS – installed on your SD card. Now, boot up your Raspberry Pi 3 using the micro USB as a power source with a LAN cable connected to the port available on Pi.

SSH Connection to Pi:

To perform any changes on your Raspberry Pi, we will need an SSH connection. Windows users can use PuTTy, where Unix/Linux based operating systems come loaded with a pre-installed version of SSH. However, on some operating systems, such as Raspbian, SSH isn’t enabled by default and can be activated by writing a file named ssh without any extension or content on the SD card.

Moving further, we will need to find the IP address assigned to the Raspi using either the router admin UI or scanning the range of the IPs using utilities like Nmap. Now to log in into the Raspi, on Linux/Unix OS, all you have to do is open the terminal and enter the following command $ssh <username>@<ip-address>. Where IP address is the IP you got from the router admin UI or Nmap scans, and username is the user existing in the operating system.

Say yes to the fingerprint and enter the password to log in. If you are unaware of the login credentials, then those can be found on the website from where you downloaded your operating system. On Windows PC, fire up the PuTTy program, enter the details in the respective fields and click Open. Once done, enter username and password to connect over SSH.

Editing WPA Supplicant Configuration:

Once inside the Raspi, we will have to edit a file residing in /etc/wpa_supplicant/ directory using the following command.

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

This file is going to contain information regarding your Wi-Fi networks such as its SSID and password. Following is the accepted format and can be copy-pasted after replacing SSID and PSK with correct values.

#Your wpa_supplicant.conf file
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

ssid="your network name"
psk=" Password."
id_str="home, office, etc."

If you want to prioritize the connection between two or more authorized networks in range, then you can use priority tag and assign a number to it. For example, in the following sample, the network with name Home will have more priority over the network Home2.

#For setting priorities

id_str="home, office etc"
priority = 1

id_str="home, office etc"
priority = 2

Now depending on the editor, you used to edit the files,  enter commands to save and quit. For Nano editor, press Control + O hit Return and then Control + X to exit.

Editing Interfaces File:

After this step, we will also need to make some changes in the interface file located in the /etc/network/ directory. Following is the content you can directly copy and paste, however, be sure to have the correct names in place. For example, some systems name your Wi-Fi network interface as wlan0 another call it wlan1, wlan2, etc. You can check the correct name by executing the ifconfig command on Raspberry Pi terminal.

#your network interfaces file

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface default inet dhcp

Once you complete this step, you can either restart the Raspberry Pi or execute the following commands:

#Restarting wireless interface
$sudo ifdown wlan0
$sudo ifup wlan0

Note: Be sure to cross check the name of the wireless you want to reset.

As soon as you perform this, you will be connected to the Wi-Fi network mentioned in previous steps.

Raspberry Pi Wi-Fi

What is WPA Supplicant?

But, before we end this blog, it’s important to talk about what is wpa_supplicant we just played around with. As the name suggests, WPA Supplicant is the IEEE 802.1X/WPA component designed to implement critical negotiations between WPA Authenticator and control the roaming of IEEE 802.11 wireless driver’s authentication and association. In simple terms, WPA Supplicant is a free package that helps the wireless driver to connect, authenticate with different known stations around you.

Now, with this, we have come to an end of our article, for any discussions or queries, please drop a comment. Thank you for visiting my blog.