Setting up a Headless Raspberry Pi 3

I picked up a Raspberry Pi 3 a while back, but didn’t get a chance to play with it much. I wanted to set it up as a headless device without a desktop environment, and then SSH into it. Today, I got a chance to set it up and fix some WiFi reliability issues in the process. Please note that I was doing this on macOS, so the paths below might be different for you if you are doing this on a different OS.

I downloaded the latest Raspbian Lite image from:

https://www.raspberrypi.org/downloads/raspbian/

After that, I flashed the image on an SD card:

sudo dd bs=4000000 if=~/Downloads/2019-04-08-raspbian-stretch-lite.img of=/dev/disk2

You don’t have to start Raspberry Pi and hook it up with keyboard and monitor to configure SSH and WiFi — it can be done offline by mounting the the boot volume on the SD card (removing and reinserting it after flashing is done will automatically mount it) and making some changes at the root of the filesystem.

To configure SSH, simply create this empty file:

touch /Volumes/boot/ssh

And to configure WiFi, add this file:

nano /Volumes/boot/wpa_supplicant.conf

The contents of this file should look something like:

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

network={
ssid="Your SSID"
scan_ssid=1
psk="Your Password"
key_mgmt=WPA-PSK
}

Please note that specifying the right country code is important.

After that, unmount or eject the boot volume, insert the SD card in the Raspberry Pi, and power it on. It should automatically boot, connect to WiFi and enable SSH.

The alternative is to connect the Raspberry Pi to a monitor through HDMI, connect a USB keyboard, and use this handy tool to configure WiFi and SSH:

raspi-config

It has a decent UI for Raspberry Pi configuration.

You can then SSH into the Raspberry Pi using:

ssh -lpi <IP Address of Raspberry Pi>

The default password for the user ‘pi’ is ‘raspberry’. Please change it right away because everyone would know it!

Once everything was set, I noticed that the Raspberry Pi was very unstable. It kept freezing up on me every few minutes. After trying out a few things, I suspected that the firmware on the device might be outdated. So I updated the firmware and the OS:

sudo rpi-update && sudo apt update && sudo apt upgrade && sudo apt dist-upgrade && sudo apt autoremove && sudo apt autoclean

After the update, I rebooted the Raspberry Pi, and the freezing went away. I thought everything was fine, but then I started hitting intermittent WiFi connectivity issues. Every few hours, Raspberry Pi would lose WiFi connection, and I couldn’t SSH to it anymore. I couldn’t get to the root cause of it, so I decided to apply a bandaid.

For that, I created this ‘checkwlan.sh’ script in ‘/usr/bin’:

#!/bin/bash
/bin/ping -c 1 8.8.8.8
if [ "$?" = "0" ]
then
echo "WiFi is Up"
else
echo "WiFi is Down"
/sbin/ifconfig wlan0 down
sleep 5
/sbin/ifconfig wlan0 up
fi

Then I set the right permissions on the script:

sudo chmod 755 checkwlan.sh

This script pings Google’s public DNS server (8.8.8.8) and then checks the return code. If the ping fails, it restarts the WiFi network interface, reconnecting to the WiFi.

To make it run automatically, I added it to the cron as root:

sudo crontab -e

This line needs to be added to the crontab to automatically run this script every minute:

* * * * * /usr/bin/checkwlan.sh

After that I rebooted the Raspberry Pi, and tested it out by manually bringing down WiFi connectivity on it. Within a minute, it recovered from the situation and reconnected to WiFi.

So far, so good. Now I will let it run for a few days, and will update this post if there are any other reliability issues. Hope this helps in setting up a headless Raspberry Pi — keep making fun things!