DEV Community

Hedy
Hedy

Posted on

How to connect Raspberry Pi to wifi using ssh in command line?

Connecting a Raspberry Pi to Wi-Fi via SSH is a classic "headless" setup method. This is especially useful when you don't have a monitor or keyboard for your Pi.

There are two main scenarios. Choose the one that applies to you.

Scenario 1: First-Time Setup (No Access to the Pi Yet)
This method is for a fresh Raspberry Pi OS image where you need to configure Wi-Fi before even booting it up for the first time. This is done by creating a special file on the SD card from your computer.

Steps:

  1. Flash Raspberry Pi OS: Write the Raspberry Pi OS (preferably the Lite version for servers, or any version) to your SD card using the Raspberry Pi Imager.

  2. Enable SSH (Mandatory):

  • After flashing, your computer will see the SD card as a drive called boot.
  • Navigate to this boot drive.
  • Create a new, empty file named ssh (with no extension).
  • On Windows, you can do this in Notepad by saving a file and typing ssh as the name, making sure "All files (.)" is selected instead of ".txt".
  • On Mac/Linux, use the terminal in the boot directory: touch ssh
  • This file tells the Pi to enable the SSH server on first boot.
  1. Configure Wi-Fi:
  • In the same boot drive, create a file named wpa_supplicant.conf.
  • Open this file with a text editor and paste the following configuration, editing it for your network:
conf

country=US  # Change to your country code (e.g., GB, FR, DE, IN, JP)
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="Your_WiFi_Name"
    psk="Your_WiFi_Password"
}
Enter fullscreen mode Exit fullscreen mode
  • Important: Replace US with your country's ISO code (e.g., GB for United Kingdom, DE for Germany, IN for India). This is required for legal radio frequency compliance.
  • Replace Your_WiFi_Name with your network's SSID (name).
  • Replace Your_WiFi_Password with your network's password.
  1. Boot the Pi:
  • Safely eject the SD card from your computer.
  • Insert it into the Raspberry Pi and power it on.
  • Wait about 1-2 minutes for the Pi to boot, connect to Wi-Fi, and enable SSH.
  1. Find the Pi's IP Address and Connect:
  • You need to find the IP address assigned to your Pi. Check your router's connected devices list (often under "DHCP Client List" or similar). The device name is usually raspberrypi.
  • Once you have the IP address (e.g., 192.168.1.100), open a terminal on your computer and connect using SSH:
bash

ssh pi@<your_pi_ip_address>
# Example:
ssh pi@192.168.1.100
Enter fullscreen mode Exit fullscreen mode
  • The default password is raspberry.

Scenario 2: The Pi is Already Connected via Ethernet or You Have Physical Access
If you can already access the Pi's command line (via a monitor/keyboard or an Ethernet cable), you can configure Wi-Fi directly from the terminal.

Method A: Using raspi-config (Easiest)
This is a simple text-based menu system.

  1. Open a terminal on the Pi.

  2. Run the following command:

bash

sudo raspi-config
Enter fullscreen mode Exit fullscreen mode
  1. Use the arrow keys to navigate:
  • Go to System Options > Wireless LAN.
  • Enter your Wi-Fi SSID (name) and password when prompted.
  1. Select Finish and reboot when asked.
bash

sudo reboot
Enter fullscreen mode Exit fullscreen mode
  1. After reboot, the Pi should be connected to Wi-Fi.

Method B: Using the Command Line (nmcli - Recommended for newer OS versions)
Modern Raspberry Pi OS versions include NetworkManager and its command-line tool nmcli, which is very powerful.

  1. List available Wi-Fi networks:
bash

nmcli dev wifi list
Enter fullscreen mode Exit fullscreen mode
  1. Connect to your network (replace "Your_WiFi_Name" and "Your_Password"):
bash

nmcli dev wifi connect "Your_WiFi_Name" password "Your_Password"
Enter fullscreen mode Exit fullscreen mode
  1. That's it! The connection is saved and will auto-reconnect on boot.

Method C: Editing the Configuration File Manually (Traditional Method)
This edits the same wpa_supplicant.conf file that Method 1 creates.

  1. Open the Wi-Fi configuration file:
bash

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Enter fullscreen mode Exit fullscreen mode
  1. Add the following block to the bottom of the file (with your details):
conf

network={
    ssid="Your_WiFi_Name"
    psk="Your_WiFi_Password"
}
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit (Ctrl+X, then Y, then Enter).

  2. Restart the networking service or reboot:

bash

sudo wpa_cli -i wlan0 reconfigure
# Or simply reboot:
sudo reboot
Enter fullscreen mode Exit fullscreen mode

After Connecting: Check Your Connection
Once you're connected via SSH (or on the Pi itself), verify the Wi-Fi connection:

bash

# Check the IP address assigned to wlan0 (Wi-Fi)
ip addr show wlan0

# Or use a simpler command
hostname -I

# Test the internet connection
ping -c 4 google.com
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Tip: Finding the Elusive IP Address
If you can't find the Pi's IP address in your router:

Use a Network Scanner: Tools like nmap can scan your network.

  • On Linux/Mac: nmap -sn 192.168.1.0/24 (adjust the network range to match yours).
  • On Windows, use a GUI tool like "Advanced IP Scanner".

Use the ping Broadcast (if supported):

bash

ping raspberrypi.local
Enter fullscreen mode Exit fullscreen mode

This often works if your computer supports mDNS (Bonjour on Windows/macOS, Avahi on Linux).

Top comments (0)