ssh-copy-id puts your public SSH key on a server so you can log in without a password. It signs in once (with your password, usually), adds the key to ~/.ssh/authorized_keys for that user, and sets the permissions OpenSSH expects.
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
It ships with OpenSSH on Linux and macOS. On Windows, and on an iPhone or iPad, there isn't one; the sections below cover what to do there.
If you don't have a key yet, make one. Ed25519 is the modern default:
ssh-keygen -t ed25519 -C "laptop"
That writes a private key to ~/.ssh/id_ed25519 and the public key to ~/.ssh/id_ed25519.pub. The comment after -C ends up at the end of the line on the server, which makes it easy to find and remove later. Then copy the key:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
Enter the password when asked. Then test that the key works on its own, with password logins ruled out for this one attempt:
ssh -o PasswordAuthentication=no user@host
| Example | What it does |
|---|---|
ssh-copy-id user@host | Copy the default key (see below) |
ssh-copy-id -i ~/.ssh/work.pub user@host | Copy a specific public key |
ssh-copy-id -p 2222 user@host | Server listens on a non-standard port |
ssh-copy-id -n user@host | Dry run: show which keys would be copied, copy nothing |
ssh-copy-id -f -i key.pub user@host | Force: skip the check for keys already on the server. Also how to send a .pub file whose private key isn't on this machine |
ssh-copy-id myserver | Uses the host, user and port from a Host myserver block in ~/.ssh/config |
With -i, the key you name. Without it, OpenSSH's version of the script copies the keys loaded in your SSH agent (ssh-add -L) if there are any, and otherwise the most recently changed ~/.ssh/id*.pub. Either way it first tries logging in with each key and skips any the server already accepts, so running it twice doesn't add duplicate lines.
That agent behaviour catches people out: with several keys in the agent, all of them get copied. Pass -i to be specific.
All ssh-copy-id really does is append one line to a file. This does the same from any machine with an ssh command:
cat ~/.ssh/id_ed25519.pub | ssh user@host 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
Or, if you already have a shell on the server, paste the public key line in directly:
echo 'ssh-ed25519 AAAA...your-key... laptop' >> ~/.ssh/authorized_keys
Use >>, not >. A single > replaces the file and removes every other key in it. Each key is one line; a key pasted with a line break in the middle won't work.
Windows 10 and 11 include the OpenSSH client (ssh, ssh-keygen) but not ssh-copy-id, which is a shell script. In PowerShell, make a key if needed with ssh-keygen -t ed25519, then pipe it to a Linux or macOS server:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
If the server is itself Windows running OpenSSH Server, the file is different: C:\Users\name\.ssh\authorized_keys for standard users, and C:\ProgramData\ssh\administrators_authorized_keys for accounts in the Administrators group.
OpenSSH ignores authorized_keys if it could have been tampered with by another user. Fix the permissions on the server, as the user you log in as:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod go-w ~
The last line is the easy one to miss: a home directory that's group-writable is enough for the server to refuse the key. If you edited the files as root, also check ownership: sudo chown -R user:user /home/user/.ssh. On SELinux systems such as Fedora and RHEL, restore the file labels with restorecon -R -v ~/.ssh.
Still failing? These narrow it down:
ssh -v user@host shows which keys the client offers and whether the server accepted any.sudo journalctl -u ssh on Debian and Ubuntu, sudo journalctl -u sshd on Fedora and RHEL./etc/ssh/sshd_config, PubkeyAuthentication must not be no, and AuthorizedKeysFile says which file the server reads (the default is .ssh/authorized_keys).iOS has no ssh-copy-id and no shell to run one. When the key lives on the phone, the phone's SSH client has to do the job. In Shell Drop there are three ways:
~/.ssh/authorized_keys over its own channel without typing into your terminal, signs in with the key to prove it works, and only then switches the saved connection to the key. If anything fails, the connection keeps signing in with the password.echo '...' >> ~/.ssh/authorized_keys line to copy and run on the server from any session that already has access.Like ssh-copy-id, the install skips a key that's already in the file and sets ~/.ssh to 700 and authorized_keys to 600. Keys are made under Settings > Login and connections > SSH keys, as Ed25519 keys or Secure Enclave keys whose private half never leaves the phone. SSH keys on iPhone covers key types and one key per device, and SSH on iPhone covers the connection itself.
It logs in to a server, usually with your password, and appends your public key to ~/.ssh/authorized_keys for that user, creating the folder and file with safe permissions if needed. After that, SSH signs in with the key instead of the password.
No. It only copies the public key, the .pub file. The private key stays on your computer.
Windows 10 and 11 include the OpenSSH client but not ssh-copy-id. Pipe the public key over ssh instead: type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" in PowerShell.
Usually permissions: OpenSSH ignores authorized_keys if the file, ~/.ssh or the home directory can be written by other users. Run chmod 700 ~/.ssh, chmod 600 ~/.ssh/authorized_keys and chmod go-w ~ on the server, then check you are logging in as the same user the key was copied to.
There is no ssh-copy-id on iOS. In Shell Drop, log in with the password and choose Set up a key for this server; it creates the key, adds it to authorized_keys, tests it and switches the connection over once it works.