Copy SSH public key to server

You have to copy your SSH public key to the server's SSH authorized_keys file in order to passwordlessly log in to the server. You can copy your SSH key to the server via any of these methods:

Copy SSH key using ssh-copy-id

  1. Use SSH's bundled command, ssh-copy-id
    $ ssh-copy-id user@host

    If you want to use other public key rather that then one in the default location, use the -i option as the following

    $ ssh-copy-id -i ~/.ssh/other_key.pub username@host

Manually add SSH key to authorized_keys file

  1. Manually append the content of your public key file (normally ~/.ssh/id_rsa.pub to the server's ~/.ssh/authorized_keys

Remotely add SSH key to authorized_keys via Bash script

  1. Using the following simple Bash script
    $ cat ~/.ssh/id_rsa.pub | ssh user@host 'cat >> ~/.ssh/authorized_keys 

Sign up for Complete Secure Shell Course and many other great courses on Udemy to learn more.


Written by Mohd Shakir Zakaria. Last updated on 2019-02-12