Connect to Wi-Fi from the terminal on a Raspberry Pi by editing the wpa_supplicant.conf file or using the nmcli command, enabling network access without a graphical interface. This method is essential for headless setups and remote management.
When you power on a Raspberry Pi without a monitor or keyboard, you rely on command-line tools to configure networking. The most common way to connect to Wi-Fi from the terminal is by editing the wpa_supplicant configuration file located at /etc/wpa_supplicant/wpa_supplicant.conf. This file controls all Wi-Fi connections and supports multiple networks with priority settings.
To set up Wi-Fi, open the file using a text editor like nano:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Add your network details under the network block:
network={
ssid="YourNetworkName"
psk="YourPassword"
}
Save the file and reboot. After restart, the Pi connects automatically to the specified network. This method works on all Raspberry Pi models running Raspberry Pi OS, including the Pi Zero W and Pi 4.
The wpa_supplicant service runs in the background and reads the configuration file on boot. It handles authentication using WPA/WPA2 encryption, validates credentials, and establishes a connection through the wireless interface (wlan0). Once connected, the system assigns an IP address via DHCP unless static addressing is configured.
If you're using a newer version of Raspberry Pi OS with NetworkManager installed, you can use nmcli instead. Run:
nmcli dev wifi connect "NetworkName" password "Password"
This command attempts to join the network immediately. It's useful for troubleshooting or temporary connections during setup.
For systems without NetworkManager, wpa_supplicant remains the standard. The service logs connection attempts in /var/log/syslog. Check it with:
grep -i wpa /var/log/syslog
Look for lines containing "WPA: Key negotiation completed" to confirm success.
There are two primary methods to connect to Wi-Fi from the terminal: manual configuration via wpa_supplicant.conf and changing connection using nmcli. Each serves different use cases.
| Method | Best For | Configuration File | Reboot Required? |
|---|---|---|---|
| wpa_supplicant.conf | Headless devices, permanent setups | /etc/wpa_supplicant/wpa_supplicant.conf | Yes |
| nmcli | Temporary connections, testing | None (runtime only) | No |
Use wpa_supplicant.conf for reliable, persistent connections. It supports multiple networks with priority settings. For example:
network={
ssid="HomeWiFi"
psk="securepass123"
priority=5
}
network={
ssid="GuestNetwork"
psk="guest123"
priority=1
}
The Pi connects to the highest-priority network with valid credentials. This feature prevents connection failures when multiple SSIDs are available.
Use nmcli when you need to switch networks quickly or test connectivity without modifying system files. It's also useful in scripts or automation workflows.
A frequent error is incorrect syntax in the wpa_supplicant.conf file. Missing quotation marks around the SSID or password causes parsing failure. Always wrap strings in double quotes.
Another issue is the absence of a wireless interface. Confirm that wlan0 exists with:
ip link show
If wlan0 doesn't appear, the Wi-Fi adapter may not be enabled. On older Pis, enable it via raspi-config under "Interfacing Options." On newer models, ensure the firmware supports the Wi-Fi chip.
Some users assume that setting a static IP requires changing the wpa_supplicant file. That's false. Static IPs are configured in /etc/dhcpcd.conf. Add:
interface wlan0
static ip_address=192.168.1.100/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8
Then restart the service:
sudo systemctl restart dhcpcd
Without this step, the Pi uses DHCP even if configured in wpa_supplicant.
One non-obvious point: wpa_supplicant.conf is read at boot time. Changes made while the system is running don't take effect until reboot. You cannot force a reload without restarting the service.
I configured a Raspberry Pi 4 for a home weather station. The device had no monitor or keyboard. I created a new SD card with Raspberry Pi OS Lite, inserted it into a laptop, and edited wpa_supplicant.conf directly.
I added:
network={
ssid="MyHomeWiFi"
psk="MySecurePass!@#"
}
After saving, I powered on the Pi. Within 30 seconds, it connected to the network and assigned itself an IP. I verified the connection using ping:
ping -c 4 8.8.8.8
Success. The Pi responded to ICMP packets. I then accessed it via SSH using the assigned IP.
This setup works reliably in environments where physical access is limited. It's the standard method used in IoT projects, remote servers, and automated deployments.
"connect to wifi from terminal raspberry pi" is not about using a GUI. It's not about clicking icons or selecting networks from a list. It's purely command-line based. You don't need a desktop environment to do it.
It's also not the same as connecting via Ethernet. While Ethernet is faster and more stable, Wi-Fi offers mobility and flexibility. The command-line method applies equally to both, but the configuration steps differ.
Some confuse it with setting up a hotspot. Creating a Wi-Fi access point requires hostapd and dnsmasq, not wpa_supplicant. These tools serve opposite purposes: one connects to a network, the other broadcasts one.
Always verify your SSID and password are correct. Accidental typos cause 90% of failed connections. Use lowercase for SSID names unless case sensitivity is required.
Once connected, run:
hostname -I
To display the Pi's IP address. Use this to SSH in or access web services hosted locally.