DEV Community

John  Ajera
John Ajera

Posted on

How to Configure SSH for GitHub Authentication on Windows

How to Configure SSH for GitHub Authentication on Windows

Secure Shell (SSH) authentication is the recommended way to securely connect to GitHub repositories without needing to enter your credentials every time. In this guide, you'll learn how to configure SSH authentication for GitHub on Windows step by step.

Step 1: Check for Existing SSH Keys

Before generating a new SSH key, check if you already have one. Open PowerShell and run:

ls ~\.ssh
Enter fullscreen mode Exit fullscreen mode

If you see files like id_rsa and id_rsa.pub, you already have an SSH key. You can use the existing key or generate a new one.

Step 2: Generate a New SSH Key

If you don’t have an SSH key or want a new one, generate it using:

ssh-keygen -o -a 100 -t ed25519 -f $env:USERPROFILE\.ssh\id_ed25519_github -C "ssh key for github"
Enter fullscreen mode Exit fullscreen mode

If your system doesn’t support ed25519, use:

ssh-keygen -o -a 100 -t rsa -b 4096 -m PEM -f $env:USERPROFILE\.ssh\id_rsa_github -C "ssh key for github"
Enter fullscreen mode Exit fullscreen mode

After running the command, you will be prompted to enter a strong passphrase. Type your passphrase, press Enter, then confirm it when prompted to enhance security.

Step 3: Add Your SSH Key to the SSH Agent

Start the SSH agent by running the following command in PowerShell:

Start-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode

Then add your key:

ssh-add $env:USERPROFILE\.ssh\id_ed25519_github
Enter fullscreen mode Exit fullscreen mode

If you used RSA, replace id_ed25519_github with id_rsa_github.

Step 4: Persist SSH Key Across Sessions

To ensure your SSH key is always loaded when you open a new PowerShell session, you need to configure the SSH settings.

Configure SSH to Automatically Add the Key

Edit (or create) your SSH config file:

notepad $env:USERPROFILE\.ssh\config
Enter fullscreen mode Exit fullscreen mode

Add the following lines:

Host github.com
  IdentityFile ~/.ssh/id_ed25519_github
  AddKeysToAgent yes
Enter fullscreen mode Exit fullscreen mode

This ensures that your SSH key is automatically loaded by the SSH agent when needed.

Alternative: Load the SSH Key in PowerShell Profile

If you want to explicitly load the SSH key every time a new PowerShell session starts, add the following command to your PowerShell profile:

notepad $PROFILE
Enter fullscreen mode Exit fullscreen mode

Add this line:

Start-Service ssh-agent; ssh-add $env:USERPROFILE\.ssh\id_ed25519_github
Enter fullscreen mode Exit fullscreen mode

Save the file and restart PowerShell.

Step 5: Add the SSH Key to Your GitHub Account

type $env:USERPROFILE\.ssh\id_ed25519_github.pub
Enter fullscreen mode Exit fullscreen mode

If you used RSA, replace id_ed25519_github.pub with id_rsa_github.pub.

Now, go to GitHub > Settings > SSH and GPG Keys and click New SSH Key. Paste the copied key and save it.

Step 6: Test the SSH Connection

Verify that your SSH key is correctly added by running:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If prompted with:

This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Enter fullscreen mode Exit fullscreen mode

Type yes to proceed.

You should see a message like:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Step 7: Configure Git to Use SSH (Optional)

If you're still using HTTPS for Git operations, switch to SSH:

git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Conclusion

You've successfully configured SSH authentication for GitHub on Windows! This setup allows you to securely connect to your repositories without entering your password repeatedly. Happy coding!

If you encounter any issues, check GitHub’s SSH troubleshooting guide.

Top comments (0)