How to Connect Raspberry Pi to WiFi Using CLI (Command Line)

Setting up your Raspberry Pi’s WiFi via the command line is a game-changer, especially for headless setups where you don’t have access to a monitor or GUI. Whether you’re using a Raspberry Pi 4, 3, or Zero W, this guide will walk you through connecting to WiFi using the terminal, ensuring a seamless and efficient setup.​

If you’re just starting out, make sure to check our beginner-friendly list of Raspberry Pi projects for beginners to get hands-on experience after connecting to WiFi.

What You’ll Need

  • Raspberry Pi: Any model with WiFi capability (e.g., Pi 3, 4, Zero W).​
  • Raspberry Pi OS: Preferably the Lite version for headless setups.​
  • Terminal Access: Via SSH or direct connection.​
  • Text Editor: Such as nano or vi.​

Don’t have a Raspberry Pi yet? Our curated list of the best Raspberry Pi starter kits for beginners will help you pick the right one to get started.

Quick Commands

TaskCommand
Scan WiFi Networks`sudo iwlist wlan0 scan
Edit WiFi Configurationsudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Restart WiFi Interfacesudo wpa_cli -i wlan0 reconfigure
Check IP Addressifconfig wlan0
Test Internet Connectionping -c 4 google.com

Step-by-Step Guide to Connect Raspberry Pi to WiFi Using CLI

1. Access the Terminal

If you’re setting up a headless Raspberry Pi, insert the SD card into your computer and create an empty file named ssh (without any extension) in the boot partition. This enables SSH on first boot.​

2. Identify Your Wireless Interface

Open the terminal and run:

iwconfig

Look for your wireless interface, typically named wlan0.

3. Scan for Available WiFi Networks

To view nearby WiFi networks:

sudo iwlist wlan0 scan | grep ESSID

This command lists all available SSIDs.

4. Configure WiFi Settings

Edit the wpa_supplicant.conf file:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add the following configuration, replacing Your_SSID and Your_Password with your network’s details:

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid=”Your_SSID”
psk=”Your_Password”
key_mgmt=WPA-PSK
}

Save and exit the file.

5. Restart the WiFi Interface

Apply the new settings by restarting the WiFi interface:

sudo wpa_cli -i wlan0 reconfigure

Alternatively, you can reboot the Raspberry Pi:

sudo reboot

6. Verify the Connection

After rebooting, check if your Raspberry Pi is connected to the internet:

ifconfig wlan0

Look for an IP address under inet. You can also test the connection by pinging a website:

ping -c 4 google.com

Now that your Pi is online, you can explore a wide range of IoT projects with Raspberry Pi — from smart home automation to weather monitoring systems.

Conclusion: Master Raspberry Pi WiFi Setup Like a Pro

Connecting your Raspberry Pi to WiFi using the command line may sound a bit technical at first—but once you follow the steps, you’ll realize how quick and simple it really is. Whether you’re using it for a headless project, IoT setup, or just prefer working through the terminal, this method gives you full control.

If you’re planning to connect your Raspberry Pi to the internet for remote access or IoT purposes, don’t forget to follow these Raspberry Pi security best practices to protect your device from potential threats.

FAQs

  1. Can I connect to a hidden WiFi network?

    Yes, add scan_ssid=1 within the network block in your wpa_supplicant.conf file.

  2. How do I set up WiFi on the first boot without a monitor?

    Place both wpa_supplicant.conf and an empty ssh file in the boot partition of your SD card before the first boot.

  3. What if my Raspberry Pi doesn’t connect to WiFi?

    Ensure your SSID and password are correct, and that your router is functioning properly. You can also check logs using dmesg or journalctl for more details.