How to Set Up SSH Key Authentication with GitHub on Windows
Tags: ssh, github, webdev
Published: 2024-05-14 15:29:23



### 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](https://git-scm.com/download/win)
- A GitHub account. If you don't have one, you can sign up at [GitHub](https://github.com/).

### 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:
```bash
   mkdir ssh
   ```
3. Change your directory into that folder:
```bash
   cd ssh
   ```
4. Generate a new SSH key pair by running the following command:
   ```bash
   ssh-keygen -t rsa -C "your-email-address" -f "github-username"
   ```
   Replace with your email address and username associated with your GitHub account.

5. 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:

```bash
cd ..
```
2. Start the SSH agent by running:
   ```b
   eval $(ssh-agent -s)
   ```
3. Add your SSH private key to the SSH agent:
   ```bash
   ssh-add /c/Users/USER/.ssh/id_rsa
   ```
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
```
2. Copy the contents of your SSH public key (`id_rsa.pub`):
   ```bash
   cat id_rsa.pub
   ```
replace "id_rsa.pub" with the name of your ssh with the `.pub` extension.

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

#### 4. Configure SSH for GitHub

1. Change directory into the ssh folder again:

```bash
cd ssh
```
2. Open or create the SSH configuration file (`config`) in the `.ssh` directory:
   ```bash
   touch config.txt
   ```
it is a normal txt file so fine ways to add the information below to it, you can use vim too.

2. Add the following SSH configuration for GitHub in the config file:
   ```plaintext
   Host github.com
     HostName github.com
     User git
     IdentityFile "/c/Users/USER/.ssh/id_rsa"
   ```
   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
```

#### 5. Test SSH Connection to GitHub

1. Test the SSH connection to GitHub:
   ```bash
   ssh -T git@github.com
   ```
2. 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.