Saturday 25 January 2014

Using public/private keys to log in to Raspbian

This assumes working from a *nix environment, and that you haven’t previously created a public/private key pair (e.g. for using Github). If you are unsure type cat ~/.ssh/id_rsa into the terminal - if you get a load of characters ending in "-----END RSA PRIVATE KEY-----" you already have a key that you can use - go straight to Step 2.

Step 1: Generating a public/private key pair
This is as easy as:

ssh-keygen -t rsa -C "name@email.com

(Obviously replace name@email.com with your own email address).

The system may ask you whether you want to create a passphrase for this key, if you do you will be asked to enter this passphrase the first time you use the key in any given session. If you do not want to set a passphrase just hit enter.

Step 2: Getting your key to the Raspberry Pi
This is again a simple one line command that logs into Raspbian using a password and adds your key to the list of authorised keys on the system.


cat ~/.ssh/id_rsa.pub | ssh pi@10.0.2.2 "mkdir .ssh; touch .ssh/authorized_keys; cat >> .ssh/authorized_keys"

You will need to change 10.0.2.2 to the IP address used by your Raspberry Pi. You will be prompted for the password of the 'pi' user on your Raspberry Pi. If you then use the exit command to leave the pi and return to your *nix system you hsould now be able to log on simply by typing:

ssh pi@10.0.2.2

If you used a passphrase to protect your keys you will be prompted to enter this the first time you use the key in any session.

ShareThis

Copyright