Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Command

ssh-copy-id user@your_server_ip

Run it on your own computer. It logs in with the account password, appends your public key to ~/.ssh/authorized_keys on the server, creates ~/.ssh if it is missing, and skips keys that are already there. You need a key pair first; see how to generate SSH keys.

Choose the Key and the Port

Without options, ssh-copy-id picks your default keys. Name one with -i, and add -p when SSH listens on another port:

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 user@your_server_ip

Point -i at the .pub file. Only the public key is sent; the private key stays on your computer. To see which keys would be installed without changing anything, add -n for a dry run.

Test the Key Login

Open a new terminal and connect:

ssh -i ~/.ssh/id_ed25519 user@your_server_ip

If you set a passphrase, ssh asks for that instead of the account password. That is the sign the key works. If it still asks for the account password, or fails, see Permission denied (publickey).

Add the Key by Hand

Where ssh-copy-id is not available, pipe the key through ssh. This creates the folder and the file with the right permissions:

cat ~/.ssh/id_ed25519.pub | ssh user@your_server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Use >>, which appends. A single > would replace every key already in the file.

Add the Key from the Server Console

If password login is already off and you have no working key, open the console in your provider's control panel, log in as the user, and paste the public key line into the file:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

If you created the file as root for another user, give it back to that user, or sshd will ignore it:

sudo chown -R user:user /home/user/.ssh

The Permissions sshd Expects

PathModeOwner
~ (home)not writable by othersthe user
~/.ssh700the user
authorized_keys600the user

Several Keys on One Account

Each line of authorized_keys is one key. Add a key for each computer you work from instead of copying one private key around. The comment at the end of each line tells you whose key it is; to revoke access, delete that line.

Keys Added in the Control Panel

Many providers let you store a public key in the control panel and place it on the server when it is created or reinstalled. Check which account receives it, often root or the image's default user. A key added in the panel later may not reach a server that is already running; in that case add it with ssh-copy-id.

Common Messages

  • No identities found: ssh-copy-id found no public key. Create one with ssh-keygen, or point -i at the right .pub file.
  • All keys were skipped: the key is already in authorized_keys. Nothing needs to be done; test the login.
  • Permission denied: the password was wrong, or the server no longer accepts password logins. Use the console method above, or a key that already works.
  • Connection refused or timed out: check the port with -p and the address before anything else.

After the Key Works

With key login confirmed for a user with sudo, you can turn off password logins: see how to disable SSH password authentication.

Was this answer helpful?
Loading...