ssh-copy-id Command: How to Use It
In this article
The ssh-copy-id command installs your public SSH key onto a remote server's authorized_keys file, enabling passwordless SSH logins. You run it from your local machine with your username and the server's address, and it copies the key and sets the correct permissions automatically. This article explains how it works, how to use it safely, and what to do when it doesn't behave as expected.
What ssh-copy-id Does
SSH (Secure Shell) can authenticate in two main ways: with a password or with a public/private key pair. The key pair consists of a private key that stays on your machine and a public key that you place on any server you want to access. When you connect, the server checks that the public key matches your private key—if it does, you're logged in without a password.
The ssh-copy-id utility automates the tedious part of that setup. Instead of manually creating the .ssh directory, setting permissions, and appending your public key to authorized_keys, the command does all of it for you. It connects to the remote host using your normal SSH authentication (usually a password), then copies the specified public key(s) into the right place.
The command is included with OpenSSH on most Linux and macOS systems. If you're on Windows, you may need to use a tool like PuTTY's pscp or install OpenSSH for Windows, but the underlying concept is the same.
How to Use ssh-copy-id
The basic syntax is straightforward:
ssh-copy-id username@remote_host
Here's a typical first-time setup:
- Generate a key pair if you don't already have one:
ssh-keygen -t rsa -b 4096(ored25519for a more modern, faster algorithm). - Run
ssh-copy-id username@remote_host. - Enter your password when prompted.
- Test the login with
ssh username@remote_host—it should not ask for a password.
If your key has a non-standard name (e.g., ~/.ssh/mykey.pub), specify it with -i:
ssh-copy-id -i ~/.ssh/mykey.pub username@remote_host
You can also copy multiple keys at once by passing multiple -i options, or use -f to force the copy even if the key already exists (which is useful if you've changed the key).
What the Command Does Behind the Scenes
When you run ssh-copy-id, it performs several steps:
- It reads the public key file(s) you specified (or the default
~/.ssh/id_rsa.pub). - It connects to the remote host using SSH, prompting for your password (or using an SSH agent if you have one).
- It creates the
~/.sshdirectory on the remote host if it doesn't exist. - It sets the correct permissions:
700for the.sshdirectory and600forauthorized_keys. - It appends your public key to
~/.ssh/authorized_keys, unless it's already present.
This is why the command is so convenient—it avoids the common mistakes of wrong permissions or a missing directory, which are the usual reasons why manually placed keys don't work.
Choosing the Right Key Type
Modern SSH supports several key types. The most common are:
- RSA (with at least 2048 bits, ideally 4096)—widely supported, but slower to generate and use.
- Ed25519—faster, smaller, and considered more secure. It's supported by all modern OpenSSH versions (6.5+).
- ECDSA—based on elliptic curves, but some older servers may have issues with it.
For new setups, ed25519 is generally recommended. To generate one:
ssh-keygen -t ed25519
Then copy it with ssh-copy-id as usual. The command will automatically pick the correct public key file if you use the default name.
Common Problems and How to Fix Them
Even with a simple command, things can go wrong. Here are the most frequent issues:
- Permission denied (publickey)—This usually means the key isn't being offered or the server rejects it. Check that you're using the correct key (
-i) and that the remoteauthorized_keysfile has the right permissions (600). - Password prompt still appears—If you can log in but are still asked for a password, the key may not have been appended correctly, or the server's SSH config has
PasswordAuthenticationenabled andPubkeyAuthenticationdisabled. Check the server's/etc/ssh/sshd_config. - ssh-copy-id not found—On some minimal systems, the command isn't installed. You can install it via your package manager (e.g.,
apt install openssh-clienton Debian/Ubuntu), or manually copy the key with a one-liner. - Multiple keys on your machine—If you have several keys and the server doesn't accept the default one, use
-ito specify the correct public key.
A manual fallback if ssh-copy-id is unavailable:
cat ~/.ssh/id_rsa.pub | ssh user@host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
That does the same thing but requires you to type your password each time.
Security Considerations
Using ssh-copy-id is safe because it only adds your public key—it never transmits your private key. However, there are a few security points to keep in mind:
- Always protect your private key with a strong passphrase. If someone steals your private key file, they still need the passphrase to use it.
- Only copy keys to servers you trust. Once your key is on a server, anyone with root access to that server can potentially use it to impersonate you on other servers that share the same key.
- Consider using an SSH agent with a passphrase-protected key so you don't have to type the passphrase every time, but be aware that the agent holds your key in memory.
- Regularly review the
authorized_keysfiles on your servers and remove keys you no longer use.
Alternatives and Related Tools
While ssh-copy-id is the standard tool, there are alternatives:
- Manual copy—As shown above, a simple
catpipe works, but it's easy to get permissions wrong. - ssh-copy-id for Windows—Windows 10 and 11 include OpenSSH, so the command works in PowerShell or Command Prompt. For older systems, PuTTY's
pscpcan copy a file, but you'd have to manually append it. - Configuration management tools—Ansible, Puppet, or Chef can manage SSH keys across many servers, but they're overkill for a single machine.
The key takeaway is that ssh-copy-id is the simplest, most reliable way to set up key-based authentication, and it's available on virtually every Unix-like system.
Frequently Asked Questions
Does ssh-copy-id work with a non-default port?
Yes, you can specify a port with the -p option, like ssh-copy-id -p 2222 user@host. This is useful if your server listens on a non-standard port.
Can I use ssh-copy-id to copy a key to multiple servers at once?
No, the command only targets one host at a time. You can loop over hosts in a shell script, but each invocation will prompt for the password unless you use an SSH agent or key already installed.
What if I already have a key on the server but want to replace it?
Use the -f flag to force the copy. This will append the new key even if the old one is already in authorized_keys. To remove the old one, you'll need to edit the file manually on the server.
Does ssh-copy-id work with SSH keys that have a passphrase?
Yes, it copies the public key, which doesn't require the passphrase. However, when you use the key later, you'll be prompted for the passphrase unless you add it to an SSH agent.
Why is my key not working after copying?
The most common causes are: incorrect permissions on ~/.ssh (should be 700) or authorized_keys (should be 600), the key not being in the right file, or the server's SSH configuration not allowing public key authentication. Check the server's /var/log/auth.log for clues.
Is ssh-copy-id available on macOS?
Yes, it's included with macOS's built-in OpenSSH. You can use it directly in Terminal.