Skip to content

ssh-copy-id Command Cheat Sheet

ssh-copy-id is a script that uses ssh to log into a remote machine and append the indicated identity file to that machine's ~/.ssh/authorized_keys file.


Synopsis

ssh-copy-id [options] [user@]hostname

Basic Usage

Auto-detect Protocol

Installs your default identity (~/.ssh/id_rsa.pub or similar).

ssh-copy-id user@server.com
You will be prompted for the user's password once.


Specify Key (-i)

If you have multiple keys, specify which one to upload.

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server.com
Tip: You can point to the private key filename (id_ed25519), and it will automatically find the .pub file.


Dry Run (-n)

Show what would have been installed, without actually doing it.

ssh-copy-id -n user@server.com

Use Custom Port (-p)

Pass SSH options using quotes.

ssh-copy-id -p 2222 user@server.com
# OR
ssh-copy-id -o "Port 2222" user@server.com

Manual Alternative

If ssh-copy-id is not available (e.g., in some minimal environments or Windows PowerShell):

cat ~/.ssh/id_rsa.pub | ssh user@server "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Notes

  • Permissions: ssh-copy-id automatically handles folder permissions/SELinux contexts on the remote side if possible.
  • Idempotency: It checks if the key is already present to avoid duplicates.