Saturday, November 30, 2019

Raspberry Pi Headless WiFi Setup

After flashing Raspbian to an SD card for the Raspberry Pi, there are a few things to do in order to make it a headless install. The first is to enable SSH. For this place an empty file named ssh with no extension to the root of the boot disk. In the terminal window, run this command:

touch /Volumes/boot/ssh

The next step is to add your wifi settings to a file on the boot disk called wpa_supplicant.conf. In the terminal window, run this command:

touch /Volumes/boot/wpa_supplicant.conf

then paste the following into it (adjusting for your ISO 3166 alpha-2 country code, network name and network password):

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="SSID"
    psk="PASSWORD"
}

Edit the file on the boot drive that was created and paste the above into it (adjusting for the name of your country code, network name and network password). The Raspberry Pi is now ready to be plugged in and will connect to your network. From here, you can log into your router and find the IP address or use a network scanner application and connect with the default pi username and password to complete the setup.
 
UPDATE: I created a small bash script to create the files and update the wireless ssid and password to speed up the process after flashing the SD card:
 
#!/bin/bash

SSID=${1}
PSK=${2}
VOLUME=${3:-/Volumes/boot}

FILES=('ssh' 'wpa_supplicant.conf')

for F in ${FILES[@]}
do
  printf "checking for ${F}\n"
  if [ ! -f $/{VOLUME}/${F} ]; then
    touch ${VOLUME}/${F}
  fi
done


cat > ${VOLUME}/wpa_supplicant.conf <<EOF
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="${SSID}"
    psk="${PSK}"
}
EOF