I have generated an SSH key a number of times, either at work or on my personal PC for a number of accounts. Now that I am using my Surface Book 2 as my primary development device until I get a new SSD for my desktop PC, I thought that it might be worthwhile documenting how to generate an SSH key and add it to a GitHub account.
Now if you already have an existing SSH key then you can go ahead and use that but if you have never generated an SSH key before or you may have forgotten how to then hopefully you will find this guide useful.
Assumptions/Pre-Requisites
- You already have Git installed on your device.
- You will be using GitHub for your repositories.
- You are using a Windows environment.
Step 1 – Create a new SSH Key
Do not proceed with this step until you have the pre-requites met otherwise this guide will not work for you unfortunately.
- Open your Git Bash Terminal.
- Execute the following command replacing <github email address> with the email address you use for your GitHub account:
ssh-keygen -t rsa -b 4096 -C <github email address>
The parameters used in the above command are:
-t rsa – the type of key to create. In this case an RSA Key.
-b 4096 – the number of bits in the key.
-C <github email address> – a comment or label for the key.
For more information about the parameters that can be used click here. - You will be prompted to provide a file location where to save the SSH Key. Press Enter. This will save the file in the default location. Using the default location is recommended in this case.
- You will be prompted to enter a secure passphrase. If you choose to provide a secure passphrase then it will secure your SSH Key, this is a recommended step.
Your SSH key should now be generated 🙂
Step 2 – Adding your new SSH Key to the ssh-agent
If you have successfully performed the step “Step 1 – Create a new SSH Key” or you already have an SSH key that you want to use then you can proceed with the following step.
- Open your Git Bash Terminal (if you have closed it).
- Execute the following command:
eval $(ssh-agent -s)
This will start the ssh-agent in the background.
- Execute the following command:
ssh-add ~/.ssh/id_rsa
Note: If your key is not in the file named “id_rsa” or is not in the same location then you will need to replace the value id_rsa in the command with the name of the private key file and/or the location of where the file is store. The above example is for the default name and location.
Step 3 – Adding your SSH Key to your GitHub Account
The hard work is now all done. The next part is super easy even though there are more steps.
- Open your Gut Bash Terminal (if you have closed it).
- Execute the following command:
clip < ~/.ssh/id_rsa.pub
This will copy the content of your public key file ready to be pasted into GitHub. It is important to not add or remove any spaces or newlines to the content.
- Log into GitHub.
- Navigate to the “Settings” page.
- Under “Personal Settings” select “SSH and GPG keys”.
- Click “New SSH key”.
- Add a Title to your key. In this case for me it was generated from my Surface Book 2 so I made sure that it was referenced in the Title.
- Paste your key in the “Key” field.
- Click “Add SSH key”.
You should now see a new SSH Key added to your GitHub profile.
For more information generating your SSH Key, adding your SSH Key to your GitHub account, and other SSH connection information take a look at the GitHub Help Page. I have tried to simplify the process by combining the information that was scattered across two to three pages from the GitHub Help Pages into one page. The GitHub team have done a fantastic job in documenting the entire process however and the above link should be used if my steps are not sufficient or confusing in any way.