SSH servers authorize public keys for individual accounts. ssh-copy-id installs a selected public key for a remote account while the private key stays on the client.

On an OpenSSH server, the public key is appended to the remote user's ~/.ssh/authorized_keys file. The ssh-copy-id helper logs in over SSH, skips keys already accepted by the server, creates the remote key file when needed, and adds the selected public key in the expected one-line format.

The first copy still needs a working way to authenticate to the remote account, usually the account password or an existing key. Installing the public key lets whoever holds the matching private key log in as that user, so the destination account must be one you intend to authorize.

Steps to copy an SSH public key to a server:

  1. Copy the public key to the intended remote account with ssh-copy-id.
    $ ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host.example.net
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/user/.ssh/id_ed25519.pub"
    /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: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    user@host.example.net's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with: "ssh -i /home/user/.ssh/id_ed25519 'user@host.example.net'"
    and check to make sure that only the key(s) you wanted were added.

    The destination user@host.example.net consists of the remote username and server address. On first contact, accept the server's host-key prompt only after comparing its fingerprint through a trusted channel. The first successful copy usually prompts for the remote account password unless another key or login method already works. Add -p 2222 to this command and the login test when the server listens on that port. If the matching .pub file is missing, recreate it from the private key before installing the key.
    Related: How to connect to an SSH server on a different port
    Related: How to extract a public key from an SSH private key
    Tool: SSH Public Key Extractor

  2. Verify that the server accepts the key by running a remote command.
    $ ssh -i ~/.ssh/id_ed25519 user@host.example.net whoami
    user

    The output names the remote account. A private-key passphrase prompt is local to the client. A remote account password prompt means the server has not accepted the copied key for that account.
    Related: How to fix SSH authorized_keys permissions
    Related: How to check SSH authorized keys in Linux

Share this guide