Setting up SSH Keys for GitHub using WSL and Keychain
3 min read
SSH keys are a secure and convenient way to authenticate with remote servers and services. This blog post will cover how to create SSH keys using the Keychain utility and then add the keys to GitHub.
Though GitHub is mentioned, you can use this for any remote Git service such as GitLab, Bitbucket, or Gitea.
To use Keychain, you'll need to install it on your system. On Ubuntu or other Debian-based systems, you can use the following command to install Keychain:
sudo apt install keychain
Creating SSH Keys
After installing Keychain, you can use the ssh-keygen command to create your SSH keys. This command will prompt you for a file to save the keys and a passphrase to protect the keys.
ssh-keygen -t ed25519 -C "email@example.com"
When prompted, enter a file in which to save the key (you can use the default location by pressing Enter) and enter a passphrase (this is an optional security measure that adds an extra layer of protection to your SSH key).
Take a little time to think of something unique if you plan on having multiple SSH keys.
I suggest using a system like [platform]-[user]-[PC]; Mine would like github-Programazing-WorkPC.
Once your SSH key is generated, you can view it by typing the following command:
The output will be the contents of your public SSH key, which you will need to add to your GitHub account.
Adding the Keys to Keychain
On Ubuntu or other Debian-based systems, you can run the following command to open your shell's login script:
Add the following to the bottom of the file:
eval `keychain --agents ssh --eval ~/.ssh/yourSSHKey`
Tip: If you're new to nano press
ctrl + sto save and
ctrl + xto exit.
After saving and returning to the terminal, you'll want to exit the terminal.
When you open the terminal again, Keychain will prompt you for your password.
That's it! Keychain will act as a go-between and keep your keys added to the same
ssh-agent every session. You'll only have to enter the passwords again when you restart your computer.
Adding the SSH Key to GitHub
Now that you have your SSH keys with Keychain, you can add the public key to GitHub to authenticate with the service.
To do this, log in to your GitHub account and go to the "Settings" page. From there, click on the "SSH and GPG keys" tab and click the "New SSH key" button.
Enter a name for the key in the "Title" field and then paste the contents of your public key file (located at ~/.ssh/id_ed25519.pub) into the "Key" field. Finally, click the "Add SSH key" button to add the key to your GitHub account.
Testing your SSH Key
Once you've added the key to GitHub, type the following into your terminal:
ssh -T firstname.lastname@example.org
If successful, you should be greeted with the following:
Hi UserName! You've successfully authenticated, but GitHub does not provide shell access.
If not, I'd suggest reading [GitHub's Troubleshooting Documentation](https://docs.github.com/en/authentication/troubleshooting-ssh).
By using Keychain, you can securely store your SSH keys and avoid having to enter the passphrase every time you use them. This allows for rapid development as you don't have to enter your SSH key whenever you want to push a change.