--- ---

Raspberry Pi Headless Setup

19 Jul 2020 - Courtney McBeth

Install Raspberry Pi OS on an SD Card

A micro SD card must be used as the main memory for the Raspberry Pi. Typically, 16 GB is sufficient. Before it can be used, the SD card must be formatted and flashed with the Raspberry Pi OS. This can be done using the Raspberry Pi Imager. Insert the SD card into your computer. If it has already been used to store files, these will need to be deleted so that its memory is empty. Open the application and select the recommended Raspberry Pi OS (currently Raspbian Buster) as the operating system. Choose your SD card and select write. Now, we’ll need to configure some settings so that we can connect to the Raspberry Pi.

Configure the Raspberry Pi for Headless Operation

Unlike an Arduino, a Raspberry Pi is a single board computer. This means that it’s more like a desktop with a monitor, mouse, and keyboard than a microcontroller. Obviously, we want to be able to use the Raspberry Pi without all of these extra peripheral devices. To do this we are going to configure it for SSH, which, simply put, allows us to connect to another computer over Wi-Fi.

Make sure that the SD card is inserted into your computer (it should now be named boot). Add a file to the SD card named ssh (make sure there is no file extention - .txt or anything like that). This file should not contain anything, it just tells the Raspberry Pi to enable SSH.

Add another file to the SD card, called wpa_supplicant.conf. This one should contain the following (replace the SSID and password):


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

network={
 ssid="your_ssid_here"
 psk="your_password_here"
}


Note (7/19/20): this only works for WPA/WPA 2 networks. For WPA/WPA 2 Enterprise networks, like Cornell’s Wi-Fi, this solution may work. I’ll look into it.

Now, eject the SD card and plug it into the Raspberry Pi.

Connect to the Raspberry Pi

Power on the Raspberry Pi and make sure that your computer is connected to the same network as the Raspberry Pi. Open your terminal (or command prompt) and enter:


ssh pi@raspberrypi.local


It will ask you for a password. The default is raspberry, but we will change this later.

Since this is the first time we’re starting up the Raspberry Pi, let’s look for software updates. Run these commands (they may take a while to execute):


sudo apt-get update



sudo apt-get upgrade


After an upgrade, it’s a good idea to reboot. Run the following to restart the Raspberry Pi:


sudo reboot


This will disconnect you from the Raspberry Pi. Give it a minute to reboot, reconnect via SSH, and reenter the password. The default password for the Raspberry Pi is incredibly guessable, so let’s change it. Enter the following command:


passwd


When prompted, enter a memorable new password.

Setup VNC Viewer

SSH is a good tool if we’re okay with typing all of our code in the command line or using SCP to transfer all of our files from our computers to the Raspberry Pi. Fortunately, we have other options. We can use VNC (Virtual Network Computing) to view the Raspberry Pi’s GUI on our computers. Install Real VNC on your computer and open it up. Now, start VNC on the Raspberry Pi over SSH using the following command:


vncserver


You should see something like this:


New desktop is raspberrypi:1 (192.168.0.21:1)


This tells you the instance of raspberrypi that you’ll need to connect to. Here, as given by raspberrypi:1, the instance is 1. In Real VNC, enter the address of the Raspberry Pi in the text box at the top of the window. In this case, the address is raspberrypi.local:1, but if you were given a different instance number, enter that instead of 1. Enter the username (pi) and password for the Raspberry Pi. Now, you’ll see the Raspberry Pi’s GUI, which you can use to control it.

Change the Hostname

The default hostname of any Raspberry Pi is raspberrypi, so if you have multiple Raspberry Pis, you may run into namespace errors when trying to SSH. To avoid this, let’s make sure that each Raspberry Pi on our network has it’s own unique hostname. There is a way to do this over the command line, but you would get disconnected and have to reconnect multiple times, so we’ll use VNC. While connected to the Raspberry Pi over VNC, open the applications menu in the toolbar and go to Preferences -> Raspberry Pi Configuration (this may require you to enter the password). Change the hostname to something memorable. Next, reboot the Raspberry Pi. VNC and SSH will be disconnected. Reconnect over SSH and VNC while changing the previous hostname, raspberrypi to your new hostname. The new SSH command will be:


ssh pi@new_hostname_here.local


And the new connection name for VNC will be something like new_hostname_here.local:1 (again change 1 to whatever instance you’re given if it differs).

Now, your Raspberry Pi is ready to use!