How to enable SSH on Raspberry Pi

Today's article will focus on one topic that many people are interested in: Everything related to SSH connections to the Raspberry Pi.

 

How to connect to the Raspberry Pi using SSH

From Windows

If you use Microsoft Windows on your primary computer, note that there are no tools for SSH connections installed by default.

That's why you'll have to download and install an app, like Putty. Putty is one of the most used software to use SSH on Windows.

  1. How to install PuTTY on Windows

Download files that match your computer's specifications. Depending on the version selected, install it or not, then launch Putty.

Putty looks like this:

How to enable SSH on Raspberry Pi Picture 1How to enable SSH on Raspberry Pi Picture 1 Putty interface

To login enter the IP address in the Host Name field and click Open.

The default SSH credentials are:

- Login: pi

- Password: raspberry

If things are not working now, don't worry, the article will explain what to do later.

From Linux or Mac

If you use a Unix-based system i.e. any Linux distribution, or even macOS, you'll have to use the command 'ssh' to connect from a terminal.

Sometimes you need to install SSH, for example on a Debian-based distribution:

sudo apt-get update sudo apt-get install ssh

The command to connect to the Raspberry Pi will look like this:

ssh pi@IP

Replace the IP with the IP address of the Raspberry Pi.

How to enable SSH on Raspberry Pi

From the desktop

For security reasons, SSH is not enabled by default on the Raspberry Pi. You have to enable it yourself by going to Start menu > Preferences> Raspberry Pi Configuration .

Go to the Interfaces tab and enable SSH.

How to enable SSH on Raspberry Pi Picture 2How to enable SSH on Raspberry Pi Picture 2 Go to the Interfaces tab and enable SSH

From a terminal

Similarly from the terminal, you will also have to manually start SSH.

To do this, enter the following command:

sudo service ssh start

You can also check the service status by typing:

sudo service ssh status

How to automatically start SSH when booting

You will quickly find that SSH, even when enabled, will not start up automatically. So you have to find a way to force it to launch.

There are several ways to do this. We have chosen to add a cron for root to launch SSH every time:

Step 1. Edit root crontab like this:

sudo crontab -e

Step 2. Add this line to the end of the file:

@reboot /usr/sbin/service ssh start

If you haven't understood this yet, don't hesitate to read our guide on how to schedule tasks on your Raspberry Pi.

IP address and port to connect

By default, SSH starts on port 22.

Therefore, you must connect to the Raspberry Pi's IP pointing to port 22.

Example from Linux:

ssh pi@192.168.1.200

Or if you have changed the SSH port, specify the port with the -p option :

ssh pi@192.168.1.200 -p2222

Replace 2222 with the selected port.

How to connect SSH without password

Now you know how to enable SSH on Raspberry Pi and how to connect to it. But if you have to do it regularly, typing in your password every time will quickly become boring.

There is a way to create an SSH key that identifies you in place of the user's password. This article will explain how to set up this key according to your operating system.

Of course, the password connection will still be possible, if you forget the key or use another computer.

 

From Windows

First things first, on Windows you will need the full Putty toolkit, with PuttyGen and PuttyAgent. So if you've just downloaded the SSH client to the portable version, you have to install all the rest.

You will also need a tool to transfer files to your Raspberry Pi. WinSCP is a good choice, worth considering.

Then follow these steps:

Step 1. Create SSH keys with PuttyGen:

  1. Launch PuttyGen.
  2. Select RSA as the key type.
  3. Enter 4096 as the number of bits.
  4. Click Generate.
  5. Leave all other options as default, no passphrases.
  6. Save the key where you want.

Step 2. Transfer public key to Raspberry Pi using WinSCP:

  1. Add a new location with Raspberry Pi login and IP information.
  2. Connect with this location.
  3. On the left side, go to the folder where you saved your key.
  4. On the right side, go to the / home / pi directory.
  5. Transfer public key to Raspberry (drag and drop).

Step 3. Allow this key to work on Raspberry Pi:

  1. Connect to Raspberry Pi using Putty.
  2. Add the transferred public keys to the allowed keys:
  3. cat /home/pi/you_key.pub >> ~/.ssh/authorized_keys
  4. Close Putty.

Step 4. Try to reconnect with Putty.

  1. This step does not require you to enter a password and log in directly.
  2. If you still need to log in, enter pi @ IP in the Hostname field (replace the IP with the IP address of the Raspberry Pi).

From Linux or Mac

Step 1. If not, create the SSH key with the following command:

ssh-keygen -t rsa

Just click Enterfor each question, default path and no password required.

Step 2. Copy the public key file to the Raspberry Pi:

cp ~/.ssh/id_rsa.pub pi@IP:/home/pi/your_key.pub

Replace IP with Raspberry Pi IP address, name the key as you like.

Step 3. Add this key to the keys allowed to operate on the Raspberry Pi with the command:

cat /home/pi/you_key.pub >> ~/.ssh/authorized_keys

Step 4. Try connecting again:

ssh pi@IP

You do not need to enter a password and can log in directly.

4.3 ★ | 3 Vote