UPDATE: This does not work since Raspberry Pi OS based on bullseye. sdm might be a good alternative approach.
I’ve been playing with some Raspberry Pi Zero W machines for a few projects. They are inexpensive, with a form factor that makes for all sorts of interesting possibilities.
They are also a pain to initially provision, because you need adapter cables for mini-HDMI and micro-USB to get a monitor and keyboard connected.
With some help from this post from Linuxconfig.org, here’s what I’ve been doing to get the Pi Zero W up and running, on the network and ready for SSH access without having to plug anything in except power and the SD card!
Mount the image
$ fdisk -l *raspbian.img
The initial FAT /boot partition lies at 8192 sectors in. A sector is 512 bytes. So, now we’ll mount the /boot partition using the loop pseudo-device.
$ mkdir boot
$ mount -o loop,offset=$(( 512 * 8192)) *raspbian.img boot
Set up SSH
Let’s go into our new boot subfolder where the image’s first partition is now mounted.
All we need to do to enable SSH is create an empty file called ssh.
$ cd boot
$ sudo touch ssh
To configure WiFi, we’ll need to drop a wpa-supplicant.conf file in this folder. Upon first boot, Raspbian moves this into the correct config location and the Pi will be able to talk to our network out of the box.
Create a file called wpa-supplicant.conf with these contents:
ssid="Your network name"
psk="Your network password"
You’ll need to replace the placeholders with your network name and password, and possibly change country too.
Unmount the image
$ cd ..
$ umount boot
Write the image
Insert the target SD card and verify which device it is using fdisk.
Take great care to ensure that the command below is run with the correct device as the target, or you’ll overwrite a hard drive on your local system.
$ sudo fdisk -l
Disk /dev/sdb ...
Model: Card Reader
I am confident that sdb is correct, because I see from fdisk that the model is Card Reader.
$ sudo dd if=*raspbian.img of=/dev/sdb
Remember that once you’ve booted, you’ve just spun up a Raspberry Pi on your network with the default username of pi and the default password of raspberry. Check your router or network logs for the assigned DHCP address of the new Pi, log in promptly over SSH and change that password!