Securely Access Your Raspberry Pi Remotely Using macOS Terminal

Learn how to connect your Raspberry Pi to a remote server using the secure shell (SSH) protocol and the macOS terminal. …


Updated October 13, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to connect your Raspberry Pi to a remote server using the secure shell (SSH) protocol and the macOS terminal.

Raspberry Pi is a versatile single-board computer that can be used for various purposes, including web servers, home automation systems, and even gaming consoles. One of the most common tasks when working with Raspberry Pi is remote access to it via SSH (Secure Shell). In this article, we will guide you through the steps on how to SSH into your Raspberry Pi from a macOS device using the Terminal application.

Prerequisites:

  • A Raspberry Pi with Raspbian OS installed and configured
  • An Ethernet or WiFi connection for the Raspberry Pi
  • macOS device with built-in Terminal app (tested on macOS Catalina)

Step 1: Checking Your Raspberry Pi’s IP Address

Before you can connect to your Raspberry Pi remotely, you need to know its IP address. To find the IP address of your Raspberry Pi, follow these steps:

  1. Open a terminal window on your Raspberry Pi by going to Menu -> Accessories -> Terminal.
  2. Type ifconfig and press Enter. You should see something like this:
eth0      Link encap:Ethernet  HWaddr b8:27:eb:xx:xx:xx  
          inet addr:192.168.1.104  Bcast:192.168.1.255  Mask:255.255.255.0

In this example, the IP address of your Raspberry Pi is 192.168.1.104. Note it down as you will need it later.

Step 2: Enable SSH on Your Raspberry Pi

By default, Raspbian OS comes with SSH server disabled for security reasons. To enable SSH, follow these steps:

  1. Open the terminal window on your Raspberry Pi and type sudo raspi-config and press Enter.
  2. Navigate to Interfacing Options -> SSH and select “Enable” or “Yes”.
  3. Exit the configuration tool by selecting Finish and reboot your Raspberry Pi with sudo reboot.

Step 3: Generating an SSH Key Pair on Your Mac

SSH keys are a more secure way to authenticate yourself when connecting to a remote server. To generate an SSH key pair, follow these steps:

  1. Open the Terminal app on your macOS device.
  2. Type ssh-keygen -t rsa and press Enter. This will create a new RSA key pair in your home directory (~/.ssh/).
  3. Leave all options as default by pressing Enter until the process is complete. You should see a message like this:
Your identification has been saved in /Users/yourusername/.ssh/id_rsa.
Your public key has been saved in /Users/yourusername/.ssh/id_rsa.pub.

Step 4: Copying the SSH Key to Your Raspberry Pi

To allow your macOS device to connect securely to your Raspberry Pi, you need to copy the public key to the Raspberry Pi’s authorized keys file. Follow these steps:

  1. Connect to your Raspberry Pi using its IP address and username (usually pi). Replace <raspberry_ip> with the actual IP address of your device:
ssh pi@<raspberry_ip>
  1. When prompted, enter the password for the pi user.
  2. Once connected, create a .ssh directory in the home folder if it doesn’t exist:
mkdir ~/.ssh
chmod 700 ~/.ssh
  1. Create an authorized_keys file and set appropriate permissions:
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
  1. Exit the Raspberry Pi session by typing exit.
  2. Copy your SSH public key to the Raspberry Pi using the following command:
cat ~/.ssh/id_rsa.pub | ssh pi@<raspberry_ip> "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"

This will append the contents of your local id_rsa.pub file to the Raspberry Pi’s authorized_keys file.

Step 5: Testing Your SSH Connection

You can now test your SSH connection by typing the following command in the Terminal app on your macOS device:

ssh pi@<raspberry_ip>

If everything is set up correctly, you should be connected to your Raspberry Pi without entering a password. You can execute commands on your Raspberry Pi just like you would locally. For example, you could run ls to list files in the home directory or sudo reboot to restart the device remotely.

Conclusion

Congratulations! You have successfully set up an SSH connection between your macOS device and Raspberry Pi. With this secure connection, you can now access and control your Raspberry Pi remotely without being physically present. This is particularly useful for headless setups where the Pi is not connected to a display or keyboard. Additionally, using SSH keys provides an extra layer of security by eliminating the need to enter a password every time you connect.