Skip to main content
How To

Using ssh-copy-id Command on Ubuntu

The ssh-copy-id command on Ubuntu helps you add your local machine's public key to remote server's authorized keys for password-less SSH access.

Sagar Sharma

If you are into servers and deploy servers on a regular basis and use SSH to get into the system, the ssh-copy-id command can be a lifesaver for you.

Adding GPG key based password-less authentication is one of the ways to harden the SSH. More than security, I feel, it adds the comfort of a password-less entry.

Using the ssh-copy-id command, you can do it without any hiccups. The ssh-copy-id command adds your public key to the remote host's authorized keys and thus enable the password-less login in SSH. It also takes care of the correct file permissions on the files. In short, it saves several manual tasks.

In this guide, I will show you how to use the ssh-copy-id command to have a hassle-free SSH connection.

Use ssh-copy-id command

🚧
Make sure that the remote machine you want to use the ssh-copy-id command is accessible via SSH, or else, it won't work.

The ssh-copy-id command is part of the OpenSSH package and you'd find it pre-installed in most Linux systems.

But somehow, if you don't have it, you can easily install SSH on Ubuntu using the following command:

sudo apt install openssh-client

Once you have it, you can start with the shown steps to send your public key using the ssh-copy-id command and have password-less SSH.

📋
These steps are performed on the client machine from where you want to access the remote server via SSH.

Prerequisite: Generate SSH keys

To enable GPG key based login, you need to have GPG based SSH keys first.

If you have used SSH on this machine to connect to remote server, you should already have the SSH keys.

Still, verify it by checking the content of the .ssh folder in your home directory.

ls ~/.ssh

You should see id_rsa and id_rsa.pub files here.

Check if you have SSH enabled on Ubuntu

If you don't see this folder or if it is empty, you need generate SSH keys on Ubuntu. Run the following command:

ssh-keygen

You can follow the simple procedure easily.

Generate SSH keys in Ubuntu

If you need details, this tutorial will help you out.

Generate SSH keys on Ubuntu to Connect to Remote Systems
Learn to generate and use SSH keys on an Ubuntu client machine for connecting to remote Linux servers.

Step 1: Add the public key to your remote server

Once you are done with key generation, you can use the ssh-copy-id command in the following manner to copy the public key to your server:

ssh-copy-id username@server-ip

Username is the user on the server, not the local machine. It could be root or other user on the server for which you have access via password.

use ssh-copy-id command to copy public key to the Ubuntu server

If you notice carefully, I entered the password but still wasn't logged in to the system.

The reason is simple. The password was asked by SSH as it needs to access your remote system to copy your public key to the ~/.ssh directory and the file would be named as authorized_keys.

On the server, you can check the content of the authorized keys to see if your local system's key was added.

where the public keys are stored when you use ssh-copy-id command

Step 2: Use SSH without a password

Once you use the ssh-copy-id command successfuillu, you no longer have to enter password while using SSH to connect to the remote server.

And as you can see, it didn't ask for the password!

Few things to know

If you use more than one user on the server, and you want password-less SSH access for all of them, you have to use the ssh-copy-id command for each users. You must know the password for each user, of course.

Similarly, if you access several servers, you need to run ssh-copy-id for each server.

This way, you don't need to enter the passwords for each user and each machine. It saves a lot of time and hassle.

By the way, if you use several servers, you can use ~/.ssh/config files and add server details there. This way, you don't even need to remember IP addresses of the servers. How cool is that!

How to Use SSH Config File [Beginner’s Guide]
Using SSH profiles can help you in cases where you regularly connect to various servers. No need to remember the IP address and other such details for SSH connection.

Enjoy the hassle free SSH experience :)

Sagar Sharma