- Setup Pi Zero W Headless Wifi
- Step 1. Download a lite Raspbian image
- Step 2. Unzip the file
- Step 3. Plug a micro SD card into your Mac
- Step 4. Unmount the micro SD card
- Step 5. Burn the image to the micro SD card
- Step 6. Enable ssh
- Step 7. Add network info
- Step 8. Eject the micro SD card
- Step 9. Boot the Pi Zero W
- Step 10. Login over Wifi
- Step 11. Change your Hostname and Password
- Step 12. Get the updates
- Troubleshooting
- Related articles
- References
- Preparing a Raspberry Pi Zero with WiFi and SSH
- Prepare the SD Card
- Download OS image
- Flash the SD Card
- Configure the OS
- Enable SSH
- Enable WiFi
- Run the Raspberry Pi Zero W
- Change password
- Change hostname
- Increase swap space
Setup Pi Zero W Headless Wifi
This article covers setting up the Pi Zero W for headless SSH access over WiFi using a Mac. The Windows version of these instructions can be found here: Headless Pi Zero W Setup (Windows).
If you have an older Pi Zero 1.3, see my article Setup Pi Zero Wifi.
Step 1. Download a lite Raspbian image
These instructions are for a Raspbian Buster image that I downloaded from here:
I’m using the lite image (no desktop) version 4.19 from February 13, 2020.
These steps emulate what an install script does. If you prefer to use an app, download balenaEtcher, flash the disk and skip down to the step about enabling ssh.
Step 2. Unzip the file
Once you download the file, unzip it and note the location of the *.img file.
cd ~/Downloads unzip 2020-02-13-raspbian-buster-lite.zip ls -ls *.img
Step 3. Plug a micro SD card into your Mac
Put a blank micro SD card into an adapter and plug it into your computer.
Open up a terminal window and type:
At the bottom of the output, you should get a result similar to this:
/dev/disk2 (internal, physical): #: TYPE NAME SIZE IDENTIFIER 0: FDisk_partition_scheme *7.9 GB disk2 1: Windows_FAT_32 boot 66.1 MB disk2s1 2: Linux 7.9 GB disk2s2
Assuming that your SD card is 8GB, you can tell that /dev/disk2 is the SD card. On your machine it may be different. Make sure you have the correct one before proceeding.
Step 4. Unmount the micro SD card
Before you can burn an image to the card, you needs to unmount it first, by issuing this command (assuming disk2!):
diskutil unmountDisk /dev/disk2
Step 5. Burn the image to the micro SD card
To burn the image to the SD card, run this command (adjusting for the location and filename of the image that you downloaded):
sudo dd bs=1m if=~/Downloads/2020-02-13-raspbian-buster-lite.img of=/dev/disk2
If you get an error that the volume is busy, make sure you ran the ummountDisk command first (see step above).
The command should take several minutes. You can press Ctrl-T to get a snapshot of the commands progress.
Once it’s done you should see the new Raspbian boot volume when you execute this command:
Step 6. Enable ssh
For security reasons, ssh is no longer enabled by default. To enable it, run this command:
Step 7. Add network info
Create a new empty file that will hold network info:
touch /Volumes/boot/wpa_supplicant.conf
Edit the file that you just created and paste this into it (adjusting for your ISO 3166 alpha-2 country code, network name and network password):
country=US ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network=
Step 8. Eject the micro SD card
The command won’t physically eject the disk/card. It’s more of a logical eject: meaning it’s safe to remove the card now. You can also do this by right-clicking on the card on your desktop.
Step 9. Boot the Pi Zero W
- Put the SD card into the Pi Zero W
- Plug a Micro-USB power cable into the power port (the one closest to the end of the board)
- Give the Pi Zero plenty of time to bootup (it can take as much as 90 seconds — or more)
Step 10. Login over Wifi
This part assumes that ssh is enabled for your image and that the default user is pi with a password of raspberry.
- Open up a terminal window
- Run the following commands:
ssh-keygen -R raspberrypi.local ssh pi@raspberrypi.local
Congratulations! You can now access your Pi Zero W over wifi!
If you can’t connect, see the troubleshooting section near the end of this article. You may need to make some adjustments for your network.
Step 11. Change your Hostname and Password
Because your pi is now on the network, you should immediately change the hostname and password.
Select the options for changing the hostname and password. On a new image, I would also recommend expanding the file system (now under the Advanced options). Once the changes are made, reboot.
Remember that once you reboot, you will need to use the new hostname to login over ssh. For example, if your new hostname is mypi you would connect like this:
Step 12. Get the updates
Once connected over WiFi, the next thing you should do is run some updates:
sudo apt-get update -y sudo apt-get upgrade -y
Troubleshooting
Mac command line issues
Every time I update my Mac I seem to run into new permission issues. If you are having trouble doing simple operations at the command line, see this article:
WiFi issues
If you have trouble connecting over wifi, see my instructions for accessing a Pi Zero over a USB cable: SSH into Pi Zero over USB. Once connected you should be able to fix any issues.
Here are some helpful commands for debugging network and wifi issues on your Pi:
- This command should list your network in the first line for wlan0:
iwconfig - This command should show info for wlan0:
ifconfig - This command should list your network name:
iwlist wlan0 scan | grep ESSID - To edit or review your wifi settings, run this command:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Related articles
Here are some of my related articles:
References
- Setting up WiFi via the Command Line [1]
- Etcher [2] — a tool that handles burning the image to the micro SD card for you
Preparing a Raspberry Pi Zero with WiFi and SSH
When working with a Raspberry Pi Zero W, as there is no network port, you will need to enable WiFi and SSH as well so that you can connect to it when it first boots.
This is far simpler than the alternative, which is to connect a keyboard and monitor to the Raspberry Pi Zero W to then set up WiFi and SSH. You can simply use your existing setup.
Prepare the SD Card
You will need a microSD card and a USB adapter. These are all cheap and plentiful, some examples of adapters are here and here. Plug your microSD card into a USB adapter, then plug it into your computer.
Download OS image
The official image for Raspberry Pi in general is Raspberry Pi OS (formerly Raspbian), which can be downloaded here. If you don’t need a desktop environment, download the Lite version. Not having a desktop environment frees up valuable memory and CPU.
Optionally, you can download and verify the checksum too.
$ wget -O raspios.zip https://downloads.raspberrypi.org/raspios_lite_armhf_latest $ sha256sum raspios.zip d49d6fab1b8e533f7efc40416e98ec16019b9c034bc89c59b83d0921c2aefeef raspios.zip
Flash the SD Card
Download Balena Etcher, choose the portable version from the dropdown.
Launch Etcher, then select the zip file that you just downloaded, and choose the USB device carefully.
Click Flash and the image should get written to the SD card shortly.
Configure the OS
Once flashing is complete, unplug and replug the USB adapter. The drive should now appear in Windows (it appeared as D:\ for me) filled with OS files for the Raspberry Pi.
You now need to allow the Raspberry Pi Zero W to connect to your network and allow yourself to connect to it.
Enable SSH
Raspbian disables SSH by default. To enable it, create an empty file in this drive, called ssh .
Just the presence of this empty file on disk is enough for Raspbian to enable SSH when you power up the Raspberry Pi later.
Enable WiFi
You will need to tell Raspbian how to connect to your WiFi.
Create a file called wpa_supplicant.conf in the same boot drive. Paste these contents in there, and replace the country, SSID and PSK values.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network=
Raspberry Pi Zero W does not support 5 GHz, make sure you have 2.4 GHz enabled on the SSID that you are connecting to.
The country code is not always necessary, but helps the WiFi radio figure out which channels it can operate on as different nations may ban the use of certain frequencies based on military, security, industrial/scientific requirements. Without the country code in place, the WiFi may simply refuse to connect.
Run the Raspberry Pi Zero W
Plug the SD Card into the Raspberry Pi Zero W. Connect a micro-USB cable and power up the Pi. You can use the official Raspberry Pi power supply (~2.5A) or a USB port that supplies adequate power (~1.2A).
Wait a few minutes, then have a look at the list of connected devices on your router’s admin pages and find its IP address. If you’re having trouble figuring it out, pick one, start pinging it, and disconnect your most recent Pi to see if that’s the right IP.
Once you have the right IP, ssh to it with the default password of raspberry
$ ssh [email protected] [email protected]'s password:
You’re now connected to the Raspberry Pi.
Change password
As a best practice, run sudo raspi-config and follow the prompts to change your password.
Change hostname
Under sudo raspi-config , choose Network Options, then Hostname. Set the name to something distinctive from other Raspberry Pis.
After renaming you will be prompted to reboot.
Increase swap space
Open up the swap configuration file
sudo nano /etc/dphys-swapfile
Change the CONF_SWAPSIZE value from 100 to something larger, like 2048, then save and exit. Restart the swap service.
sudo /etc/init.d/dphys-swapfile stop sudo /etc/init.d/dphys-swapfile start
Verify the new swap space using free -m