How to Use ssh-copy-id Command (with examples)
1. Copy your keys to the remote machine
The ssh-copy-id
command allows you to easily copy your public key to a remote machine’s authorized_keys
file. This is useful when you want to set up public key authentication and eliminate the need for typing passwords every time you connect to the remote machine.
To copy your keys to the remote machine, use the following command:
ssh-copy-id username@remote_host
username
: The username you use to connect to the remote machine.remote_host
: The hostname or IP address of the remote machine.
Motivation: Using this example, you can quickly and securely copy your public key to the remote machine, enabling you to authenticate without a password. This improves the security of your SSH connections and makes authentication more convenient.
Example Output:
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
username@remote_host's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'username@remote_host'"
and check to make sure that only the key(s) you wanted were added.
2. Copy the given public key to the remote
Sometimes, you may have multiple public key files or you want to specify a different public key file instead of the default id_rsa.pub
. In such cases, you can use the -i
option followed by the path to the public key file.
To copy the given public key to the remote machine, use the following command:
ssh-copy-id -i path/to/certificate username@remote_host
path/to/certificate
: The path to the public key file you want to copy to the remote machine. This can be your own generated key or a specific key for the remote machine.username
: The username you use to connect to the remote machine.remote_host
: The hostname or IP address of the remote machine.
Motivation: This example allows you to copy a specific public key file to the remote machine, giving you more flexibility and control over which key is used for authentication. It can be helpful when you have different key pairs for different purposes or when you are managing multiple SSH connections.
Example Output:
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
username@remote_host's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -i path/to/certificate username@remote_host"
and check to make sure that only the key(s) you wanted were added.
3. Copy the given public key to the remote with a specific port
By default, SSH uses port 22 for communication with the remote machine. However, there are scenarios where the SSH service is running on a different port. In such cases, you can specify the port using the -p
option.
To copy the given public key to the remote machine with a specific port, use the following command:
ssh-copy-id -i path/to/certificate -p port username@remote_host
path/to/certificate
: The path to the public key file you want to copy to the remote machine.port
: The port number on which the SSH service is running on the remote machine.username
: The username you use to connect to the remote machine.remote_host
: The hostname or IP address of the remote machine.
Motivation: This example is useful when you need to connect to a remote machine that has SSH running on a non-standard port. By specifying the port with the -p
option, you ensure that the ssh-copy-id
command connects to the correct port and copies the public key to the appropriate location.
Example Output:
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
username@remote_host's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -i path/to/certificate -p port username@remote_host"
and check to make sure that only the key(s) you wanted were added.
In conclusion, the ssh-copy-id
command is a convenient way of installing your public key on a remote machine for passwordless authentication. By using the different examples provided, you can copy your keys to the remote machine, copy a specific public key, or copy a key with a specific port. With this command, you can simplify your SSH authentication process and enhance the security of your remote connections.