Copying SSH Public Keys

Hello! So, you’re tired of logging into your Raspberry Pi using SSH and having to put your password in every. Single. Time. I know, incredibly infuriating but luckily there is another way. Passwordless key authentication login is one name for it, but even though it goes by many names this principle is the same.

You generate a key on your local machine, that key is in two parts, a public and private one and you upload the public one to the remote server so the next time you try to login to that server you don’t need a password because it knows it’s you because you have the right key with you. If you want to know more, the Wikipedia article on SSH keys is pretty good. And I’ve also written a post on how to Generate SSH keys as well.

The Super Quick Way

If you’re able to then I would recommend using a program called ssh-copy-id, it can be installed on most platforms I think but not all. For example if you are on MacOS and have Homebrew installed (which you should), the command to install it would be:

Installing ssh-copy-id on MacOS

brew install ssh-copy-id

Installing ssh-copy-id on Debian & its Derivatives

sudo apt-get install ssh-copy-id

Using ssh-copy-id

ssh-copy-id pi@your_remote_ip

When ssh-copy-id has finished you can now login to your Pi without using your password!

The Slightly Longer Way

If you don’t have or want ssh-copy-id installed on your machine, then you can use the following command to copy your public SSH Key over to the remote machine in question. In this case it’s a Raspberry Pi.

Copying Public SSH Key to Server if SSH Folder Exists

If the ~/.ssh folder exists on the remote machine, and you have a file called id_rsa.pub in your local ~/.ssh folder, then you will need this command:

cat ~/.ssh/id_rsa.pub | ssh pi@192.168.0.84 'cat >> .ssh/authorized_keys && echo "Key copied"'

Copying Public SSH Key to Server If SSH Folder Doesn’t Exist

If the ~/.ssh folder DOES NOT exist on the remote machine, and you have a file called id_rsa.pub in your local ~/.ssh folder, then you will need this command:

cat ~/.ssh/id_rsa.pub | ssh pi@192.168.0.84 'umask 0077; mkdir -p .ssh; cat >> .ssh/authorized_keys && echo "Key copied"'

Entering Your Password For The Last Time

After you press enter you will be asked for your password for the last time, if everything went well you should see the words “Key copied” and be dropped back into a normal command prompt like so:

Key Copied

Let’s Test Everything

So, if you got the “Key copied” message then your public key should now be happily residing on your Raspberry Pi or other remote machine and all we need do to test everything is working is simply login as we normally would. If everything has worked, you WILL NOT BE ASKED for your password:

testing everything

Success

You are now successfully logged in to your Raspberry Pi, and you didn’t need to use your password. Top banana! :metal:

Success