Using the ssh-copy-id Command

This tutorial intends to teach you Using the ssh-copy-id Command. SSH will secure your communication between a local machine and a remote machine. In this way, you must provide a password for each time you want to log in. The ssh-copy-id command will help you to access the server without a password.

Now follow the steps below to see how to use the ssh-copy-id command.

Steps To Use the ssh-copy-id Command

Before you start to use the ssh-copy-id command, you must generate the SSH public key. To do this, you can visit this guide on Generate SSH key pairs in Linux.

After you have generated your SSH keys, you must follow the steps below to copy the generated keys to your remote server. This can be done by the ssh-copy-id command.

Step 1 – Add SSH Public Key to the Remote Server with ssh-copy-id Command

At this point, we assumed that you have generated your SSH public key. It should be like the following:

Output
...
The key's randomart image is:
+---[RSA 3072]----+
| ..*E+o          |
|. O+== .         |
|.+.+.o+          |
|o.o.oo .         |
|oB....o S        |
|..=.+o o .       |
|  +=. . o        |
| + o.o.o         |
|o   o++..        |
+----[SHA256]-----+

Now you must use the ssh-copy-id command to add the generated ssh public key to your remote server. The syntax of this command is like the following:

ssh-copy-id -i path-to-ssh-public-key user@remote-server-ip

In the command, you must define the exact path of your SSH public key and your username and for the remote IP address enter the IP or hostname of the remote server.

ssh-copy-id -i /root/.ssh/id_rsa.pub root@remote-server-ip

You will be asked some questions. First, press Yes to continue the process. Then, you must enter the destination password to confirm it. This is the only time you use the password.

When it is completed, try to connect to your remote server without a password.

Step 2 – Connect to the Remote Server Without a Password

At this point, you have copied your SSH public key to your remote server. Now you can easily connect to your server without using a password by using the command below:

ssh user@remote-server-ip

This will enable the passwordless login to the remote server.

Step 3 – The ssh-copy-id Command Usage

At this point, you can use the help command for the ssh-copy-id to see the general syntax and its options.

ssh-copy-id -h

In your output, you will see:

Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n|-s] [-i [identity_file]] [-p port] [-F alternative ssh_config file] [[-o <ssh -o options>] ...] [user@]hostname
        -f: force mode -- copy keys without trying to check if they are already installed
        -n: dry run    -- no keys are actually copied
        -s: use sftp   -- use sftp instead of executing remote-commands. Can be useful if the remote only allows sftp
        -h|-?: print this help

Conclusion

At this point, you have learned to enable the passwordless login to the remote server by using the ssh-copy-id command. You can quickly generate the SSH public key add it to your remote server and try to connect to your server without a password.

Hope you enjoy it. You may be interested in these articles too:

How To SSH into a Windows Machine

Install and Secure SSH Server on Debian 12 Bookworm

How to Secure SSH in Linux

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular