ssh-copy-id installs your public key on a remote server. It logs in over SSH with whatever authentication already works, usually a password, creates ~/.ssh if needed and appends the key to ~/.ssh/authorized_keys without touching existing entries. ssh-copy-id -i picks the exact key to copy. Without the tool, cat over SSH with >> does the same job.

What ssh-copy-id does to ~/.ssh/authorized_keys on the server

ssh-copy-id opens a regular SSH connection and writes your public key into the remote user's ~/.ssh/authorized_keys. According to its manual page, it follows these steps:

  1. Selects the keys. With -i it uses the file you name. Without -i it takes the keys listed by ssh-add -L; if the agent is empty, it uses the most recent ~/.ssh/id*.pub.
  2. Skips keys already installed. It tries to log in with each key and only copies the ones that fail.
  3. Prepares the directory. It creates ~/.ssh and authorized_keys if they do not exist, using a restrictive umask.
  4. Appends instead of replacing. New keys go at the end of the file, and existing lines stay as they were.

That first login needs another working authentication method, almost always a password. If the server already runs with PasswordAuthentication no, ssh-copy-id cannot get in. You then have to add the key another way, for example through a different user who already has access. Each line follows the sshd authorized_keys file format: optional options, key type, base64 key and comment.

ssh-copy-id -i: step-by-step syntax with an Ed25519 key

The most explicit form is ssh-copy-id -i path/key.pub user@server. If the file name does not end in .pub, ssh-copy-id adds the extension and copies the public half. OpenSSH has supported Ed25519 since version 6.5 (January 2014), so any current server accepts it.

# 1. Generate the key (if you don't have one yet)
ssh-keygen -t ed25519 -C 'laptop-ana'

# 2. Dry run: show what would be copied, change nothing
ssh-copy-id -n -i ~/.ssh/id_ed25519.pub [email protected]

# 3. Install it
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]

# 4. Server on a non-default port
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 [email protected]

# 5. Confirm it logs in without a password
ssh -o PasswordAuthentication=no -i ~/.ssh/id_ed25519 [email protected]

These are the most useful flags:

  • -n runs a dry pass and prints the keys it would install.
  • -f forces the copy without checking whether the key is already there, and does not need the private key.
  • -p sets the port, which defaults to TCP 22.
  • -o passes any option through to ssh.

Step 5 is the one that matters: if you get in with password authentication disabled on the client side, the key works.

Manual alternative to ssh-copy-id using cat over SSH

This one-liner replaces ssh-copy-id when the tool is not available, for example on Windows without it, in a mobile client or on a minimal system. It creates the directory, appends the key with >> and sets the permissions OpenSSH expects: 700 on ~/.ssh and 600 on authorized_keys.

ssh [email protected] 'umask 077; mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys' < ~/.ssh/id_ed25519.pub

If you already have a shell on the server, pasting the public line is enough:

echo 'ssh-ed25519 AAAAC3Nza...rest-of-key laptop-ana' >> ~/.ssh/authorized_keys

Unlike ssh-copy-id, the manual method does not check for duplicates. Run it twice and the key appears twice. Nothing breaks, but the file gets cluttered and revoking access later becomes harder.

ssh-copy-id vs manual method vs mobile: comparison table

All three routes leave the same line in authorized_keys. What differs is how many safety checks happen for you.

What is comparedssh-copy-id (desktop)Manual method (cat/echo)Mobile (iOS/Android)
AvailabilityShips with the OpenSSH client on many systemsAny shell with ssh or an open sessionNo ssh-copy-id; paste the key in a session
Avoids duplicatesYes, tests each key before copyingNoNo, unless you check the file
Creates ~/.ssh and sets permissionsYes, with a restrictive umaskOnly if your command includes itOnly if your command includes it
Key selection-i path.pubWhatever file you redirectThe key generated on the phone
Preview without changes-n shows what it would copyNot availableNot available

Installing a public key from your phone without ssh-copy-id

iOS and Android do not ship ssh-copy-id, so the phone workflow is the manual one: generate the key, copy the public half and append it from a session opened with a password. In an SSH terminal for iOS and Android such as termi1, keys are generated on the phone and stored in the system's secure keystore. The private key never leaves the device and is never sent to termi1's servers.

  1. Generate an Ed25519 key on the phone and copy its public line.
  2. Connect with your password one last time.
  3. Type the append command with >>. The termi1 keyboard adds Tab, Ctrl and arrow keys, which the system keyboard hides, so editing the line is not a fight with the cursor.
  4. Open a second session and confirm it logs in without a password before closing the first one.

You can also ask termi1's AI with a sentence like 'append my public key to authorized_keys'. The AI types the command at the prompt without pressing Enter, so you can check it uses >> and not > before running it. On connections tagged as production, Production Guard holds back anything that changes state before it reaches the prompt.

For a Mac, termi1 Companion (macOS 14 or later) pairs the iPhone with a six-digit code and installs the public key for you. Once the key is in place, Mosh uses it too, because Mosh starts mosh-server through an initial SSH connection. You can download termi1 for iPhone for free.

4 mistakes when installing a key with ssh-copy-id or by hand

  1. Using > instead of >>. A single arrow replaces authorized_keys and wipes every previous key, possibly including another admin's.
  2. Leaving permissions too open. With StrictModes enabled, its default, sshd ignores the key if the directory or file is writable by other users. Keep 700 on ~/.ssh and 600 on authorized_keys.
  3. Copying the private key. Only the .pub file goes to the server. The private key stays on your machine.
  4. Relying on a legacy RSA signature. Since OpenSSH 8.8 (September 2021), the ssh-rsa signature with SHA-1 is disabled by default. If an old client only offers that signature, login fails even with the key in place. Ed25519 avoids the issue.

Frequently asked questions

Does ssh-copy-id overwrite authorized_keys?

ssh-copy-id does not overwrite authorized_keys: it appends new keys to the end and keeps existing ones. Before copying, it tries to log in with each key and skips those that already work. Running it twice therefore does not duplicate entries, unless you force the copy with -f.

What permissions does ~/.ssh/authorized_keys need?

~/.ssh/authorized_keys should be mode 600, and the ~/.ssh directory mode 700. If other users can write to them, sshd ignores the key while StrictModes is enabled, which is the default. The server then keeps asking for a password even though the key was copied correctly.

How do I use ssh-copy-id with a port other than 22?

ssh-copy-id accepts a port other than 22 through the -p flag, for example ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 user@server. It also takes -o to pass options to ssh, and it honours host aliases in ~/.ssh/config, including their Port and User settings.

How do I install an SSH key generated on my phone?

An SSH key generated on a phone is installed like any other. Copy the public line that starts with ssh-ed25519 and append it with >> to the server's ~/.ssh/authorized_keys. In termi1, the private key stays in the phone's secure keystore, and only the public half travels.