ShellSage
ShellSage SSH key security and server access control
SSH BASICS

How to Copy Your SSH Public Key to a Server with ssh-copy-id

Learn to securely copy your SSH public key to a remote server using ssh-copy-id.


To copy your SSH public key to a server, use the ssh-copy-id command, which simplifies the process of adding your public key to the server's authorized keys.

What is ssh-copy-id?

The ssh-copy-id command is a handy utility available on most Unix-like systems. It automates the process of copying your SSH public key to a remote server's ~/.ssh/authorized_keys file. This allows you to log in without a password, enhancing security and convenience.

Using ssh-copy-id saves time and minimizes errors when setting up SSH key-based authentication.

When you run ssh-copy-id, it handles the following tasks:

This command is especially useful for system administrators and developers who frequently need to access multiple servers securely.

How to Use ssh-copy-id

Using ssh-copy-id is straightforward. Ensure you have your SSH public key generated. If not, you can create one using ssh-keygen.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This command generates a new SSH key pair. The default location for the keys is ~/.ssh/id_rsa for the private key and ~/.ssh/id_rsa.pub for the public key. Once you have your public key, you can proceed with the ssh-copy-id command.

Here’s how you can copy your public key to a remote server:

ssh-copy-id user@hostname

Replace user with your username on the remote server and hostname with the server's address. You will be prompted for your password on the remote server. After entering it, your public key will be copied, and you should see a message confirming success.

The first time I used ssh-copy-id, I forgot to check if my public key was already on the server. It’s worth verifying before trying to copy again!

What If ssh-copy-id Is Not Available?

On some systems, ssh-copy-id may not be installed by default. In such cases, you can manually copy your public key using ssh and cat commands.

First, you can display your public key using:

cat ~/.ssh/id_rsa.pub

Then, you can log in to your server:

ssh user@hostname

Once logged in, create the ~/.ssh directory if it doesn't exist:

mkdir -p ~/.ssh

Next, append your public key to the authorized_keys file:

echo "your_public_key" >> ~/.ssh/authorized_keys

Replace your_public_key with the actual key you displayed earlier. Finally, set the correct permissions for the ~/.ssh directory and the authorized_keys file:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

These permissions ensure that only your user can access these files, which is crucial for SSH security.

How to Verify SSH Key Authentication

After copying your public key, it's essential to verify that passwordless SSH is working correctly. You can do this by attempting to log in to the remote server again:

ssh user@hostname

If everything is set up correctly, you should log in without being prompted for a password. If you still have to enter a password, check the following:

Using ShellSage, you can easily check these configurations from your phone. Sage can explain the output of your commands, helping you troubleshoot any issues effectively.

Common Issues and Troubleshooting

Even with the best intentions, issues can arise when setting up SSH key authentication. Here are a few common problems and their solutions:

ssh-add ~/.ssh/id_rsa

Using ShellSage, you can quickly check your firewall settings and SSH configurations right from your device. The blast-radius preview feature helps you understand the impact of commands before running them, reducing the risk of misconfiguration.

⚡ Key takeaways

Saqlain Bukhari
Your Questions, Answered

Frequently Asked Questions

If you forget your SSH key passphrase, you cannot recover it. You will need to generate a new key pair.
Yes, you can specify a non-default port using the <code>-p</code> option: <code>ssh-copy-id -p 2222 user@hostname</code>.
Check that the SSH service is running on the server and that your network connection is stable. You can also manually copy the key as described.
Yes, <code>ssh-copy-id</code> uses the same secure SSH protocol to copy keys as standard SSH connections.
Download ShellSage free
ShellSage is an AI-first SSH terminal with Sage built in: it explains output, fixes broken commands, and previews the blast radius before anything runs. Start on the free Hobby plan with no credit card, and get a 7-day full-Pro trial when you want everything.
← More tutorials