How to Login to Raspberry Pi Using SSH

Jinku Hu Feb 02, 2024
  1. SSH Remote Access Basics and Raspberry Pi OS
  2. Log in to Raspberry Pi via SSH Using Password
  3. Use GUI Applications via SSH on Raspberry Pi
How to Login to Raspberry Pi Using SSH

This article will introduce several methods of how you can log in to Raspberry Pi using SSH.

SSH Remote Access Basics and Raspberry Pi OS

SSH (Secure SHell) is a network protocol that provides secure remote access to another host. Even though we will refer to SSH as software or a tool, it’s important to denote that the term is used as a generic name for the protocol.

In contrast, the actual software used on the given operating system is formally referred to as the implementation of the SSH protocol. Generally, there are multiple implementations of SSH protocol and corresponding utilities that can be installed on the given operating system.

The most commonly used free implementation is the OpenSSH package, and it’s usually part of almost every Linux distribution, including the Raspberry Pi OS.

SSH acts as client-server architecture software, so you’d need to have client packages installed on the system from where you can access Raspberry Pi. On the other hand, Raspberry Pi needs to be running the SSH server, which is installed by default, but you may need to enable it manually.

You can still check if your Raspberry Pi has the SSH installed with the following command, which prints the version of the software package:

ssh -V

If the previous command does not print the valid response starting with the string - OpenSSH_, you must install the needed packages using the next command.

sudo apt install openssh-server

Next, you need to enable the SSH server as it’s usually disabled by default Raspberry Pi OS configuration. The following command will enable the SSH daemon, which is the program that lets you log in:

sudo systemctl start ssh.service && sudo systemctl enable ssh.service

The previous command automatically enables the SSH daemon to be run on system startup. If you would like to investigate if the SSH service is running at the moment, you can utilize the following command.

sudo systemctl status ssh.service

Log in to Raspberry Pi via SSH Using Password

When connecting to a host using SSH, you need to specify the user assumed to exist in the host system. In this case, we assume that the default pi user exists in the Raspberry Pi OS that we are trying to access. Additionally, we need to know the IP address of the Pi host, which depends on the network it’s located in. You can retrieve the IP address using the following command, but it needs to be executed on the Pi:

ip a

Once we know the username and the IP address (e.g., 192.168.0.11), we can insert them in the SSH command and access the host as shown in the following command. Note, though, that commands in this article will be compatible with Unix-based systems such as Linux or Mac OS.

ssh pi@192.168.0.11

The latter command usually will generate a shell session for the pi user where you can execute commands remotely as needed. Although, when you connect to a given host for the first time, it shows an authenticity verification prompt where you’d need to provide a yes reply to continue. Notice that the latter step is done to save the host identification and prevent man-in-the-middle attack to some degree.

Use GUI Applications via SSH on Raspberry Pi

SSH supports X11 forwarding, allowing graphical interface programs to be displayed on a remote system. Even though the feature seems quite powerful, it has some security implications, which is mentioned in the SSH manual, and better to avoid using it unless you fully understand the behavior.

The following SSH command will enable the X11 forwarding and connect to the host. Note that X11 forwarding must usually be enabled on the server-side(Raspberry Pi), but since the default Pi OS configuration includes it enabled, we skip this step in this guide.

ssh -Y pi@192.168.0.11
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - Raspberry Pi

Related Article - Raspberry Pi Login