Setting Up SSH Keys in WSL for GitHub and Bitbucket

🐧Part of the WSL Complete Guide, every Windows Subsystem for Linux post on this site, organised by task.

To use SSH keys with GitHub or Bitbucket from WSL, create a key inside your Linux distro with ssh-keygen, load it into the SSH agent, add the public key to your Git service, then clone with the SSH address. Keys made in WSL stay in WSL; Windows has its own, separate set, covered in SSH Keys for GitHub on Windows.

Run everything below in your WSL terminal. My screenshots are from 2020 with Bitbucket; GitHub works the same way.

Create the Key

Shell
# Change the email to yours. Press Enter to accept the default file, then set a passphrase
ssh-keygen -t ed25519 -C "you@example.com"

Ed25519 is what GitHub recommends today, and Bitbucket supports it too. In 2020 I used RSA (-t rsa -b 4096), which still works, and saved the key under its own name:

ssh-keygen creating a new SSH key in WSL Ubuntu in 2020

The default file is ~/.ssh/id_ed25519 (private key, never share it) and ~/.ssh/id_ed25519.pub (public key, the one you give to GitHub or Bitbucket).

Load It Into the SSH Agent

Shell
# Start the agent and add the key. Use the file name you chose above
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-agent started and the key added with ssh-add in WSL

The agent remembers your passphrase for as long as that terminal is open, so you type it once per terminal instead of on every git pull. If you named the key something else, as I did in the screenshot, use that name on the ssh-add line.

Add the Public Key to GitHub or Bitbucket

Shell
cat ~/.ssh/id_ed25519.pub
The public key printed with cat in WSL

Copy the whole line, from ssh-ed25519 to the end, and paste it into your Git service: in GitHub that is Settings > SSH and GPG keys > New SSH key, and in Bitbucket Personal Bitbucket settings > SSH keys.

Adding the SSH public key in Bitbucket settings

Then test the connection:

Shell
# GitHub
ssh -T git@github.com

# Bitbucket
ssh -T git@bitbucket.org
ssh -T git@bitbucket.org confirming the key is accepted

The first time, type yes to trust the host. A message with your username means it worked.

Clone and Start Working

Copy the SSH address from the repository page (it starts with git@, not https://) and clone it into your Linux home folder:

Copying the SSH clone address from a Bitbucket repository
git clone over SSH into WSL
Shell
cd ~
git clone git@github.com:your-name/your-repo.git
cd your-repo
code .
Opening the cloned repository in VS Code from WSL with code .
The cloned repository open in VS Code

Keeping the repository in the Linux filesystem rather than under /mnt/c is what keeps Git fast; why is in Open WSL Files in Windows File Explorer. For a repository you already cloned over HTTPS, switch it to SSH. If Git is not on the Windows side yet, Install Git on Windows covers it, and Link Windows Files and WSL shows how to reach the same files from both sides.

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.