DEV Community

Seamoon Pandey
Seamoon Pandey

Posted on

Transferring SSH Keys (`id_rsa` and `id_rsa.pub`) to a New System

SSH keys are essential for secure communication between computers. If you're migrating to a new system but want to retain your existing SSH keys (id_rsa and id_rsa.pub), follow these steps to seamlessly transfer them.

Step 1: Locate Your SSH Keys

First, identify the location of your SSH keys on your old system. Typically, they reside in the ~/.ssh/ directory.

cd ~/.ssh/
ls -al
Enter fullscreen mode Exit fullscreen mode

Step 2: Backup Your Keys

Ensure the safety of your SSH keys by creating a backup. Copy them to a secure location, such as an encrypted USB drive or a trusted cloud storage service.

cp id_rsa id_rsa.pub /path/to/backup/location
Enter fullscreen mode Exit fullscreen mode

Step 3: Transfer the Keys to the New System

Use the scp (secure copy) command to transfer the SSH keys to your new system.

scp /path/to/backup/location/id_rsa /path/to/backup/location/id_rsa.pub user@new_system_ip:~/.ssh/
Enter fullscreen mode Exit fullscreen mode

Replace user with your username on the new system and new_system_ip with the IP address or hostname of your new system.

Step 4: Set Permissions

Ensure that the permissions of your id_rsa file are secure. Only the owner should have read and write permissions.

chmod 600 ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Step 5: Add Public Key to Authorized Keys

Append the contents of your id_rsa.pub file to the authorized_keys file on your new system. This allows you to authenticate using your SSH private key.

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

Step 6: Test the Connection

Finally, test the SSH connection to your new system to ensure everything is set up correctly.

ssh user@new_system_ip
Enter fullscreen mode Exit fullscreen mode

If successful, you should log in to your new system without being prompted for a password.

Congratulations! You've successfully transferred your SSH keys to your new system, ensuring secure access to your remote servers.


Feel free to customize the instructions further based on your specific environment or requirements!

Top comments (0)