Skip to content

ssh-copy-id command in Linux

The Linux ssh-copy-id command simplifies the process of copying SSH keys to remote servers, allowing for secure and efficient access without the need for manual key management. By using ssh-copy-id, users can quickly and easily authorize their public keys on remote servers, streamlining the authentication process and enhancing overall security.

Terminal window
ssh-copy-id [options] [user@]hostname
OptionDescription
-iIdentity file
-fForce mode (overwrite keys)
-nDry run (do not make any changes)
-hShow help
-oAdditional options
-pPort number
ParameterDescription
[options]Specify the options for the ssh-copy-id command
[user]Username for the SSH connection
[hostname]The target host to copy the SSH key to
Terminal window
ssh-copy-id user@remote_host

Copies the SSH key of the local user to the specified remote host.

Terminal window
ssh-copy-id "-p 2222 user@remote_host"

Copies the SSH key while specifying a non-default SSH port (in this case, port 2222) for the remote host.

Terminal window
ssh-copy-id -i user@remote_host

Prompts for confirmation in interactive mode before adding the SSH key to the remote host.

Copy SSH key using a specific identity file

Section titled “Copy SSH key using a specific identity file”
Terminal window
ssh-copy-id -i ~/.ssh/another_key user@remote_host

Copies the SSH key specified by the identity file parameter to the remote host.

Disable password authentication during key copy

Section titled “Disable password authentication during key copy”
Terminal window
ssh-copy-id -n user@remote_host

Disables password authentication temporarily during the key copy process for increased security.

Specify a custom path for the authorized keys file

Section titled “Specify a custom path for the authorized keys file”
Terminal window
ssh-copy-id "-f ~/.ssh/custom_authorized_keys user@remote_host"

Copies the SSH key to the remote host while using a custom path for the authorized keys file.

Preserve remote key in the known_hosts file

Section titled “Preserve remote key in the known_hosts file”
Terminal window
ssh-copy-id "-o 'UserKnownHostsFile=~/.ssh/known_hosts' user@remote_host"

Preserves the remote host key in the known_hosts file, which is useful for keeping track of verified host keys.

Terminal window
ssh-copy-id "-F /etc/ssh/ssh_config user@remote_host"

Copies the SSH key to the remote host using a specified SSH configuration file for custom settings.

To use the ssh-copy-id command in Linux, execute the following command:

Terminal window
ssh-copy-id user@hostname

How to copy the SSH key to a specific port using ssh-copy-id?

Section titled “How to copy the SSH key to a specific port using ssh-copy-id?”

To copy your SSH key to a specific port on a remote host with ssh-copy-id, use the following command:

Terminal window
ssh-copy-id -p port user@hostname

How can I specify a custom SSH key file using ssh-copy-id?

Section titled “How can I specify a custom SSH key file using ssh-copy-id?”

To specify a custom SSH key file when using ssh-copy-id, you can use the following command:

Terminal window
ssh-copy-id -i /path/to/key user@hostname

How do I disable strict host key checking when using ssh-copy-id?

Section titled “How do I disable strict host key checking when using ssh-copy-id?”

To disable strict host key checking and automatically add the host key when using ssh-copy-id, you can use the following command:

Terminal window
ssh-copy-id -o StrictHostKeyChecking=no user@hostname

How to prompt for the user’s password on the remote system when using ssh-copy-id?

Section titled “How to prompt for the user’s password on the remote system when using ssh-copy-id?”

To prompt for the user’s password on the remote system when using ssh-copy-id, you can use the following command:

Terminal window
ssh-copy-id -o PubkeyAuthentication=no user@hostname

How to copy a specific SSH key to a remote host using ssh-copy-id?

Section titled “How to copy a specific SSH key to a remote host using ssh-copy-id?”

To copy a specific SSH key to a remote host with ssh-copy-id, use the following command:

Terminal window
ssh-copy-id -i /path/to/key user@hostname

How do I provide a custom port and user when using ssh-copy-id?

Section titled “How do I provide a custom port and user when using ssh-copy-id?”

To specify both a custom port and user when using ssh-copy-id, you can use the following command:

Terminal window
ssh-copy-id -p port user@hostname

How do I add a comment when copying an SSH key with ssh-copy-id?

Section titled “How do I add a comment when copying an SSH key with ssh-copy-id?”

To add a comment when copying an SSH key using ssh-copy-id, you can use the following command:

Terminal window
ssh-copy-id -C "user@hostname key" user@hostname

How do I force the SSH key copy operation using ssh-copy-id?

Section titled “How do I force the SSH key copy operation using ssh-copy-id?”

To force the SSH key copy operation and overwrite any existing keys on the remote host, you can use the following command:

Terminal window
ssh-copy-id -f user@hostname
  • Securely copy the public key to a remote server for passwordless authentication
  • Automate the process of adding the public key to the authorized_keys file on a remote server