CodeBrowser

We earn commissions when you shop through the links below. Learn more

How to Use ssh-copy-id to Copy Your SSH Key to a Server

ssh-copy-id adds your public key to a server in one command, so you can log in without a password. Here’s how to use it, its most useful options, what to do on Windows where it isn’t included, and how to fix common errors.

Mac & LinuxWindows alternativeCustom ports & keysTroubleshooting

The basics

Copy your key in one command

ssh-copy-id user@203.0.113.10

Enter the account’s password one last time. ssh-copy-id logs in, adds your public key to ~/.ssh/authorized_keys on the server, and sets the right permissions. Then test it with ssh user@203.0.113.10; you won’t be asked for a password.

No SSH key yet?

ssh-copy-id copies an existing key, so create one first with ssh-keygen -t ed25519. Our guide on how to generate an SSH key walks through it on every operating system.

Options

Useful ssh-copy-id options

Copy a specific key (-i)

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server

Use a custom SSH port (-p)

ssh-copy-id -p 2222 user@server

See what would be copied (-n, dry run)

ssh-copy-id -n user@server

Copy even if it looks installed (-f)

ssh-copy-id -f -i ~/.ssh/id_ed25519.pub user@server

Without -i, ssh-copy-id copies the keys loaded in your ssh-agent, or your newest key in ~/.ssh. Using -i is the safest way to know exactly which key goes to the server.

Windows

ssh-copy-id on Windows

Windows includes the OpenSSH client but not ssh-copy-id. This PowerShell one-liner does the same job: it creates the .ssh folder on the server if needed, adds your key and sets the permissions.

Prefer a graphical app? PuTTY users can paste the key into authorized_keys by hand, as shown below.

PowerShell equivalent of ssh-copy-id

type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Manual method

Add your key without ssh-copy-id

Copy your public key

Show it with cat ~/.ssh/id_ed25519.pub and copy the whole line.

Log in to the server

Connect with your password, or use your host’s web console.

Add the key

Run mkdir -p ~/.ssh && nano ~/.ssh/authorized_keys, paste the key on its own line and save.

Fix permissions

Run chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys.

Setting up a brand-new server? Most hosts, including DigitalOcean and Hostinger, let you paste your public key when you create the server, so you can skip this step entirely.

Troubleshooting

ssh-copy-id errors and fixes

MessageFix
ERROR: No identities foundYou don’t have a key yet, or it isn’t in the default place. Create one with ssh-keygen -t ed25519, or point to it with -i.
ssh-copy-id: command not foundOn Windows, use the PowerShell command above. On a Mac without it, install it with brew install ssh-copy-id.
Permission denied (publickey)The server already refuses passwords, so ssh-copy-id can’t log in. Add the key through your host’s web console or dashboard instead.
All keys were skipped because they already existThe key is already on the server. If login still fails, check the username and the file permissions on the server.
Still asked for a password afterwardsMake sure you connect as the same user you copied the key to, and run ssh -v to see whether the key is offered.

Next step

Now turn off password logins

Once key login works, disable passwords on the server so bots can’t guess their way in. Test from a second terminal before closing your current session. Our SSH hardening guide has the exact settings.

Disable passwords

echo "PasswordAuthentication no" | sudo tee /etc/ssh/sshd_config.d/00-hardening.conf
sudo sshd -t && sudo systemctl restart ssh

Need a server?

Need a server to copy your key to?

Most hosts below let you paste your public key when you create the server, so you may not even need ssh-copy-id.

Best overall

DigitalOcean

Simple cloud servers with great docs. Add your SSH key when you create a Droplet.

From about $4/month

View DigitalOcean deals
Most RAM for the money

Hostinger

4 GB of RAM and NVMe storage on the entry VPS plan, with an easy control panel.

About $5.19/month on a 24-month term with our extra 20% off

Get 20% off Hostinger VPS
Best budget

RackNerd

Yearly KVM VPS deals for about the price of one month elsewhere. Great for learning.

From about $11/year on promo

View RackNerd deals

Prices are approximate. Compare all VPS hosts, including Vultr and ShockHosting.

FAQ

ssh-copy-id questions, answered

What does ssh-copy-id actually do?

It logs in to the server with your password, appends your public key to ~/.ssh/authorized_keys for that user, and makes sure the .ssh folder and file have safe permissions. It never copies your private key.

Does ssh-copy-id copy my private key?

No. It only copies the public key, the .pub file. Your private key never leaves your computer.

Can I run ssh-copy-id more than once?

Yes. It checks whether the key is already installed and skips it, so running it again is harmless. Use -f to force it anyway.

How do I copy a key for a different user?

Put that username in the command, for example ssh-copy-id deploy@server. The key is added to that user’s authorized_keys file, and you will need that user’s password once.

How do I remove a key that ssh-copy-id added?

Log in to the server, open ~/.ssh/authorized_keys in a text editor and delete the line that ends with your key’s comment, then save the file.

Does ssh-copy-id work with a custom port?

Yes. Add -p and the port number, for example ssh-copy-id -p 2222 user@server, or set the port for that host in your SSH config file.

Need a server to copy your key to?

Set up a new Linux VPS the right way, with your SSH key added from the start, in about 30 minutes.

How to set up a VPS