Thursday, 2 May 2013

Login using a public key for ssh (without password) from GNU/Linux

Login using ssh, a public key and a password related to that file is a more secure way to do it than just using a password. I'll show you a not so secure way to login to you pi but more secure than a password. We'll use a passwordless private/public key.

I'll refer to your computer to refer to the computer where you'll connect from and to pi to the little rapsberry pi. Let's go.

We must create a private/public key pair if we don't already have one. On green what you must enter:

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/donosor00/.ssh/id_rsa): [press ENTER]
Created directory '/home/donosor00/.ssh'.
Enter passphrase (empty for no passphrase): [press ENTER*]
Enter same passphrase again: [press ENTER*]
Your identification has been saved in /home/donosor00/.ssh/id_rsa.
Your public key has been saved in /home/donosor00/.ssh/id_rsa.pub.
The key fingerprint is:
f8:5f:d7:b2:bb:a9:25:5f:51:0f:e8:64:c3:8e:ad:3e donosor00@weee
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . . |
| * . .|
| . B . .o|
| . S . + ..|
| . . ..|
| . . o + o|
| oE. = = |
| o...*o |
+-----------------+

------------------------------------------------------------------------------------------------------
Where is [press ENTER*] you shoud enter a password if you want that additional protection but we are here for easeness.

[font='courier new', ', courier, monospace} ']Now we have to copy our public key to the pi. There are two ways :[/font]
  • [font='courier new', ', courier, monospace} ']the easy one, just typing: [/font][font=courier new,courier,monospace]ssh-copy-id pi@192.168.1.254[/font] <-- here the IP of your pi
  • [font="courier new, , courier, monospace}"]the old one. ssh-copy-id doesn't exist and we must copy it the hard way:[/font]
host $ scp .ssh/id_rsa.pub pi@[ip of the raspberry]:
Password:
host $ ssh pi@[ip of the raspberry]
Password:

pi $ if [ ! -d .ssh ] ; then
>  mkdir .ssh
>  chmod 700 .ssh
>fi
pi $ cat id_rsa.pub >> .ssh/authorized_keys
pi $ rm id_rsa.pub

No comments:

Post a Comment

Amazon

Donate

Donate Towards More Raspberry PI's for Projects