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:
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:
And to configure WiFi, add this file:
The contents of this file should look something like:
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:
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/ping -c 1 220.127.116.11
if [ "$?" = "0" ]
echo "WiFi is Up"
echo "WiFi is Down"
/sbin/ifconfig wlan0 down
/sbin/ifconfig wlan0 up
Then I set the right permissions on the script:
sudo chmod 755 checkwlan.sh
This script pings Google’s public DNS server (18.104.22.168) 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!