Generating SSH Keys - Transferring your key
Written by Sam Moffatt   
Tuesday, 16 October 2007 05:15
Article Index
Generating SSH Keys
Generating a key
Transferring your key
Using an SSH Agent
Keychain and SSH
Wrapping Up
All Pages

Transferring your key

Before you can get started using SSH Key's you need to distribute your SSH public key. The easiest way to do this is to secure copy the file to the remote server as the "authorized_keys" file in the ".ssh" directory on that server. SCP (like cp) cannot create directories if they don't exist if you're only copying a single file, however your can recursively copy a directory which will create it remotely for you automatically. Since typically we will be transferring our key to new servers we can create a template ".ssh" directory with an authorized keys file. Please note this will only work if you don't already have a .ssh directory on the server already, otherwise it will create a new template directory within your .ssh directory.

In our .ssh directory again:

moffats@shadow:~/.ssh> mkdir template
moffats@shadow:~/.ssh> cd template/
moffats@shadow:~/.ssh/template> cp ../ authorized_keys
moffats@shadow:~/.ssh/template> cd ..
moffats@shadow:~/.ssh> scp -r template/ dev1:.ssh
authorized_keys 100% 224 0.2KB/s 00:00

If you have more than one public key you will have to append it, something like this will work:

scp /home/moffats/.ssh/ remoteserver:my_key
ssh remoteserver mkdir .ssh (if the folder doesn't already exist)
cat mykey >> .ssh/authorized_keys (Note: >> is the append output redirector, it will create a new file or append to an existing one).

Now we have our SSH key on a remote server, we can use the key when we SSH into it:

Enter passphrase for key '/home/moffats/.ssh/id_rsa':

And we can use this password to log into those servers to validate the key works. If you hit enter at the passphrase prompt (without entering anything else in) you are prompted for the password as per normal.

Last Updated on Wednesday, 09 April 2008 06:57