Setting up a Raspberry Pi without a monitor (headless)

Eric Bezzam
6 min readOct 7, 2021

--

With the Raspberry Pi 4 opting for micro HDMI ports, not everyone may have an adapter handy. Or you may not have an external screen to hook up your Pi. In this tutorial, we will see how to set up a Raspberry Pi without a monitor, also known as headless.

Photo by Vishnu Mohanan on Unsplash

What you need:

  • A laptop with an SD card slot.
  • A Raspberry Pi.
  • A power supply (USB-C for Raspberry Pi 4 and micro USB for the others).
  • A microSD card (at least 8GB) and adapter.
  • Wireless local network + credentials.
  • (Optionally) Ethernet cable if no WiFi or connecting to WPA Enterprise WiFi (e.g. at a university). Instructions for that are at the bottom.

Flashing the SD card

The most convenient way to flash an SD card for Raspberry Pi is with the Raspberry Pi Imager. Download the software for your OS from the official website.

Insert the SD card adapter (with your microSD card inside) into your laptop and launch the software.

Choose the OS. Raspberry Pi OS (32-bit) is usually sufficient.

Note: as of November 8, 2021, Bullseye is the new Raspberry Pi OS. You can see what changes that entails from the Raspberry Pi’s official post. One significant change is the camera driver (no more raspistill and raspivid) so if that or another change is not compatible with your project, you can still download the last version of Buster here (ZIP file), and use the “Use custom” option when selecting the Operating System, and select the ZIP file.

Choose the storage device. The SD card you inserted should appear.

Before pressing that Write button, we will modify the Advanced options so that on boot:

  • The Raspberry Pi will connect to your WiFi.
  • SSH will be enabled.

Newer versions (as of March 23, 2022) have a “Gear” button in the lower right corner to modify these settings.

If you do not have the “Gear” button, you can use the keyboard shortcut Ctrl + Shift + X to access these options.

Enable SSH and (optionally) set the hostname and password.

Configure the WiFi and press Save.

Now press Write!

After a few minutes, your SD card should be successfully flashed with the Raspberry Pi OS.

Before removing the SD card adapter, safely eject it from your file manager.

Connecting to the Raspberry Pi

Insert the flashed microSD card into your Raspberry Pi and power it up.

From your local machine, open up a Terminal window and enter the following command, replacing raspberrypi.local with the hostname you set above.

ssh pi@raspberrypi.local

If prompted for a password, enter the one you defined above. Check out this article for how to connect passwordless.

Logging out

Ctrl+ D will exit the SSH connection. To shutdown the Raspberry Pi you can run:

sudo shutdown -h now

Troubleshooting

SD card is write protected

If you are not able to write to your microSD card, be sure that the SD card adapter is not in the Lock position, namely the grey slider should be all the way up. Below is how the SD card will appear if in the Lock position, namely you will see “WRITE PROTECTED”.

It could be that the grey slider moves as you insert the SD card adapter. If that is the case, try inserting it slowly.

Connection refused

If you find yourself with the error below when trying to SSH, you are probably not connected on the same local network as the Raspberry Pi.

ssh: connect to host raspberrypi.local port 22: Connection refused

It could be that the WiFi credentials were not entered correctly.

After having flashed the SD card, you can configure the WiFi (without having to reflash) by inserting the SD card into your laptop and creating/saving a file called wpa_supplicant.conf in the boot folder of the SD card. Its contents should resemble below, with your WiFi credentials.

# Giving configuration update rights to wpa_cli 
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
# ISO/IEC alpha2 country code in which the device is operating country=<XX># network section
network={
ssid="<network_1>"
psk="<password_1>"
}
# can configure multiple networks
network={
ssid="network_2"
psk="password_2"
}

You will also need to add an empty file called ssh into the boot folder. This will enable SSH.

No WiFi? No problem

If you do not have an available WiFi network, you can connect the Raspberry Pi to your laptop with an Ethernet cable. The same SSH command should work.

Connecting to WPA Enterprise WiFi (e.g. university WiFi)

Check the EPFL guidelines for Linux, for the latest settings. The configuration below was tested on Sep 5, 2022.

You will have to edit the /etc/wpa_supplicant/wpa_supplicant.conf file as shown here, namely add the following network configuration (changing identity and password).

ctrl_interface=/var/run/wpa_supplicantnetwork={
ssid="epfl"
key_mgmt=WPA-EAP
proto=WPA2
eap=PEAP
identity="gaspar@epfl.ch"
password="my_password"
anonymous_identity="anonymous@epfl.ch"
phase2="auth=MSCHAPV2"
ca_cert="/etc/ssl/certs/DigiCert_Global_Root_CA.pem"
subject_match="CN=radius.epfl.ch"
priority=8
}

If you don’t want to leave your university password out in the clear like that, you can encrypt it by running the following command

echo -n password_here | iconv -t utf16le | openssl md4

Which may output something like below

(stdin)= XXXXXXXXXXXXXXXXXXXXXXXXXXXX

You can then replace the password line like so

password=hash:XXXXXXXXXXXXXXXXXXXXXXXXXXXX

On a WPA enterprise WiFi, you may not be able to SSH wirelessly with the hostname (raspberrypi.local above). For that, we may need to know the IP address. An Ethernet cable will be necessary here (otherwise you can connect to the screen to run ifconfig.)

Connect your local machine to the Raspberry Pi with an Ethernet cable, and you should be able to SSH with the hostname, namely:

ssh pi@raspberrypi.local

On the Raspberry Pi, run ifconfig in order to determine the IP address. It should appear with wlan0, next to inet.

...wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet XXX.XXX.XXX.XXX netmask 255.255.255.0 broadcast 192.168.1.255
...

For the latest Buster OS, I also had to make some changes to /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant, as described here. This was not necessary for the Bullseye OS.

And finally restart!

You should now be able to SSH wirelessly.

ssh pi@XXX.XXX.XXX.XXX

Note: this IP address will most likely change each time you start up the Raspberry Pi so you would have to repeat this process with the Ethernet cable (or a screen) to determine the new IP address.

--

--

Eric Bezzam

PhD student at EPFL. Previously at Snips/Sonos, DSP Concepts, Fraunhofer IDMT, and Jacobs University. Most of past work in audio and now breaking into optics!