Setting up SSH Keys in WSL

Setting up SSH keys on Windows is generally done via OpenSSL or Putty. We can set this up and have quick authentication to places like GitHub or Bitbucket. But, we are also able to rely on a Linux distro in WSL for our SSH. If you are developing in WSL, this might be useful as that’s where you will be cloning your git repos to.

This guide walks you through setting up SSH keys in WSL on an Ubuntu 20.04 distribution with Bitbucket as the version control system.

Steps covered in this guide:
> Create New SSH Key
> Add SSH Key to Agent
> Add SSH Key to Project
> Clone Repo & Start Coding

Create New SSH Key

To keep your SSH keys backed up on the local machine in case anything happens to the WSL instance, create a new SSH key within a WSL-linked folder. This will also prevent you from needing to re-enter your password each time by adding the key to the SSH agent.

Run the following command to create a new SSH key in your WSL Ubuntu instance:

# Create a new ssh key
ssh-keygen -t rsa -b 4096 -C "someone@email.com"
WSL ssh-keygen

Add SSH Key to Agent

To avoid entering the passphrase every time you use the key, add the SSH key to the SSH agent:

# Add ssh key to agent
eval $(ssh-agent -s)
ssh-add pw_bitbucket
eval ssh-add for agent example

Add SSH Key to Project

Once the key is ready, add it to your Git service (in this case, Bitbucket). Use the cat command to display the public SSH key (.pub file), then copy the entire key (excluding the email at the end).

cat bitbucket public key

Add your SSH key to Bitbucket:

Now, test the SSH connection:

# Ssh to app/host
ssh -T git@bitbucket.org

This process may be different in corporate environments. You’ll hopefully be guided into the right direction before needing to Google it!

Clone Repo and Start Coding

Now that the SSH key is configured and authenticated, you’re ready to clone repositories into WSL. Navigate to your desired directory in the WSL terminal and use the link provided from web:

clone repo link
git clone example

The repository will be cloned, and you can open it in Visual Studio Code and get working!

open vs code git repo
git repo in visual studio