Raspberry Pi Zero W: Headless Bring-up (Raspbian)

In this post we set up a Raspberry Pi Zero W the simplest way and connect it to a WPA2 Wi-Fi network.

Preparation

We download the image and unpack it:

wget -O raspbian-stretch-lite.zip https://downloads.raspberrypi.org/raspbian_lite_latest
unzip raspbian-stretch-lite.zip

Then we mount the image in the local filesystem:

su -c 'losetup -f --show 2017-11-29-raspbian-stretch-lite.img'
  /dev/loop0
su -c 'partx -a /dev/loop0'
mkdir boot root
su -c 'mount /dev/loop0p1 boot'
su -c 'mount /dev/loop0p2 root'

Enabling the SSH server

su -c 'touch boot/ssh'

Configuring the Wi-Fi interface

We set up wlan0 so that the interface comes up automatically and gets its address via DHCP:

su -c 'emacs root/etc/network/interfaces'
source-directory /etc/network/interfaces.d
+
+auto wlan0
+allow-hotplug wlan0
+iface wlan0 inet dhcp
+    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Next we create the file wpa_supplicant.conf and open it:

su -c 'emacs root/etc/wpa_supplicant/wpa_supplicant.conf'

There we configure wpa_supplicant to match our Wi-Fi network:

-country=GB
+country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
+
+network={
+  ssid="YOUR_SSID"
+  psk="YOUR_PSK"
+  proto=RSN
+  key_mgmt=WPA-PSK
+  pairwise=CCMP
+  auth_alg=OPEN
+}

Cleaning up

We unmount the partitions of the image and clean up:

su -c 'umount root'
su -c 'umount boot'
su -c 'partx -d /dev/loop0'
su -c 'losetup -d /dev/loop0'

Writing the image to the microSD card

We put the microSD card into the computer and search the systemd journal for the matching device node:

journalctl -k -f | grep sd
  ...
  Jan 23 20:41:10 discordia kernel:  sdb: sdb1
  ...

With dd we write the image to the card:

Careful, dd does not ask before it overwrites! The output file (of) must be the device node of the microSD card!

su -c 'dd bs=4M if=2017-11-29-raspbian-stretch-lite.img of=/dev/sdb conv=fsync'

Now we put the microSD card into the Raspberry Pi and connect the power supply. The Pi needs a little time to boot.

Connecting to the Raspberry Pi

We search the local network for the Raspberry Pi:

nmap -p22 192.168.1.0/24 | grep raspberrypi
  Nmap scan report for raspberrypi (192.168.1.27)

Finally we log in via SSH:

ssh pi@192.168.1.27