ssh-copy-id
Overview
ssh-copy-id installs your public key on a remote account’s ~/.ssh/authorized_keys with safer permissions than hand-copying. Prefer it over pasting keys via ad-hoc echo >> when the remote still allows password auth for bootstrap.
Syntax
ssh-copy-id [options] [user@]host
ssh-copy-id -i key.pub user@hostCommon Options
| Option | Description |
|---|---|
-i key |
Public key file (default: first available id_*.pub) |
-p port |
SSH port |
-o Opt=Val |
Pass through to ssh |
-f |
Force add without duplicate check (implementation-dependent) |
-n |
Dry-run (where supported) |
Safety
- Still authenticates once with the remote password (or existing key).
- Review remote
authorized_keysafter install.
- Do not use world-writable home or
.sshdirectories — sshd will ignore keys.
Examples with Explanations
Default identity
ssh-copy-id alice@server.example.com
ssh alice@server.example.comExplicit key and port
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 alice@server.example.comVia ProxyJump
ssh-copy-id -o ProxyJump=bastion.example.com alice@internalManual equivalent (when tool missing)
cat ~/.ssh/id_ed25519.pub | ssh alice@server \
'umask 077; mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys'Notes & Pitfalls
- Multiple keys: specify
-iso the wrong identity is not installed.
- Some hardened images disable passwords — need console/cloud-init to place the first key.
- SELinux contexts on
~/.sshcan block access until restored.
Additional Resources
man ssh-copy-id