Creating ssh keypairs PDF Print E-mail
Thursday, 09 April 2009 15:13

I'm lazy when it comes to putting in my password and don't want to have to do it every time I do a cvs checkin so I create ssh keypairs.  I always forget how I created them so this time I'm writing it down.

On the Destination machine (in this case called detritus) use ssh-keygen to create both the public and the private key.  Then rename the private key to id_rsa:

    cianer@detritus:~> mkdir .ssh
    cianer@detritus:~> cd .ssh
    cianer@detritus:~/.ssh> ssh-keygen -t rsa -f detritus.rsa
    Generating public/private rsa key pair.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in detritus.rsa.
    Your public key has been saved in detritus.rsa.pub.
    The key fingerprint is:
    d5:54:a0:a8:be:3e:d0:1b:ad:dd:b5:68:85:09:4c:3e cianer@detritus
    cianer@detritus:~/.ssh> ls -la
    total 8
    drwxr-xr-x    2 cianer   users         112 2009-04-09 16:10 .
    drwxr-xr-x    3 cianer   users         104 2009-04-09 16:10 ..
    -rw-------    1 cianer   users         887 2009-04-09 16:10 detritus.rsa
    -rw-r--r--    1 cianer   users         223 2009-04-09 16:10 detritus.rsa.pub
    cianer@detritus:~/.ssh> mv detritus.rsa id_rsa


I want to be able to ssh into detritus from two machines - anghammarad and vetinari - so I firstly repeat the step above to generate a public/private keypair for each machine.  Then simply add the public key from each (i.e. anghammarad.rsa.pub and vetinari.rsa.pub) to ~/.ssh/authorized_keys on detritus:

    cianer@detritus:~/.ssh$ scp vetinari:.ssh/vetinari.rsa.pub .
    cianer@vetinari's password:
    vetinari.rsa.pub                              100%  397     0.4KB/s   00:00    
    cianer@detritus:~/.ssh$ scp anghammarad:.ssh/anghammarad.rsa.pub .
    cianer@anghammarad's password:
    anghammarad.rsa.pub                           100%  400     0.4KB/s   00:00    
    cianer@Detritus:~/.ssh$ cat vetinari.rsa.pub >> authorized_keys
    cianer@Detritus:~/.ssh$ cat anghammarad.rsa.pub >> authorized_keys
    cianer@Detritus:~/.ssh$

Now test it works...

    cianer@anghammarad:~$ ssh detritus

    Last login: Tue Mar 23 00:37:49 2010 from anghammarad
    cianer@detritus:~$
    cianer@vetinari:~$ ssh detritus

    Last login: Tue Mar 23 01:09:19 2010 from anghammarad
    cianer@detritus:~$


Top tip:  Add detritus's own public key to authorized_keys as well, so that the public key of *all* your machines is in the one file.  Then copy this version of authorized_keys to all machines and you can ssh to every machine from every machine.

Note: If you use a passphrase you will have to enter it once per session.

 
Joomla Templates by Joomlashack