Stratos Gerakakis

Passwordless ssh connection with another server

Tagged as: linux  cli  ssh 

Mar 21 2008

First, create the key on the local machine (without a passphrase) ::

user@machine:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/
The key fingerprint is:

Then create a directory on the remote machine::

user@machine:~> ssh otherUser@remote "mkdir -p .ssh && chmod 700 .ssh"
otherUser@remote's password:

Finally append new key to remote .ssh/authorized_keys file::

user@machine:~> cat .ssh/ | ssh otherUser@remote 'tee -a .ssh/authorized_keys'
otherUser@remote's password:

Note: In place of the last command you could have also used::

user@machine:~> cat .ssh/ | ssh otherUser@remote 'cat >> .ssh/authorized_keys'
otherUser@remote's password:

but in the case your public key file does not contain a newline character at the end of the file (which probably it does not) the final concatenation on the authorized_keys will end up with long text line without newlines between the keys, thus making the file invalid. Tee on the other hand prepends the concatenation with a newline, keeping authorized keys happy.