Home > Network Management > ssh-copy-id

ssh-copy-id: Copy SSH Public Key

The ssh-copy-id command is a utility that easily and securely adds your local system's SSH public key to the authorized_keys file on a remote server. This allows you to set up passwordless SSH connections and reduces errors that can occur during manual key copying.

Overview

ssh-copy-id automates the process of setting up SSH public key authentication, enabling users to securely connect to remote servers without a password. This command by default uses standard public key files like ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub, but you can also specify a particular key file.

Key Features

  • Copies SSH public key to the remote server
  • Automatically sets permissions for the authorized_keys file
  • Supports establishing passwordless SSH connections
  • Prevents duplicate key additions even when run multiple times (idempotency)

Key Options

The ssh-copy-id command offers several useful options to finely control the key copying process.

Key and Connection Settings

Execution Modes

Generated command:

Try combining the commands.

Description:

`ssh-copy-id` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various scenarios for copying SSH public keys to a remote server using ssh-copy-id.

Basic Usage

ssh-copy-id user@remote_host

Copies the default public key (e.g., ~/.ssh/id_rsa.pub) to the 'user' account on 'remote_host'. You will be prompted to enter the password.

Specify a Specific Key File

ssh-copy-id -i ~/.ssh/my_custom_key.pub user@remote_host

Copies the key using a specified public key file (e.g., ~/.ssh/my_custom_key.pub).

Using a Non-Standard SSH Port

ssh-copy-id -p 2222 user@remote_host

Use this when the remote server runs its SSH service on a port other than the default (22), such as port 2222.

Combining Multiple Options

ssh-copy-id -i ~/.ssh/another_key.pub -p 2222 user@remote_host

An example of copying a key to a non-standard port using a specific key file.

Tips & Precautions

Useful tips and precautions when using ssh-copy-id.

Prerequisites

Before using ssh-copy-id, please check the following:

  • Generate Public Key: If you don't have an SSH key pair yet, you must generate one first using the `ssh-keygen` command.
  • Remote Server Access: You need to have a password or another authentication method to access the remote server via SSH. ssh-copy-id requires a password for this initial authentication.

Permission Issues

ssh-copy-id automatically sets the correct permissions for the `~/.ssh` directory and the `~/.ssh/authorized_keys` file on the remote server. If you copied the key manually, you must set the permissions correctly using the commands `chmod 700 ~/.ssh` and `chmod 600 ~/.ssh/authorized_keys`.

Idempotency

ssh-copy-id does not add a key that has already been copied. Therefore, running it multiple times will not result in duplicate keys accumulating in the authorized_keys file. This is a very convenient feature for use in scripts.


Same category commands