How to Copy SSH Keys to Another Machine

Estimated reading: 2 minutes 1399 views

Introduction

Using SSH keys is the most secure and convenient way to log in to another machine. Instead of typing your password every time, you can authenticate using a public/private key pair. This guide explains how to copy your public key to a remote machine so that passwordless login works reliably.

1. Verify Your SSH Key Pair

On your local machine, your keys are in the .ssh directory:

ls -l ~/.ssh

You should see:

-rw------- 1 user user 1843 Mar 27 15:09 id_rsa
-rw-r--r-- 1 user user 413 Mar 27 15:10 id_rsa.pub
  • id_rsa → private key (keep secret)

  • id_rsa.pub → public key (this is what we copy to the remote machine)

If you don’t have a key pair, generate one:

ssh-keygen -t ed25519 -C "[email protected]"

2. Copy Your Public Key to the Remote Machine

Option A: Using ssh-copy-id (Recommended)

ssh-copy-id your_username@remote_machine
  • Automatically copies your public key to the remote machine’s ~/.ssh/authorized_keys

  • Sets correct permissions

Test login:

ssh your_username@remote_machine

You should log in without entering a password.


Option B: Manual Copy

  1. Display your public key:

cat ~/.ssh/id_rsa.pub
Log in to the remote machine using a password:
ssh your_username@remote_machine

2. Create the .ssh directory and authorized_keys file:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys

3. Append your public key to authorized_keys:

echo "your-public-key-content" >> ~/.ssh/authorized_keys

Test login again:

ssh your_username@remote_machine

3. Permissions & Best Practices

  • .ssh directory → 700

  • authorized_keys → 600 (or 644 if needed)

  • Never copy your private key to another machine

  • Use modern key types like ed25519 for better security


Conclusion

Copying your SSH public key to another machine allows secure, passwordless logins. The simplest method is ssh-copy-id, which handles everything automatically. If needed, you can do it manually by creating .ssh/authorized_keys and appending your public key. Proper permissions are crucial to ensure the key works and remains secure.

Share this Doc

How to Copy SSH Keys to Another Machine

Or copy link

CONTENTS