Github SSH Key Authentication in Windows

This article will teach you how to generate an SSH key and configure git to use it as an authentication method into github.

Generating an SSH key and configuring it with GitHub is a great way to securely authenticate without needing to enter your username and password every time. In fact Github has disabled the ability to connect remotely through username and password as of August 2021.

Step 1: Check for Existing SSH Keys

First, check if you already have an SSH key:

  1. Open a terminal (or Git Bash on Windows).
  2. Run the following command to check for existing keys
dir C:\Users\YourUsername\.ssh

If you see files like id_rsa and id_rsa.pub, you already have an SSH key pair. You can use these or generate new ones.

Step 2: Generate a New SSH Key

If you don’t have an existing key or want to generate a new one:

  1. Run the following command and replace [email protected] with your GitHub email address:
ssh-keygen -t ed25519 -C "[email protected]"

If you are using an older system that doesn’t support Ed25519, use RSA instead:

ssh-keygen -t ed25519 -C "[email protected]"

When prompted to “Enter a file in which to save the key,” you can press Enter to accept the default file location (C:\Users\YourUsername\.ssh\id_ed25519).

Next, you’ll be asked to enter a passphrase. It’s recommended to use a passphrase for added security, but you can leave it empty if you prefer. If the command runs successfully a key fingerprint and randomart image will be displayed.

Step 3: Add Your SSH Key to the SSH Agent

To use your SSH key, you need to add it to the SSH agent. Luckily windows has a simple command to start your ssh agent and it will automatically add your identity when the command is entered:

  1. Start the SSH agent and add your identity:
start-ssh-agent

Step 4: Add the SSH Key to Your GitHub Account

Now, you need to add the SSH key to your GitHub account:

Display the public key for your SSH credentials:

    type C:\Users\YourUserName\.ssh\id_ed25519.pub

    A public key will have a format similar to the following:

    ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPBxjhTHvpU7HeQQ3hmmUYOMNAtCn1sfoAZL8GZ9SJpO your_email@example.com
    1. Go to GitHub.com and log in.
    2. In the upper-right corner, click your profile photo, then click Settings.
    3. In the user settings sidebar, click SSH and GPG keys.
    4. Click New SSH key or Add SSH key.
    5. In the “Title” field, add a descriptive label for the new key. For example, “My laptop key”.
    6. Paste your key into the “Key” field.
    7. Click Add SSH key.
    8. If prompted, confirm your GitHub password.

    Step 5: Configure Known Hosts

    You will need to add github to your known hosts file if it doesn’t already exist.

    ssh-keyscan github.com >> C:\Users\YourUserName\.ssh\known_hosts

    Step 6: Test the SSH Connection

    To make sure everything is set up correctly, test the connection:

    1. Open a terminal and run the following command:
    ssh -T git@github.com

    Step 7: Test cloning a repository on your local machine

    Finally, make sure your Git repository is using the SSH URL instead of HTTPS:

    1. If you are cloning a new repository, use the SSH URL:
    git clone git@github.com:username/repository.git

    Summary

    Congratulations! You have securely generated a set of SSH keys and configured your local git to use them for authentication.

    Index