Setting up a headless Raspberry Pi Zero W

A guide how to setup a headless Raspberry Pi Zero W without ever connecting it to a screen or keyboard.

I was recently setting up a headless Raspberry Pi Zero W. Since I did not have the required cables at hand to connect it to my screen, a fully headless setup was required. A bunch of guides exist online, but I needed to grab the content from here and there to get it fully running and decided to make a tiny guide. Most of the commands below must be executed as root.

Requirements to do this setup:

  • Raspberry Pi Zero W
  • Wifi Router
  • micro SD card + reader
  • Power supply for RPi

Flash SD card

  • download Rasbian image from here (use 32 bit lite version)
  • flash SD card using your favorite tool (command line: sudo dd if=<filename> of=<drivename> bs=32M && sudo sync where you replace
    • <filename> with the path pointing to the downloaded file
    • <drivename> with the location of the SD card you want to flash (careful here, you can mess up a lot if you select the wrong target location (of))

Preparing system for ssh

  • first thing we need to do is to make sure that the device will know to which WiFi it should connect
  • if not mounted yet: mount the boot partition from the sd card somewhere (e.g. sudo mount /dev/mmcblk0pq boot/)
  • create a new file in the partition: touch boot/wpa_supplicant.conf
  • edit the content of that file using e.g. nano,vim,etc:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="wifiName"
    psk="wifiPassword"
    key_mgmt=WPA-PSK
}

where you of course replace wifiName and wifiPassword (attention: leave the starting and trailing “). Also replace the country identifier if necessary to adapt to your local country. This file will later be copied over and the password will be available to the pi on a system level.

Enable ssh remote access

Next thing is to enable the ssh remote access to the pi. It is sufficient to generate an empty file called ssh in the boot directory: touch boot/ssh.

Allow access through ssh with default password or modify password

  • at some point the raspberry pi community decided that ssh access using the default username and password should not be allowed
  • let’s adapt the default password
  • we create yet another file in the boot folder called userconf into which we store a hashed version of the password
  • generate hashed version: echo 'password' | openssl passwd -6 -stdin > pwd_hased.txt where you of course replace password with your new password
  • open the file called pwd_hashed.txt and append the username in front of the hashed password: username:hashed-password
  • copy file to previously mentioned boot directory: cp pw_hashed.txt <boot>/userconf

Unmount and boot up pi

Now it is time to unmount the SD card, insert it into you Raspberry Pu and connect it to power (if you wonder, the power plug is the one more towards the corner of the device). The Raspberry Pi should become visible in your network approximately after 30 seconds. Note that the files you created in the boot partition will be moved / deleted after the first boot.

After the first boot, login to the device and run the raspi-config tool as sudo. I needed to enable ssh access here, because the ssh server was disabled after reboot again (method with creating the empty ssh file seems to be non-permanent).

Further reading / alternative guides:

Leave a Reply

Your email address will not be published. Required fields are marked *