[SSH]: Remote SSH to a Raspberry Pi without password (macOS, Linux)

bash

mac

09/25/2020


Overview

This post will show you how to establish SSH remote connection to a Raspberry Pi or other devices without using password via ssh-keygen

1. Connect to Raspberry Pi on terminal

BASH

Refer to this page if you need to find out local ip address of pi

2. Generate authentication key on pi

BASH
ssh-keygen -t rsa -b 4096 -C "[email protected]"

-t rsa defines encryption type

-b 4096 uses 4096 bits

-C provides a comment (in this example, email)

This will prompt you to enter directory and passphrase of the key. Feel free to press enter to skip these prompts. The default directory is ~/.ssh where the two keys will be saved: id_rsa and id_rsa.pub

id_rsa.pub vs id_rsa

id_rsa is a private key, used to sign into a remote host

id_rsa.pub is a public key that needs to be supplied to the remote host via authorized_key file

3. Add generated key to authorized key

BASH
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_key

This will generate authorized_key that contains the public key

4. Set permissions for authorized key

BASH
chmod go= ~/.ssh/authorized_key
chmod u=r ~/.ssh/authorized_key

go= adds other users for permission. g indicates other uses in the file's group, and o indicates other users not in the file's group.

u=r gives the owner of the file, u, permission to read

5. Move generated key to your device

Logout from pi machine to go back to your device that you wish to use to connect to pi. You'll need the private key, ~/.ssh/id_rsa, on your device in order to sign into the remote host, pi.

Example
BASH
cd ~/Documents
# Copies the private key from pi to the current directory of your device
scp [email protected]:/home/pi/.ssh/id_rsa .

6. Connect without typing in password

Finally, use the below command to connect to pi without password

Example
BASH
ssh -i ~/Documents/id_rsa [email protected]

WRITTEN BY

Keeping a record