Raspberry Pi Zero W: Headless bring up (Raspbian)

The following article describes the most straightforward way to setup and connect a Raspberry Pi Zero W to your WPA2 Wi-Fi network.

Preparation

Download and unpack the image:

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

Mount the image to 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'

Enable SSH server

su -c 'touch boot/ssh'

Configure Wi-Fi interface

Automatically setup wlan0 for 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

Create and open file wpa_supplicant.conf:

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

Configure wpa_supplicant according to your 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
+}

Clean up

Unmount image partitions and clean up:

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

Install image to MicroSD card

Insert the MicroSD card into your host computer and check the systemd journal for the appropriate device node:

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

Use dd to write the image to the card:

Be careful when running the following command! The output file (of) has to be the MicroSD card's device node!

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

Now put the MicroSD card into your Raspberry Pi and attach the power supply. Give the device some time to boot up.

Connect to the Raspberry Pi

Scan your 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 connect to it via SSH:

ssh pi@192.168.1.27