You have to add your SSH public key to the remote SSH server's authorized_keys file for you to be able to log in to the remote SSH server using the public key authentication method.

The file is normally located in the server's ~/.ssh/ directory and there are various ways that you can copy your public SSH key to the to the server. The easiest one is by using the ssh-copy-id command.

You can copy your SSH public key to the remote SSH server using ssh-copy-id command at the terminal.

Steps to copy SSH public key to server using ssh-copy-id:

  1. Locate your public SSH key.
     $ ls ~/.ssh/id*
    /home/user/.ssh/id_rsa           /home/user/.ssh/

    The public key is normally the one with the .pub extension.

  2. Copy your SSH public key using ssh-copy-id command.
    $ ssh-copy-id [email protected]
    The authenticity of host 'remote-host (' can't be established.
    ECDSA key fingerprint is SHA256:hXGpY0ALjXvDUDF1cDs2N8WRO9SuJZ/lfq+9q99BPV0.
    Are you sure you want to continue connecting (yes/no)? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 2 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    [email protected]'s password:
    Number of key(s) added: 2
    Now try logging into the machine, with:   "ssh '[email protected]'"
    and check to make sure that only the key(s) you wanted were added.

    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/ [email protected]
  3. Try to log in using key to test.
    $ ssh -i .ssh/id_rsa [email protected]
    Welcome to Ubuntu 19.04 (GNU/Linux 5.0.0-20-generic x86_64)
     * Documentation:
     * Management:
     * Support:
      System information as of Sat Jun 29 11:31:23 UTC 2019
      System load:  0.16               Processes:            211
      Usage of /:   25.8% of 19.56GB   Users logged in:      1
      Memory usage: 13%                IP address for ens33:
      Swap usage:   0%
     * MicroK8s 1.15 is out! It has already been installed on more
       than 14 different distros. Guess which ones?
    0 updates can be installed immediately.
    0 of these updates are security updates.
    Last login: Sat Jun 29 11:08:20 2019 from

Alternative methods:

  • Manually append your public key to the remote ssh server's key to authorized_keys file. For example, copy the content of your ~/.ssh/ to the server's ~/.ssh/authorized_keys file.
  • Using the following command combination
    $ cat ~/.ssh/ | ssh [email protected] 'cat >> ~/.ssh/authorized_keys 

Guide compatibility:

Operating System
Ubuntu Linux
Debian Linux
Red Hat Enterprise Linux
Fedora Linux
CentOS Linux
openSUSE Linux
SUSE Linux Enterprise Server
Share this guide!
Discuss the article:

Comment anonymously. Login not required.