How to Resolve 'Permission Denied (publickey)' Errors with GitHub: A Comprehensive Guide for Mac, Linux, and Windows

Photo by Joan Gamell on Unsplash

How to Resolve 'Permission Denied (publickey)' Errors with GitHub: A Comprehensive Guide for Mac, Linux, and Windows

ยท

2 min read

Problem Statement

SSH authentication failures when interacting with Github repos can be frustrating. Which is something I recently encountered and had to solve. So hopefully this will be of help to someone .This article aims to provide a step-by-step guide to diagnose and solve this issue across Mac, Linux, and Windows platforms.

Introduction

SSH is a secure way to interact with repositories on GitHub. However, setting it up might sometimes result in the infamous 'Permission denied (publickey)' error. This article aims to be a one-stop solution for resolving this issue.

Solutions

For Mac Users

Generating New SSH Key

  1. Open Terminal.

  2. Run ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Adding SSH Key to SSH Agent

  1. Run eval "$(ssh-agent -s)"

  2. Run ssh-add ~/.ssh/id_rsa

Adding SSH Key to GitHub Account

  1. Copy the SSH key: pbcopy < ~/.ssh/id_rsa.pub

  2. Paste it in GitHub -> Settings -> SSH Keys

For Linux Users

The steps are largely similar to Mac, with the key difference being in copying the SSH key to the clipboard. You can use xclip for that purpose:

  1. Install xclip: sudo apt install xclip

  2. Run xclip -sel clip < ~/.ssh/id_rsa.pub

For Windows Users

Windows users can use Git Bash to generate SSH keys and then add them to the SSH agent.

  1. Open Git Bash.

  2. Generate an SSH key: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

To add the key to the SSH agent, you can use:

// Start the ssh-agent in the background
eval $(ssh-agent -s)

// Add your SSH key to the ssh-agent
ssh-add ~/.ssh/id_rsa

Conclusion

Encountering 'Permission denied (publickey)' errors while working with GitHub can disrupt your workflow. However, with the guidelines provided in this article for Mac, Linux, and Windows users, you can resolve this issue and get back to coding in no time!

ย