Raspberry Pi: SSH using public and private keys

Sometimes it’s necessary to leave a publicly accessible SSH connection available on your Raspberry Pi. While a strong password is essential, a much more secure method for authentication is to use a public and private key system. This guide will take you through all of the steps to creating the keys, configuring the Raspberry Pi, and finally configure PuTTY.

Before we begin, this assumes you already have SSH access using a password.

Generating the keys

The keys will be generated on your PC, not on the Raspberry Pi.

Run PUTTYGEN (PuTTY Key Generator).

The default parameters are fine (SSH-2 RSA, 2048).

Click the Generate button and move your mouse cursor around as instructed.

Copy / paste the public key into Notepad, we’ll need this in a moment.

Save the private key to a ppk file on your PC. You can disregard the passphrase pop-up.

Configuring public key for a user

Connect to the device over SSH and login using your (non-root) username and password. For this example, we’ll use the username “darian”. 😉

Enter the following:

cd ~
mkdir .ssh
cd .ssh
nano authorized_keys

Paste the public key from Notepad into Nano. This key should be a single line in the file.