Tuesday, July 23, 2013

Copying public ssh key to the remote server

After we create ssh keys pair with 'ssh-keygen' command, the next step is to copy the public key to remote server and append in ~/.ssh/authorized_keys file.

Traditional method
$ scp ~/.ssh/id_dsa.pub username@my.server.org:

Login to remote server
$ mkdir .ssh --mode 700
$ cat ~/id_dsa.pub >> ~/.ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

Fast and simple Method using ssh-copy-id

ssh-copy-id is a script that uses ssh to log into a remote machine and append the indicated identity file to that machine's ~/.ssh/authorized_keys file creating the file, and directory, if necessary.

$ssh-copy-id username@my-server.org

If your public key filename is anything other than id_rsa.pub you will get error stating "/usr/bin/ssh-copy-id: ERROR: No identities found". In this case you must provide the location and filename of  the public key

$ssh-copy-id -i ~/.ssh/id_dsa.pub username@my-server.org

If remote server is listening on non-defaut port for ssh then,
$ssh-copy-id -i ~/.ssh/id_dsa.pub -p 5555 username@my-server.org

No comments:

Post a Comment