Network security

ssh-copy-id: Copy Your SSH Key to a Server in One Command

ssh-copy-id is the one-command way to set up passwordless SSH login: it copies your public key to the server and sets the right permissions for you.

Cysoro Security TeamPublished · 7 min read
$ ssh-copy-iddeploy@server✓ Key installedauthorized_keys updated$ ssh deploy@serverWelcome — no password NETWORK SECURITY ssh-copy-id One command to passwordless login.
Quick answer

ssh-copy-id is an OpenSSH utility that copies your local SSH public key to a remote server's ~/.ssh/authorized_keys file, so you can log in with your key instead of a password. Run ssh-copy-id user@host, enter the remote password once, and test with ssh user@host. Use -i to pick a key, -p for a non-standard port, and -n to preview what would be installed.

Key takeaways

  • ssh-copy-id copies only your public key; your private key never leaves your computer.
  • Use -i to copy one specific key, because without it ssh-copy-id may copy every key your agent holds.
  • After copying, disable password login only once key login is tested and working.

Typing a password every time you log into a server is slow, and passwords can be guessed. SSH keys fix both problems — and ssh-copy-id is the one command that installs your key on the server for you.

What is ssh-copy-id?

ssh-copy-id is a utility that copies your local SSH public key to a remote server's authorized_keys file. It ships with the OpenSSH client, so it is already present on most Linux systems and on macOS at /usr/bin/ssh-copy-id.

The command connects to the remote host over SSH, using the same settings as a normal login, including any matching entry in your ~/.ssh/config. It appends the key to ~/.ssh/authorized_keys on the server, creating the .ssh directory and the file if they do not exist, with restrictive permissions.

Its purpose, as SSH.com puts it, is to make setting up public key authentication easier. Once the key is installed, the server accepts it for login and you no longer need the password.

How does ssh-copy-id work?

When you run ssh-copy-id, it first assembles a list of keys to install, then logs into the server with each one to see whether it is already authorized. Keys that fail that test — meaning they are not yet installed — are appended to authorized_keys.

Without -i, it uses the keys listed by ssh-add -L if your agent has any. If the agent has no keys, it uses the most recently modified file matching ~/.ssh/id*.pub, excluding certificate files. That default is convenient, and it is also the main trap: it can copy more keys than you intended.

The copy itself asks for the remote user's password, or another authentication method the server accepts. Only the public key travels to the server; the private key never leaves your computer.

What ssh-copy-id does, step by step1. Your computerpublic key in~/.ssh/id_ed25519.pub2. SSH loginpassword once, toprove it is you3. Serverkey appended to~/.ssh/authorized_keysNext login: key only, no passwordprivate key never leaves your computer
ssh-copy-id installs your public key on the server; the private key stays local. Illustration by Cysoro.

What do you need before using ssh-copy-id?

You need an SSH key pair and a server account you can log into with a password, or with an existing key. If you have no key pair yet, generate one first.

ssh-keygen -t ed25519 -C "[email protected]"

Accept the default location, ~/.ssh/id_ed25519, and set a passphrase. The passphrase protects the private key if your laptop is stolen; your agent can remember it so you do not type it on every login.

Check that the server allows public key authentication. In /etc/ssh/sshd_config on the server, PubkeyAuthentication should be yes. If password authentication is already disabled and you have no working key, you will need console or provider access to add one another way.

How do you use ssh-copy-id?

The basic syntax is ssh-copy-id [user@]hostname. Replace user with the remote username and host with the server address or IP. You will be prompted for the remote user's password.

ssh-copy-id [email protected]
ssh-copy-id [email protected]

After the key is copied, test the login in a new terminal window, keeping the old one open in case something is wrong:

ssh [email protected]

If the login completes without asking for a password, the key works. To confirm which key was used, add -v for verbose output, or force key-only login with ssh -o PreferredAuthentications=publickey [email protected].

What are the ssh-copy-id options?

Most use comes down to four options: -i for the key, -p for the port, -n for a dry run, and -f for forced mode. The full syntax from the ssh-copy-id manual page is ssh-copy-id [-f] [-n] [-i identity_file] [-p port] [-o ssh_option] [user@]hostname.

OptionWhat it doesExample
-i identity_fileCopy only the key in this filessh-copy-id -i ~/.ssh/id_ed25519.pub user@host
-p portConnect on a non-standard SSH portssh-copy-id -p 2222 user@host
-o ssh_optionPass an option to the SSH clientssh-copy-id -o Port=2222 user@host
-nDry run: print keys that would be installedssh-copy-id -n user@host
-fForced mode: skip the already-installed checkssh-copy-id -f user@host
-hPrint a usage summaryssh-copy-id -h

Use -i whenever you have more than one key. SSH.com warns that without it, the tool can easily install multiple keys or unintended keys, because it copies everything your agent lists. A dry run with -n shows exactly what would be installed before anything changes.

How do you copy a specific key?

Use -i with the public key file. If the filename does not end in .pub, ssh-copy-id adds it, so -i ~/.ssh/id_ed25519 copies id_ed25519.pub, not the private key.

ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
ssh-copy-id -i ~/.ssh/work_key.pub [email protected]

Combine -i with -p when the server uses a non-standard port:

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 [email protected]

If you created a new key and it is not the one ssh-copy-id picks by default, either use -i or touch the preferred .pub file to make it the most recently modified, as the manual page suggests.

How do you copy a key to multiple servers?

For a handful of servers, a loop is enough. Keep the list in a file or an array, and run ssh-copy-id once per host.

for host in web1.example.com web2.example.com db1.example.com; do
  ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@$host
done

For fleets, use configuration management — Ansible, Puppet, or your cloud provider's key management — instead of a shell loop. Those tools record which keys are authorized where, and they remove a key everywhere when someone leaves, which a loop cannot do.

Keep an inventory of authorized keys. A key copied to a server years ago by someone who has left is still a valid login, and ssh-copy-id has no built-in way to revoke it.

What if ssh-copy-id is not available?

Some minimal systems and Windows-only clients do not have ssh-copy-id. The manual method does the same thing in one command, and it is worth knowing because it shows exactly what the tool does.

cat ~/.ssh/id_ed25519.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Then set the permissions by hand: 700 on ~/.ssh and 600 on ~/.ssh/authorized_keys. Wrong permissions are the most common reason a manually copied key is ignored, which is exactly the error ssh-copy-id avoids.

On Windows, OpenSSH is available through Windows Terminal or PowerShell in current versions, and ssh-copy-id works there once OpenSSH is installed. Otherwise, paste the public key into the server's authorized_keys file through your provider's console.

Permission denied? Check these five things1. Wrong key — test with ssh -i and -v to see which key was tried2. ~/.ssh is not 700 or authorized_keys is not 6003. Files owned by the wrong user on the server4. PubkeyAuthentication no in /etc/ssh/sshd_config5. Custom AuthorizedKeysFile path — check sshd_config
Five checks for the most common ssh-copy-id failure. Illustration by Cysoro.

How do you fix ssh-copy-id errors?

A Permission denied (publickey) error means the server rejected the key that was tried. Run ssh -v user@host and look for the line naming the key file offered; if it is not the key you copied, use -i on both commands.

Check ownership and permissions on the server. The .ssh directory must be 700, authorized_keys must be 600, and both must be owned by the user logging in. SSH refuses keys when these are too open, and it does so silently.

Check the server configuration. If PubkeyAuthentication is no in /etc/ssh/sshd_config, or the server uses a custom AuthorizedKeysFile path, the key can be in the file and still be ignored. After changing sshd_config, restart the SSH service.

If ssh-copy-id itself reports command not found, install the OpenSSH client package: on Ubuntu and Debian it is openssh-client, and on Fedora and RHEL it is openssh-clients.

Is ssh-copy-id safe? What should you watch out for?

ssh-copy-id is safe when used deliberately, and risky when used carelessly. The main risk is copying keys you did not mean to authorize. SSH.com warns that the tool can easily install multiple keys or unintended keys, and that it does not add command restrictions to keys.

Use one key per purpose — one for work, one for personal servers — and copy only the key you mean to authorize. Set a passphrase on the private key, and use your agent so you type it once per session rather than on every login.

Key hygiene is one item on a wider list. Our small business cybersecurity checklist covers it alongside updates, backups, and multi-factor authentication.

Once key login is tested and working, consider disabling password authentication on the server by setting PasswordAuthentication no in sshd_config. Do this only after testing, and only while you still have a working session open, because a mistake here can lock you out. For the wider hardening picture, see our guide to securing your Wi-Fi and home network.

The bottom line

ssh-copy-id turns a fiddly manual task into one command: copy the public key, set the permissions, and test the login. Use -i to pick the key, -p for a non-standard port, and -n to preview.

Test key login before you disable passwords, keep an inventory of authorized keys, and remove keys when people or devices leave. You can confirm your server is reachable and check its address with our free what-is-my-IP tool before you start.

Frequently asked questions

What does ssh-copy-id do?

It logs into a remote server over SSH and appends your public key to the remote user's ~/.ssh/authorized_keys file, creating the .ssh directory and the file if needed. After that, the server accepts your matching private key for login.

Does ssh-copy-id copy my private key?

No. Only the public key is copied to the server. Your private key stays on your local computer and should never be copied to another machine.

What if I do not have an SSH key yet?

Generate one first with ssh-keygen, for example ssh-keygen -t ed25519, and accept the default location. Then run ssh-copy-id to copy the new public key to the server.

How do I copy a specific key with ssh-copy-id?

Use the -i option with the public key file, for example ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. If the filename does not end in .pub, ssh-copy-id adds it.

How do I use ssh-copy-id on a non-standard port?

Use the -p option, for example ssh-copy-id -p 2222 user@host, or pass -o Port=2222. The port option is passed through to the SSH client.

Why do I get Permission denied (publickey) after ssh-copy-id?

Common causes are the wrong key, wrong ownership or permissions on the server's .ssh directory or authorized_keys file, a custom AuthorizedKeysFile path, or public-key authentication being disabled in the server's sshd_config.

Is ssh-copy-id available on macOS and Windows?

macOS includes ssh-copy-id at /usr/bin/ssh-copy-id, and it ships with the OpenSSH client on Linux. On Windows, use OpenSSH through Windows Terminal or PowerShell, or copy the key manually.

Can ssh-copy-id install the same key twice?

Yes, if you use -f, forced mode, which skips the check for keys already installed. Without -f, ssh-copy-id tests each key first and skips any that are already authorized.

Sources

  1. OpenSSH — ssh-copy-id manual page (Debian)
  2. SSH.com — What is ssh-copy-id? How ssh-copy-id works?
  3. Linuxize — ssh-copy-id Command: Copy SSH Keys to a Remote Server
  4. OpenSSH — ssh-keygen manual and key types
Written & fact-checked byCysoro Security Team

The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.

  • Email authentication
  • Phishing & BEC
  • Domain & DNS security
  • Small-business security

Checked against the ssh-copy-id manual page, SSH.com's academy guide, and Linuxize's command guide.

How we write and fact-check →

Free tools for this topic

No account needed.

See where your domain stands

Free for up to 3 domains. No card, no installs.

Start free