In one of my earlier posts I talked about how to setup the Raspberry Pi in the least stressful way. Now, I’ve been using the Raspberry Pi for a couple of years and truth be told, I hardly ever connect a monitor, keyboard or mouse directly, no, I tend to use the Raspberry Pi headless.
This means that I am using my laptop to access the Raspberry Pi via a network: either WiFi or occasionally Ethernet, through SSH or using the VNC Viewer. However, for the longest time, how I would always setup Raspbian on the Pi using NOOBS and the whole assemblage of monitor, keyboard and mouse. Till recently when I asked myself, why am I not setting up the Pi the way I typically use it?
So this post is basically a guide on how to setup the Raspberry Pi headless. There are some hiccups that I typically encounter and solutions to those problems will be outlined as well.
In order to setup up the Raspberry Pi headlessly, you will need the following equipment:
- A Raspberry Pi (I’m using the Pi 3B+ in this tutorial)
- A microSD card with Raspbian flashed on it (32 GB is my preferred choice)
- An SD Adapter for the microSD card.
- A computer with an SD card reader
- Ethernet cable or WiFi
- Power adapter for the Pi
DOWNLOADING THE RASPBIAN IMAGE
The first thing that you’d need to do is to flash the Raspbian image onto the microSD card. The preferred type of microSD card to use would be a Class 10 as it should give you decent read and write speeds, so if your are getting a new one, try and get a Class 10. Size I’d say 16 GB going up, though these days I see 32 GB cards are relatively inexpensive on Amazon.
To get the latest version of Raspbian head to the Raspberry Pi website’s download section (at the time of writing this the latest version was Raspbian Buster). There typically are three versions that you can get:
Choose whichever suits your needs; I typically just get the first option, Raspbian Buster with desktop and recommended software, and download the image as ZIP (or torrent is you like).
FORMATTING THE microSD CARD
So now that you have your zipped Raspbian image, you’re going to need to unzip it with a trusty file archiver like 7zip. It should unzip to a folder with Disc Image File that is approximately 5 GB and that’s why you’d probably want to get a 16 GB microSD card or larger, actually just get a 32 GB microSD card or larger.
Once you have the extracted Disc Image File, you need to flash this image onto your SD card.
But before flashing you need to format your microSD card and a good option to use to do this is SD Card Formatter.
You simply select your card’s drive letter, the formatting option (Quick format is good enough) and you Format the card away. Typically takes a couple of seconds.
FLASHING THE RASPBIAN IMAGE
Next you need to flash the Raspbian image onto the microSD card. For a seemless flashing experience use Etcher, however, for this tutorial I’ll use Rufus because it poses an interesting problem that I’ll provide a solution for.
Launch Rufus, select your image and start the flashing process. Should take a couple of minutes depending on your computer’s specs and the speed of the SD card that you are using.
THE PROBLEM WITH RUFUS
Once the flashing is complete eject the microSD card and reinsert it. Now if all goes well, there should be a drive on your microSD card called boot but for some reason, this folder isn’t always available to be by default, I always have to carry out a few extra steps before it shows up. However, if you use Etcher, this step won’t be necessary.
First of all you need to open Disk Management by pressing the Windows Key + X and selecting it from the menu that appears
In the Disk Management window that opens, if you locate your microSD card, you will see that indeed a folder by the name boot does exist. The problem is that drive letter hasn’t been assigned to it. To rectify this simply right click on the boot partition and select Change Drive Letter
A new window will open and you’ll be able to assign the boot partition a Drive Letter, afterwards the boot partition should appear on Windows Explorer
Now the reason why we need access to the boot drive is because it shall aide us in enabling SSH and setting up WiFi for our Pi.
SETTING UP SSH
By default, SSH is disabled on the Raspberry Pi, so in order to enable it, we need open up Notepad create an empty file with the name ssh and make certain that the file doesn’t have an extension. If you do it right, your ssh file should like as shown below:
Next place this ssh file in the root of the boot partition.
SETTING UP WiFi
Now it is time to prep the Pi for WiFi. For this, open up Notepad and create a file called wpa_supplicant.conf. In this file, enter the following text:
ssid="The Name of Your WiFi Network"
psk="The password to your WiFi Network"
In the “country” line enter the code for your country, for instance KE is for Kenya; TZ would be for Tanzania, NG is Nigeria; you can use this list as a reference for other country codes (the code to use is the Alpha-2 Code with 2 characters). Technically the country can be ignored, but the Pi will pop up a lot of warnings if this line is missing when you start using the Pi, so it’s best to include it. Then also configure the ssid and psk with the WiFi name and WiFi password respectively.
Save the file and drop it in the root of boot and we are good to go.
Eject the microSD card from the PC and place it in the Raspberry Pi. If you choose to use Ethernet instead of WiFi, connect your Ethernet cable to the Pi and your computer and power up the Pi.
COMMUNICATING WITH THE PI WITH PuTTY
Now, if you choose to use the Ethernet cable to communicate with the Pi, it is best to ensure that WiFi is turned off on your computer to make communication easier, and also if using WiFi, make sure Ethernet isn’t connected. This is just a little precaution.
Let the Pi boot up for maybe a couple of minutes, and then we are going to open up PuTTY. PuTTY is a free and open-source terminal emulator, serial console and network file transfer application. This is what we’ll use to SSH into the Pi. Setup PuTTY as shown below:
The good this about recent releases of the Pi is that you don’t really need to know the IP address of the Pi explicitly, if you simply use “raspberrypi.local” as the address, you’ll be able to gain access; this works for both Ethernet and WiFi.
If all goes well you should get the following screen:
Where the default username is pi and the default password is raspberry. And if you successfully login you will get the following screen:
And voila, you have successfully setup your Raspberry Pi headlessly.
SETTING UP VNC TO ACCESS THE RASPBIAN DESKTOP
If you want to access the Raspbian desktop, you’ll need to turn on the VNC server on the Pi. To do this from the command line enter sudo raspi-config
It should bring up a menu from which you want to select the Interfacing Options menu item
This will bring up a list of interfaces that you can activate, what you are interested in is the VNC option
Select the option and enable VNC
You can now exit the menu and VNC should be activated.
Now if you head on over to the VNC Viewer program on your computer and log in to your Raspberry Pi (you can use the raspberrypi.local ip address, and the username and password is the same as that used to login to the SSH session with PuTTY), VNC Viewer may put out the message, Cannot currently show the desktop
This is easy to fix, you simply need to change the resolution. Using PuTTY to access the Pi’s command line, head back to the configuration menu using sudo raspi-config, this time around select the Advanced Options menu item
Then select Resolution to change the resolution.
The default resolution is like 720×480 which I guess VNC Viewer was having a hard time. So choose something larger, I went with 1920×1080 because that’s the resolution for my computer.
Save the changes, exit the configuration menu and reboot the Pi. Once you’ve rebooted the Pi and you log in through VNC Viewer, you should have access to the Raspbian Desktop
And you can go about finishing your setup or what have you.
So setting up the Raspberry Pi headless isn’t too much of a hustle. This method is actually faster than using NOOBS, it could have a couple of hiccups here and there, but they are solvable.