Copying SSH Keys to different Linux Machine


Overview

When working with SSH keys, it is important to keep them safe. We can protect ourselves against accidental deletion by storing them in an encrypted file. We can also make sure they aren’t compromised if someone tries to steal them by keeping them offline. For example, we could store them in a password protected folder on a USB drive.

However, this isn’t the best way to do things. If you have multiple machines and want to copy your key from one machine to another, then there are better ways of doing that than copying the key files over. This article will show you how to copy your SSH keys securely between two different computers.

On Our Machine

On our local computers, we store our SSH keys in a subdirectory called .ssh.

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

The.ssh directory is only accessible by our own user and contains two file −

$ ls -l .ssh
total 16
-rw------- 1 vagrant vagrant 1843 Mar 27 15:09 id_rsa
-rw-r--r-- 1 vagrant vagrant 413 Mar 27 15:10 id_rsa.pub

Our private keys are stored in id_rsasubjective.pem, and we need to move the public part of the pair (id_rsa.pub) to the other computer so that we can use them to sign messages.

Manual Copy

We first need to manually copy the public key from one computer to another. To do so, we use the scp command.

$ scp .ssh/id_rsa.pub our_username@other_machine:

We need to transfer the file from our computer to the remote server by typing our password into the terminal window. After that, we need to log in to the remote server using our user name and password. To ensure that our home folder has the same permissions as the one on our local computer, we first need to make sure that the remote server's home folder also contains a folder named.ssh with the same access privileges as the one on our computer.

$ mkdir .ssh
$ chmod 700 .ssh

This directory should contain an authorized_keys file that is accessible only by our own account.

$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys

The input should be a list of public keys for each key pair that we want to allow users to login with. We'll add the public key that we transferred to this list.

$ cat id_rsa.pub >> .ssh/authorized_keys

We no longer need to type our passwords when logging into this other computer.

Using ssh-copy-id

The manual approach requires a lot of effort. Luckily, Linux comes with a handy tool that can do all the work for us. This tool is named ssh-keygen. We just type the following into our terminal window −

$ ssh-copy-id our_username@other_machine

We're going to copy our public key from its current location into another computer, perform all the same steps we performed manually above.

Conclusion

We learned how to manually create an SSH key pair for use with GitHub. To perform a manual creation, we must ensure that all files are restricted by default. The easiest way to do so is through the use of the ssh-keygen command.

Updated on: 23-Dec-2022

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements