About me

Music producer, (ex-)DJ, electronics tinkerer, Linux enthusiast and coder.

Pre-enabling wifi and SSH on a Raspberry Pi SD card


I've bought many Pi Zero W's, as they're just irresistibly cheap, so I have a lot of little Linuxes (or is the plural Linuces?) running around the house.

Unfortunately I keep forgetting how to pre-enable wifi and SSH on the SD card for a headless setup, and every time Google finds me old, incomplete tutorials, leaving me wondering why a Pi is not connecting, so it's best I finally write my own.

Enabling the SSH service is easy; just place an empty file named ssh on the boot partition of the SD card. It will disappear at next boot, but don't worry, because that means it worked. Remember to remove any file extension (ssh.txt, for example), if you create the file in Windows, which confusingly hides them by default even though it's the only OS that actually needs them. <insert annoyed grumbling>

To enable wifi, you can create a file called wpa_supplicant.conf on the boot partition. It will get moved to /etc/wpa_supplicant/wpa_supplicant.conf at next boot, so later you can edit it there. Put the following lines in the file, replacing YOUR_SSID and PASSWORD to match your network:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
  ssid="YOUR_SSID"
  psk="PASSWORD"
  key_mgmt=WPA-PSK
}

Most tutorials leave out the first lines, and that won't work in recent Raspbian versions. The lines also allow wpa_cli to update the configuration, if you wish to use it. See the man page for more complex configurations.

Once again, if you create the file in Windows, make sure there's no extra extension after .conf and save the file with Unix/Linux line breaks, not DOS/Windows breaks. I doubt Notepad can do that, so it's best to use a proper editor such as Notepad++, which has the line break option in the bottom bar (right click "Windows (CR LF)" and select "Unix (LF)" instead).

Finally eject the SD card and stick it in your Pi. After it's done booting, you should be able to connect with ssh pi@raspberrypi.local (or an IP address assigned by your router). The default password is raspberry, which you should change immediately.

By the way, it's best to give each device a different hostname, because identical hostnames will create a lot of confusion when you have several Pis running. Simply replace "raspberry" with something else in /etc/hosts and /etc/hostname and reboot to activate the change. Note that SSH may complain about a conflicting host key, if you've previously connected to another device with the same hostname. Just run this command to fix that:

ssh-keygen -f ~/.ssh/known_hosts -R raspberrypi.local

1 comment: