How to Copy SSH Key to Ubuntu Server with ssh-copy-id

Using SSH keys for authentication provides a secure and convenient way to log into your remote Ubuntu servers without typing a password every time. This article demonstrates how to use the ssh-copy-id command-line tool to quickly and safely copy your local SSH public key to a remote Ubuntu Linux server. By the end of this guide, you will be able to configure passwordless SSH access in just a few simple steps.

Prerequisites

Before you begin, ensure you have: * An SSH key pair generated on your local computer. * The IP address or domain name of your remote Ubuntu server. * The username and password for a user account on the remote server.

If you do not have an SSH key pair on your local machine, you can generate one by running the following command in your local terminal and following the prompts:

ssh-keygen -t ed25519

Step 1: Copy the SSH Public Key to the Remote Server

The ssh-copy-id tool is the easiest and most secure way to install your public key on a remote machine. It automatically appends your local public key to the ~/.ssh/authorized_keys file on the remote server and configures the correct file permissions.

Open your local terminal and run the following command, replacing username with your remote username and remote_host with your server’s IP address or domain:

ssh-copy-id username@remote_host

If your remote server uses a custom SSH port (other than the default port 22), specify the port using the -p flag inside quotes, or use the -i flag to specify a path to a specific identity file:

ssh-copy-id -p 2222 username@remote_host

Step 2: Authenticate and Complete the Transfer

After running the command, you will see a output in your terminal:

  1. If this is your first time connecting to this server, your terminal will ask you to confirm the authenticity of the host. Type yes and press Enter.
  2. The utility will then prompt you to enter the password for the remote user account.
  3. Type your password and press Enter.

Once authenticated, ssh-copy-id will copy the public key and display a success message indicating that the key has been added.

Step 3: Test the Passwordless Login

To verify that the SSH key was copied successfully and that you can log in without a password, attempt to connect to the remote server using the standard SSH command:

ssh username@remote_host

If the configuration is correct, the remote server will authenticate your session using your local private key, and you will be logged directly into the Ubuntu shell environment without being prompted for a password.