Use SSH to logging into Raspberry Pi without Password


Scenario

We can login into remote Raspberry Pi using ssh command:

ssh -p PORT USER@IP
password: RASPBERRY_PASSWORD

As it can be seen, we require to input the Raspberry Pi’s password each time to establish SSH connection.

Solution: Add local machine’s SSH key to Raspberry Pi for password less SSH connection

To resolve the scenario, we need to add our local machine’s SSH key to the Raspberry Pi.

  • Check if you have SSH key in local machine using:

    ls -al ~/.ssh
    
  • If you don’t have any SSH key, generate one:

    ssh-keygen -t rsa -b 2048
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/username/.ssh/id_rsa):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /home/username/.ssh/id_rsa.
    Your public key has been saved in /home/username/.ssh/id_rsa.pub.
    
  • Copy the keys to the Raspberry Pi server:

    ssh-copy-id -p PORT USER@RASPBERRY_IP_ADDRESS
    

    Following prompt will be opened and you need to give the Respberry Pi’s password:

    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s),
    to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed
    -- if you are prompted now it is to install the new keys
    USER@RASPBERRY_IP_ADDRESS's password:
    Number of key(s) added: 1
    
  • Try logging into the Raspberry Pi without the password:

    ssh -p PORT USER@IP
    

    It will take few seconds to logging into the Raspberry Pi and you will get access into Raspberry Pi’s terminal.

    Linux raspberrypi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    

alt logging into raspberry pi

  • You can now try different commands to check if you are able to execute commands. The following command will show human readable memory size of available disk space of the Raspberry Pi :

    df -h
    

    alt run command in raspberry pi

Tested Environment

  • Operating System : Ubuntu 18.04 LTS (64-bit)

The above solution should work with all Linux based operating systems and Mac OS.

Reference