Useful, if we develop a DIY project with a Raspberry Pi, is to be able to access it remotely. It may be the case of an error, that we want to modify the implemented code or whatever reason. That is why we are going to explain what SSH is and how to configure it on your Raspberry Pi.
What is SSH ?
It is a secure communications protocol between two systems based on a client / server architecture. Secure Shell (SSH) allows users to connect to a host remotely. It differs from other communication protocols such as FTP or Telnet, in that SSH performs an encryption of the connection. This prevents a malicious observer monitoring the communication from obtaining passwords and other data, since the information is encrypted.
Configure and enable SSH on a Raspberry Pi
We are now going to configure SSH to remotely access our Raspberry Pi.
Raspberry Pi OS operating system has SSH service disabled by default. To enable it, we must perform the following steps from the desktop:
- We go to the «Raspberry Pi Configuration» from the «Preferences» menu
- Find the “Interfaces” tab
- We select the “Enable” box next to “SSH”
- We click on «OK»
We can also do it through terminal
- We open a terminal window
- We write “sudo raspi-config” in the terminal window
- We select «Interface options»
- We navigate and choose “SSH”
- We choose the option «Yes»
- Now we press «OK»
- Finally, we give «Finish»
Alternatively we can use “systemctl” to start the service
sudo systemctl enable ssh sudo systemctl start ssh
When we enable SSH on a Raspberry Pi that has an internet connection, it is important to change the default password to ensure security.
Enable offline SSH
The Raspberry Pi does not need to be connected to enable SSH. We can do this by placing a file named “ssh”, without any extension, on the boot partition of an SD card from another device. When the Raspberry Pi starts it does a search for the “ssh” file. If the file is found, SSH will be enabled and the file will be deleted. What is inside the file does not matter at all, it can be completely empty.
If we install the Raspberry Pi OS operating system on a blank SD card, we will see that there are two partitions. The smaller of the two is the boot partition. We must add the “ssh” file to this partition.
We now go with the configuration of SSH access for Linux or Mac OS distributions , or simply to connect from another Raspberry Pi without the need for additional software.
To connect to the Raspberry Pi, we need to know its IP address, if we don’t have it. If we don’t have it yet, we write the text hostname -I in the terminal of the Raspberry Pi . In case we are using a Raspberry Pi offline, we can look at the list of devices connected to the router.
Now to connect to the Raspberry Pi from a computer with a Linux or Mac OS distribution we must use the command that we will be able to follow below. You must change the <IP> text to the IP address of our Raspberry Pi.
If we have configured a different username for our Raspberry Pi, we will have to change the pi header to our username. For example: ssh firstname.lastname@example.org
Two things can happen now:
- If we receive a connection timed out error, there is a possibility that the IP entered is wrong. We will have to correct it.
- If the connection is successful, we will see a security / authenticity warning. We must write yes to continue. This warning will only appear the first time we connect.
We can find the case that the Raspberry Pi is using an IP address to which the computer has previously connected. This could reflect on the screen a warning that asks us to delete the registry from the list of known devices. We must follow the instructions and go through the ssh steps again to be successful.
If everything goes well, we will be asked for the username and password of the Raspberry Pi to which we are trying to connect. This username and password is important to change for security. The default username for a Raspberry Pi is pi and the default password is raspberry .
Now a command line should appear. The identifier of the Raspberry Pi will be the same as on our board. If we have changed it, the identifier will be the same as we have entered, if we have not changed it, it will be pi .
pi@raspberrypi ~ $
We are now remotely connected to the Raspberry Pi and we can start entering commands.
X-forwarding para Mac OS X
Additionally we can forward the session “x” through SSH to allow the use of graphical applications, making use of the -Y flag :
ssh -Y email@example.com
We are now remotely connected to the Raspberry Pi, and we can start entering commands.