< Add a SSH key to Windows

Recently, I have been experimenting with many different Linux distributions and had generated a SSH key to use for GitHub. As my workflow involves using Git across the projects that I would be working on. In the end I decided to give Windows 10 a try for the interim period. I wanted to retain the SSH key that I had and to add it to the Windows machine. However with Linux/Unix commands being quite different to what is used on Windows.

Here are the steps that I took to add it to my Windows machine, and an additional step in case if the GitHub account did not have it in the list. If GitHub Desktop is installed on the machine, it comes with an useful tool called Git Bash which can be used to perform the tasks below.

Using Git Bash

If Git Bash cannot be found on the machine, and you have GitHub Desktop installed then it would be needed to check the options. This is to ensure that the option for Git Bash is selected to be used as the default shell for Git commands. Run the shortcut Git Shell, and it shall open up Git Bash.

Checking for existing SSH keys

Before a SSH key can be generated, it is good practice to check if there’s any existing SSH keys on the machine. Using Git Bash, type out the command below to find the .ssh folder (this typically is found in c:\Users\YOURNAME\.ssh\) on Windows.

1
2
$ ls -al ~/.ssh
# This will list the files inside .ssh directory (if there are any).

If there are no id_rsa or id_rsa.pub files which is your SSH key within that directory, then it’s good to go for generating a new SSH key.

Generate a new SSH key

Using the same Git Bash window if you were checking to ensure that no existing SSH keys were on the machine, type in the following to generate a SSH key. Change the email placeholder to a valid email address.

1
2
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Create a new SSH key and use provided email address as a label.

Once you’ve entered that, it will prompt you with where to save the SSH key. The default place would be /Users/you/.ssh/id_rsa. Press Enter if there’s no changes to be made for putting the SSH key in a specific place on the machine. There will be a following prompt after this, which enables you to add a passphrase or leave it blank. For obvious reasons, it is recommended that a passphrase is used for the SSH key. The prompt would look like this.

1
2
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Now the machine has a SSH key that can be used for a lot of tasks such as SSH’ing into remote machines, using Git via SSH to do version control over projects.

Add new SSH key to the ssh-agent

Armed with the SSH key on the machine, now it’s time to add it to the ssh-agent. If the same Git Bash window is still running, then type in the following:

1
$ eval "$(ssh-agent -s)"

This command finds out if ssh-agent is running, and if it is then you can proceed to add the SSH key by typing:

1
$ ssh-add ~/.ssh/id_rsa

This will ask you for your passphrase if you have typed one in, and once the passphrase has been added, it will add the SSH key to the machine to be used.

Add the SSH key to the GitHub account

Adding a new SSH key to your GitHub account. This is straight from the horse’s mouth at GitHub. This covers it in detail about how to add the SSH key with screenshots of each step to undertake.

Why did I write this?

This is mainly a quick step guide for myself to refer to later down the line if I needed to get a new SSH key, or if I had misplaced the one I have currently. I have found that if I have little journal entries on things like this, it becomes really useful for me and I don’t need to resort to searching for it on the Internet. I have found one of my entries to be useful time and time again.