Copying SSH Keys to different Linux Machine

When working with multiple Linux machines, you often need to copy your SSH keys between systems to enable password-less authentication. This process involves securely transferring your public key from one machine to another and properly configuring the target machine to accept your key for authentication.

SSH keys consist of a private key (kept secret on your local machine) and a public key (shared with remote machines). The public key is added to the target machine's authorized_keys file, allowing secure authentication without passwords.

SSH Key Structure

On your local machine, SSH keys are stored in the .ssh directory in your home folder

drwx------ 5 user user 160 Apr 23 13:11 .ssh

The .ssh directory contains your key pair files

$ ls -l .ssh
total 16
-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

The id_rsa file is your private key (never share this), and id_rsa.pub is your public key that gets copied to remote machines.

Method 1: Manual Copy Process

Step 1: Copy the Public Key

First, transfer your public key to the target machine using scp

$ scp ~/.ssh/id_rsa.pub username@remote_host:

Step 2: Set Up Remote Machine

Log into the remote machine and create the necessary directories with proper permissions

$ ssh username@remote_host
$ mkdir -p ~/.ssh
$ chmod 700 ~/.ssh

Step 3: Configure authorized_keys

Create and configure the authorized_keys file

$ touch ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
$ rm ~/id_rsa.pub

Method 2: Using ssh-copy-id (Recommended)

The ssh-copy-id command automates the entire process in a single step

$ ssh-copy-id username@remote_host

This command automatically

  • Copies your public key to the remote machine

  • Creates the .ssh directory if it doesn't exist

  • Sets proper permissions on directories and files

  • Adds your key to the authorized_keys file

Verification

After copying your SSH key, test the connection

$ ssh username@remote_host

If successful, you should log in without being prompted for a password.

File Permissions Summary

File/Directory Permissions Description
~/.ssh/ 700 SSH directory (owner only)
~/.ssh/id_rsa 600 Private key (owner read/write only)
~/.ssh/id_rsa.pub 644 Public key (world readable)
~/.ssh/authorized_keys 600 Authorized public keys (owner only)

Conclusion

Copying SSH keys between Linux machines enables secure, password-less authentication. While manual copying works, ssh-copy-id is the preferred method as it handles all configuration steps automatically and ensures proper file permissions for security.

Updated on: 2026-03-17T09:01:38+05:30

724 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements