DEV Community

Ayomide Aluko
Ayomide Aluko

Posted on

How to Set Up SSH Key Authentication with GitHub on Windows

Introduction

In this article, I'll walk through the process of setting up and troubleshooting SSH key authentication for connecting to GitHub repositories on a Windows system using Git Bash. SSH key authentication provides a secure and convenient way to interact with GitHub without entering your username and password repeatedly.

Prerequisites

Before you begin, ensure you have the following:

  • Git installed on your Windows machine. Download here
  • A GitHub account. If you don't have one, you can sign up at GitHub.

Steps to Set Up SSH Key Authentication

1. Generate SSH Key Pair

  1. Open Git Bash on your Windows machine.
  2. Create a new ssh folder:
   mkdir ssh
Enter fullscreen mode Exit fullscreen mode
  1. Change your directory into that folder:
   cd ssh
Enter fullscreen mode Exit fullscreen mode
  1. Generate a new SSH key pair by running the following command:
   ssh-keygen -t rsa -C "your-email-address" -f "github-username"
Enter fullscreen mode Exit fullscreen mode

Replace with your email address and username associated with your GitHub account.

  1. Optionally, provide a passphrase for extra security.

2. Start SSH Agent and Add SSH Key

  1. You can now change directory back into your default root:
cd ..
Enter fullscreen mode Exit fullscreen mode
  1. Start the SSH agent by running:
   eval $(ssh-agent -s)
Enter fullscreen mode Exit fullscreen mode
  1. Add your SSH private key to the SSH agent:
   ssh-add /c/Users/USER/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Replace /c/Users/USER/.ssh/id_rsa with the actual file location.

3. Add SSH Public Key to GitHub Account

  1. Change directory into the ssh folder again:
cd ssh
Enter fullscreen mode Exit fullscreen mode
  1. Copy the contents of your SSH public key (id_rsa.pub):
   cat id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

replace "id_rsa.pub" with the name of your ssh with the .pub extension.

  1. Log in to your GitHub account.
  2. Navigate to Settings > SSH and GPG keys.
  3. Click on New SSH key.
  4. Paste your SSH public key into the Key field and give it a descriptive title.
  5. Click Add SSH key to save.

4. Configure SSH for GitHub

  1. Change directory into the ssh folder again:
cd ssh
Enter fullscreen mode Exit fullscreen mode
  1. Open or create the SSH configuration file (config) in the .ssh directory:
   touch config.txt
Enter fullscreen mode Exit fullscreen mode

it is a normal txt file so fine ways to add the information below to it, you can use vim too.

  1. Add the following SSH configuration for GitHub in the config file:
   Host github.com
     HostName github.com
     User git
     IdentityFile "/c/Users/USER/.ssh/id_rsa"
Enter fullscreen mode Exit fullscreen mode

Replace /c/Users/USER/.ssh/id_rsa with the path to your SSH private key. Don't forget the double quote if you are on windows, use this if you are on mac:

~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

5. Test SSH Connection to GitHub

  1. Test the SSH connection to GitHub:
   ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode
  1. If prompted to confirm the authenticity of the host, type yes and press Enter.

Troubleshooting

  • Permission Denied (publickey) Error: If you encounter this error, ensure that your SSH key is added to the SSH agent and correctly configured in your GitHub account and SSH configuration file.
  • Path Formatting (Windows): Use forward slashes (/) and quotes (" ") for file paths in the SSH configuration file (config) on Windows.

Conclusion

By following these steps, you should now have SSH key authentication set up and working with GitHub on your Windows machine. This method provides a secure and efficient way to interact with GitHub repositories using Git commands without the need to enter your GitHub credentials repeatedly.

Feel free to share this article with others who may encounter similar issues when setting up SSH key authentication with GitHub on Windows.

Top comments (0)