How to SSH into Raspberry Pi: A Complete Step-by-Step Guide

Secure Shell (SSH) is a powerful tool that lets you remotely access and control your Raspberry Pi through the command line. With SSH, you can manage files, run programs, or configure your Raspberry Pi without needing a monitor, keyboard, or mouse connected.

This guide provides a step-by-step walkthrough for enabling SSH, connecting to your Raspberry Pi, and securing your connection. Whether you’re a beginner or a seasoned developer, SSH is a must-know tool for managing your Raspberry Pi.


What is SSH?

SSH (Secure Shell) is a protocol for remotely connecting to a device over a secure, encrypted network. It enables you to send commands, transfer files, and perform administrative tasks from another computer or device.

Why Use SSH with Raspberry Pi?

  • Remote Management: Control your Raspberry Pi from another device, anywhere.
  • Headless Operation: Ideal for setups without a dedicated monitor or keyboard.
  • Secure Communication: SSH ensures encrypted data transfer, protecting your connection.

What You’ll Need

  1. Raspberry Pi: Any model running Raspberry Pi OS.
  2. MicroSD Card: With Raspberry Pi OS installed.
  3. Network Connection: Both Raspberry Pi and your client device must be on the same network for initial setup.
  4. SSH Client:
    • Windows: PuTTY or Windows PowerShell.
    • macOS/Linux: Built-in Terminal.
    • Mobile: SSH apps like Termius or ConnectBot.

Step 1: Enable SSH on Raspberry Pi

SSH is disabled by default in Raspberry Pi OS for security reasons. You can enable it manually.


Option 1: Enable SSH via Raspberry Pi Configuration (With Monitor)

  1. Connect your Raspberry Pi to a monitor, keyboard, and power supply.
  2. Boot into Raspberry Pi OS.
  3. Open the terminal and run:
    sudo raspi-config
  4. Navigate to Interface Options > SSH and select Enable.
  5. Save and exit the configuration tool.

Option 2: Enable SSH for a Headless Setup (Without Monitor)

  1. Insert the Raspberry Pi’s microSD card into your computer.
  2. Open the boot partition of the SD card.
  3. Create an empty file named ssh (no file extension) in the boot directory:
    • Windows: Right-click in the folder, select New > Text Document, name it ssh, and remove .txt.
    • macOS/Linux: Run the command:
      touch /Volumes/boot/ssh
  4. Eject the microSD card and insert it back into the Raspberry Pi. SSH will be enabled on boot.

Step 2: Find Your Raspberry Pi’s IP Address

To connect via SSH, you need the IP address of your Raspberry Pi.


Method 1: Using Raspberry Pi Terminal

If you have access to the Raspberry Pi, open a terminal and type:

hostname -I

This will display your local IP address (e.g., 192.168.1.100).


Method 2: Check Your Router

Log into your router’s admin panel (usually accessible at 192.168.1.1) and check the list of connected devices. Look for a device named raspberrypi or similar.


Method 3: Use a Network Scanner

Download a network scanning app like Fing (iOS/Android) to detect devices on your network. Look for the Raspberry Pi in the list.


Step 3: Connect to Raspberry Pi via SSH

Once SSH is enabled and the IP address is identified, connect using your preferred SSH client.


Option 1: Connect Using PuTTY (Windows)

  1. Download and install PuTTY.
  2. Open PuTTY and configure the connection:
    • Host Name (or IP address): Enter the Raspberry Pi’s IP address.
    • Port: 22 (default SSH port).
    • Connection Type: SSH.
  3. Click Open to start the connection.
  4. When prompted, enter your Raspberry Pi’s credentials:
    • Username: pi
    • Password: raspberry (default, unless changed).

Option 2: Connect Using Terminal (macOS/Linux)

  1. Open the Terminal.
  2. Enter the following command:
    ssh pi@<raspberry_pi_ip>

    Replace <raspberry_pi_ip> with your Pi’s IP address (e.g., 192.168.1.100).

  3. When prompted, enter the default password (raspberry).

Option 3: Connect Using Mobile (Termius or ConnectBot)

  1. Install Termius (iOS/Android) or ConnectBot (Android).
  2. Add a new SSH connection:
    • Host: Enter the Raspberry Pi’s IP address.
    • Port: 22.
    • Username/Password: Use the Raspberry Pi’s credentials.
  3. Tap Connect to access your Raspberry Pi.

Step 4: Secure Your SSH Connection

To prevent unauthorized access, it’s essential to secure your SSH connection.


1. Change the Default Password

Run the following command on your Raspberry Pi:

passwd

Follow the prompts to set a strong password.


2. Use SSH Key Authentication

  1. Generate an SSH Key Pair (On Your Computer):
    ssh-keygen -t rsa -b 4096

    Save the key to the default location.

  2. Copy the Public Key to Raspberry Pi:
    ssh-copy-id pi@<raspberry_pi_ip>
  3. Disable Password Authentication:
    Edit the SSH configuration file on the Raspberry Pi:

    sudo nano /etc/ssh/sshd_config

    Change PasswordAuthentication to no and save the file. Restart SSH:

    sudo systemctl restart ssh

3. Change the Default SSH Port

Edit the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Change the Port value to a non-standard number (e.g., 2222). Restart the SSH service:

sudo systemctl restart ssh

Troubleshooting Common Issues

1. Connection Refused

  • Cause: SSH service is not running.
  • Fix: Start the service:
    sudo systemctl start ssh

2. Permission Denied

  • Cause: Incorrect username or password.
  • Fix: Use pi as the username and your updated password.

3. Timeout or No Response

  • Cause: Network issues or incorrect IP address.
  • Fix: Verify the IP address and ensure the Raspberry Pi is connected to the network.

FAQs

1. Can I SSH into Raspberry Pi from another network?
Yes, set up port forwarding on your router or use a VPN for secure remote access.

2. What if I lose the default Raspberry Pi password?
Boot into recovery mode, mount the filesystem, and reset the password using the terminal.

3. Can I use SSH for graphical applications?
Yes, enable X11 forwarding or use tools like VNC for a graphical interface.


Conclusion

SSH is an essential tool for remotely managing your Raspberry Pi, making it perfect for headless setups and advanced projects. By following this guide, you’ve learned how to enable SSH, connect from various devices, secure your connection, and troubleshoot issues.

Take full advantage of SSH to control your Raspberry Pi with ease and flexibility!