Copying SSH Keys between different hosts


updated

How to copy SSH keys between Linux hosts and from Windows to Linux.

Table of Contents

  1. Copy SSH key between Linux hosts
  2. Copy SSH key from Windows to Linux

Copy SSH key between Linux hosts

ssh-copy-id <user>@<ip-address>

# if target machine's hostname is in /etc/hosts
ssh-copy-id <user>@<hostname>

Copy SSH key from Windows to Linux

cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'cat >> .ssh/authorized_keys && echo "Key copied"'

The above command will fail if the .ssh folder does not already exist on the Linux host. Below is a more advanced command that will create the folder and give it correct permissions.

cat ~/.ssh/id_rsa.pub | ssh <user>@<hostname> 'umask 0077; mkdir -p .ssh; cat >> .ssh/authorized_keys && echo "Key copied"'

Next: GitHub Docs instructions for adding a GPG keys.

Generating an SSH key pair

Setup SSH authentication to push to Github