Sushant S. Bhosale

How to SSH into Raspberry Pi without password

How to SSH into Raspberry Pi without password

I have more than three Raspberry Pi in the house. It can get tiring to connect to each one with a password. One way to avoid having to type a password during SSH is to use SSH Key.

SSH(Secure Shell) Key is basically a really really long (about 600 chars long password). The most commonly used is the 2048 RSA encryptions.

Step 1: Check for existing keys
If you have ever created a SSH Key, you can use the same one or create a new one. To check if you already have SSH key on your machine, goto the folder:

cd ~/.ssh
ls

OR

ls ~/.ssh

If you see any file in there that ends with *.pub that is your public key. You should also see a private key without the .pub extension with same name. Below you can see id_rsa and id_rsa.pub are the keys on my machine.

config		id_rsa		id_rsa.pub	known_hosts

If you want to use the same keys, then skip the next step to generate new ones.

Step 2: Create a new SSH Key

Type the following on your Terminal:

ssh-keygen

You will see the following output:

[email protected] .ssh % ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/neo/.ssh/id_rsa): my_key
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in my_key.
Your public key has been saved in my_key.pub.
The key fingerprint is:
SHA256:gqw/vi22+9VhJSJSFYS&JHSDSDdCtD+jm4CJTrXubt3+ts 
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|           . .   |
|   ...       . + |
|   ....  . .=    |
|    o.o..S+o.o   |
|   .  =.o o=     |
|  .   .= ...+ o .|
|   .+o....o..o o.|
|   o**+o...+. O.E|
+----[SHA256]-----+
[email protected] .ssh %

Above, I entered my_key as the name of my key, followed by a secret passphrase. The passphrase is optional, you can skip it by pressing enter.

Passphrase is important because if I copied your keys, then I could impersonate you and gain access. The passphrase only you will know.

This will generate a new key for you in the .ssh folder.

[email protected] .ssh % ls
config		id_rsa		id_rsa.pub	known_hosts	my_key		my_key.pub

Step 3: Add your key to the Pi

Run the following command to copy your key

ssh-copy-id <USERNAME>@<IP-ADDRESS>

This will copy the key over to the Pi. Now try to ssh into your Pi and you should login without entering password!

ssh <USERNAME>@<IP-ADDRESS>

Next I will talk about adding all your hosts in the SSH config that way you will not have to type [email protected] every single time. Instead it will be as simple as SSH Pi and it will connect to that Pi. Thanks for reading!