How to Connect via SSH to a Raspberry Pi

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to enable SSH on a Raspberry Pi and connect to it via SSH. This is especially useful when trying to access a headless Raspberry Pi where there is no desktop environment.

1. Image Your Raspberry Pi SD Card

The first step is to image your SD card with Raspberry Pi OS. Download the Raspberry Pi Imager here, and install it on your computer.

Open the Raspberry Pi Imager and click on CHOOSE OS. You can pick any flavor of Raspberry Pi OS, but we will be using the Lite (32-bit) version with no desktop environment which is under the Raspberry Pi OS (other) section. This is the first option in the screenshot below.

Raspberry Pi operating system list

Next, insert an SD card that’s 4 GB or bigger and click on CHOOSE SD CARD. Pick your SD card from the list.

Finally, you can click on WRITE. This will erase everything on the SD card.

The process will take a few minutes. When it’s done, physically take the SD card out of your computer, and re-insert it. The name of SD card should be called boot.

2. Enable SSH Access on Raspberry Pi

Since the Raspberry Pi OS is headless which means there is no desktop environment, we will have to access it via SSH. If you’re not familiar with SSH, think of it as a way to login to your Raspberry Pi from your computer.

In order to enable SSH access on your headless Raspberry Pi, simply create an empty file called ssh at the root of your SD card. If you are on Mac, the follow command will do the trick:

touch /Volumes/boot/ssh

3. Set Up WiFi on Raspberry Pi

Again, because your Raspberry Pi is headless, we need a way to automatically have it connect to Wifi when it starts up so we can login via SSH. In order to do this, we need to create a file at the root of the SD card called wpa_supplicant.conf. Replace WIFI_NAME and WIFI_PASSWORD with the actual name and password for your WiFi network.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<Insert 2 letter ISO 3166-1 country code here>

network={
 ssid="WIFI_NAME"
 psk="WIFI_PASSWORD"
}

When you’ve done that, eject the SD card from your computer and insert it into your Raspberry Pi.

4. Find the IP Address of Raspberry Pi

Apply power to your Raspberry Pi and wait a few seconds for it to boot up.

In order to connect to the Raspberry Pi via SSH, we need to determine its IP address. To do this open Terminal on Mac/Linux (or Command Prompt on Windows) and  execute the following command.

ping raspberrypi.local

If your Raspberry Pi successfully connected to the WiFi network with the credentials you provided, you should see output similar to this.

Determine Raspberry Pi IP address

In this case, the IP address of the Raspberry Pi is 192.168.0.136.

5. Login to Raspberry Pi vis SSH

Now that we know the IP address of the Raspberry Pi, we can login via SSH.

On a Mac, open terminal and use the SSH command and IP address to login. If you are using Windows, please follow this tutorial to login to your Raspberry Pi via SSH with Putty. Of course, use the IP address of your Raspberry Pi.

ssh pi@192.168.0.136

The default Raspberry Pi username is pi and the default password is raspberry.

When you are logged in, your terminal window will look similar to this.

SSH access on Raspberry Pi

6. Give Raspberry Pi a Hostname (optional)

If you’d like, you can assign your Raspberry Pi a hostname instead of using the IP address directly. In order to map a hostname to the IP address of your Raspberry Pi, you simply need to edit the hosts file on your computer.

For Mac and Linux users, you can watch the video below, and Windows users can learn how to edit the hosts file here.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment