Using Multiple SSH Keys for Multiple GitHub Accounts - ossiejhmoore/howto GitHub Wiki

Overview

I have a personal GitHub account. I also have a work account and accounts with various clients. I use SSH keys to authenticate for all of them. A SSH public key will work with only one GitHub account. This will show how to create, assign, and use multiple SSH keys with multiple GitHub accounts.

Using Multiple SSH Keys for Multiple GitHub Accounts provides all the information needed and does an excellent job of explaining the entire process. My only complaint was that (at least to me) it didn't make it clear to me that the hostname you use is the SSH config Host value. I suggest you visit this page for a complete overview of the process. I only provide the raw steps required here so that I personally always have them.

In this example, I use:

These instructions will work on macOS, Linux and Windows.

For Windows, you need to have something installed that gives you the ability to use SSH from the command line. I personally use, love, and recommend Cmder | Console Emulator. It provides a lot more than just git and ssh. If you are a fan of the Linux/macOS command line, give Cmder | Console Emulator a try.

Steps

Create SSH Keys

  • Open a terminal session and change into the .ssh directory of your home directory. This will avoid you having to provide a full path for where to create the keys. (On Windows, you can use the Git bash terminal.)
johnsmith@fractal:~$ cd .ssh
johnsmith@fractal:~/.ssh$ pwd
/home/johnsmith/.ssh
johnsmith@fractal:~/.ssh$ 
  • Create SSH keys for your personal email.
johnsmith@fractal:~/.ssh$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/johnsmith/.ssh/id_rsa): id_rsa_personal
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa_personal
Your public key has been saved in id_rsa_personal.pub
The key fingerprint is:
SHA256:FzGcOcc9CsdY/jlT/TxZdA4Z+UwtN+4eZk5Oem5msXc [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|         .oB..o++|
|          B+= *=*|
|          .=...B*|
|           ... =*|
|        S .   *oo|
|         .     @.|
|              X +|
|             . XE|
|              *.o|
+----[SHA256]-----+
johnsmith@fractal:~/.ssh$ 
  • Create SSH keys for your work email.
johnsmith@fractal:~/.ssh$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/johnsmith/.ssh/id_rsa): id_rsa_work
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa_work
Your public key has been saved in id_rsa_work.pub
The key fingerprint is:
SHA256:kEKNMexXJo8dmrsI5REt34+OyE/TWH2iVKv7m4xEJc4 [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|   .+=           |
|   .=.+.+        |
|   [email protected]      |
|    +.*++= .     |
|   o o .Eo+ .    |
|  . . .*.o.o     |
|   o o++=        |
|    +.oo.+ .     |
|     .. o.=.     |
+----[SHA256]-----+
johnsmith@fractal:~/.ssh$ 
  • Confirm the keys were created.
johnsmith@fractal:~/.ssh$ ls -l
total 16
-rw------- 1 johnsmith johnsmith 2610 Oct 30 07:00 id_rsa_personal
-rw-r--r-- 1 johnsmith johnsmith  575 Oct 30 07:00 id_rsa_personal.pub
-rw------- 1 johnsmith johnsmith 2602 Oct 30 07:03 id_rsa_work
-rw-r--r-- 1 johnsmith johnsmith  573 Oct 30 07:03 id_rsa_work.pub
johnsmith@fractal:~/.ssh$ 

Keep this terminal window open, you will need it in the next step.

Add Keys to GitHub Accounts

Add the id_rsa_personal.pub public key to your personal GitHub account.

  • Use the cat command in the terminal to display the contents of your public key (id_rsa_personal.pub) using the cat command.
johnsmith@fractal:~/.ssh$ cat id_rsa_personal.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDA7qI3tW8boOx72r1tC7kHF4LM2t5SINy52RsDvOC
D9eX9SV19UoiwD0qbwA0BFPa0FTzbMgyrn/byT8JB/v15i10wwDZFjc2XY720pYPFz8EoCNH8X4i6Jm
ENSYAv4lhjbJnwPE/aUKv929mEV24WHbddXRINReCModwCdK3DHVn8VXgsCa1roI28bhd8Er9Zm/Sx7
qvhb4pIXFNbjw3PAuMMHJxFYWrFztR2XUHoIwxPZeJzEgNnwJWqx6iwKU+ey6UGtEtG5HhEkMHD/O+x
eO2x2f7oHfO7YI8BNbovB1P5UuU/pFvwRQ/3BHVklCJIjHLIsz6E1+wmAmVa5M/84wa/sqEbEVdtkhb
K/pkYvOH7hrfUqeYHo/n15z5kG4QvBRIrJW1jT3BL1bWWiRp3ArPJvJZpEHUppQN0asMm7fiaf6+6pw
dEhC4T7kqJm0qOpArOtpSpOkQT1KoQ1RxLLTCL/EUeshiEOwTiVi/8a3ao1tMItiwJhgGXDCeyH70= 
[email protected]
  • Login to GitHub with your personal email address in your web browser.
  • Navigate to Settings. (Click on your account icon in the upper right corner and then click on Settings.)
  • Click on SSH and GPG Keys on the left sidebar.
  • Click the New SSH key button.
  • Give the new SSH key a title and then paste the entire contents of the ssh key into the Key field.