LinuxCommandLibrary

ssh-copy-id

TLDR

Copy the default public key to a remote host

$ ssh-copy-id [user@host]
copy
Copy a specific public key
$ ssh-copy-id -i [~/.ssh/id_ed25519.pub] [user@host]
copy
Copy to a host on a non-standard port
$ ssh-copy-id -p [2222] [user@host]
copy
Dry run to see which keys would be installed
$ ssh-copy-id -n [user@host]
copy
Force copy without checking for existing keys
$ ssh-copy-id -f -i [~/.ssh/id_rsa.pub] [user@host]
copy

SYNOPSIS

ssh-copy-id [-f] [-n] [-s] [-i identityfile] [-p port] [-o sshoption] [user@]hostname

DESCRIPTION

ssh-copy-id installs SSH public keys on a remote server's authorizedkeys file, enabling passwordless authentication. It connects via SSH (usually with password authentication), creates the ~/.ssh directory and authorizedkeys file if needed, and appends your public key.
The script ensures correct permissions are set: ~/.ssh directory at 700 and authorized_keys at 600, which SSH requires for security. Incorrect permissions cause authentication failures.
By default, ssh-copy-id uses keys from ssh-add -L or the most recent ~/.ssh/id*.pub file. Use -i to specify a different key.

PARAMETERS

-i identityfile_

Use the specified identity file (public key)
-p port
Connect to the specified port on the remote host
-f
Force mode; don't check if keys already exist on remote
-n
Dry run; print keys that would be installed without installing
-s
Use sftp instead of cat for copying (useful for restricted shells)
-o sshoption_
Pass options to the underlying ssh command

CAVEATS

Password authentication must be enabled on the remote host for the initial copy. After installation, you may want to disable password authentication in sshd_config. The script is a shell wrapper around ssh, so all ssh options apply.

HISTORY

ssh-copy-id is a convenience script included with OpenSSH to simplify the public key installation process. It automates what would otherwise require manually copying keys and setting permissions correctly.

SEE ALSO

Copied to clipboard