How to install Debian 12 Bookworm in Raspberry Pi Zero 2 W headless

At the time of writing this article, the Pi Imager application does not offer the possibility to install Debian 12 Bookworm in a RPi Zero 2. Only Debian 11 is available with the Imager app.

Here are the steps I followed to install Debian 12 in a RPi 2 Zero 2 W in headless mode (no keyboard, no display, no direct LAN connection, only wifi).

Download the Debian 12 bookworm image from the Raspberry site and write it in a SD card.

Mount the SD card in another computer. You must mount 2 partitions : bootfs and rootfs.

In the rootfs partition :

Replace WIFISSID with your wifi SSID.

Create the file /etc/NetworkManager/system-connections/WIFISSID.nmconnection :

[connection]
id=WIFISSID
uuid=257add22-489d-4dc2-947a-35ebc3bb67e0
type=wifi
interface-name=wlan0
autoconnect=true
[wifi]
mode=infrastructure
ssid=WIFISSID
[wifi-security]
auth-alg=open
key-mgmt=wpa-psk
psk=WIFIPASSWORD
[ipv4]
method=auto
[ipv6]
method=auto

Replace WIFIPASSWORD with your wifi password.

Replace the uuid value with another one. You can use uuidgenerator.net to generate a UUID.

Set the owner of this file to root:root (chown root:root <file>) and the permissions to 0600 (chmod 0600 <file>).

Create the file /boot/firmware/firstrun.sh :

#!/bin/bash
# Stripped back version of the Bookworm firstrun.sh generated by the Raspberry Pi Imager.
# Calls raspi-config to set Wifi country, which in turn calls iw on Bookworm.
set +e
rfkill unblock wifi
for filename in /var/lib/systemd/rfkill/*:wlan ; do
    echo 0 > $filename
done
raspi-config nonint do_wifi_country CH
rm -f /boot/firmware/firstrun.sh
exit 0

Replace CH with your country code.

Make this file executable (chmod +x <file>)

In the bootfs partition :

Create an empty ssh file in the root of the bootfs partition.

This will have the effect of enabling ssh at boot time.

Create a userconf.txt file in the root of the bootfs partition.

This file must contains a single line with the format :

username:encrypted-password

Replace username by the username you want to use to connect to the RPi.

Replace encrypted-password with the encrypted password you want to use. You can encrypt the password with :

echo 'my-password' | openssl passwd -6 -stdin

The end

Boot the RPi Zero 2 W with this SD card and after a while (can be a couple of minutes), you should be able to connect to the RPi with ssh.

Find the RPi in your network :

$ sudo nmap -sn 192.168.1.0/24 | grep -iB2 "D8:3A:DD"
Nmap scan report for 192.168.1.123
Host is up (0.073s latency).
MAC Address: D8:3A:DD:3F:38:B1 (Unknown)

A RPi has been found with IP 192.168.1.123 :

ssh username@192.168.1.123