How to fix the issue SSH key Permission denied (publickey) in git?-2023

Indeed, I can help you with that! The “Permission denied (publickey)” error usually occurs when trying to access a GitHub repository using SSH and your SSH key isn’t recognized or authorized. So here I will explain to you to fix this issue step-by-step.

Table of Contents

Step1. Check Existing SSH Keys:

First, check if your system has an SSH key pair. Open a terminal(or Gitbash) and enter the below command and look for files named id_rsa and id_rsa.pub or similar.

				
					cd .ssh/
ls -al
				
			

Step2. Generate a New SSH Key:

If you don’t have an SSH key pair or want to create a new one, then generate it with the following command:

				
					ssh-keygen -t rsa -b 4096 -C "your_email_id"
				
			

NOTE:- Replace “your_email_id” with the email associated with your GitHub account. Press Enter to accept the default file location and an optional passphrase.

Step3. Add the SSH Key to the SSH Agent:

Add your private key to the agent with the help of below commands-

				
					eval "$(ssh-agent -s)"
eval "$(ssh-agent -s)"
				
			

Step4. Copy the Public Key to GitHub:

Display your public key with the help of the below command

				
					cat ~/.ssh/id_rsa.pub
				
			

Step5. Add SSH Key to GitHub:

  • Log in to your GitHub account.
  • Go to Settings > SSH and GPG keys.
  • Click on New SSH key.
  • Add a title (e.g., “My SSH Key”) and paste the copied key into the Key field.
  • Then Click on  Add SSH key.

Step6. Test the Connection:

To test if your SSH key is working, please run below command

				
					ssh -T git@github.com
				
			

You might see a message like: “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”

Step7. Update Remote URLs:

  • If you’re still facing issues, make sure your repository’s remote URL is using SSH.
  • To change it, navigate to your repository folder(which means inside your local repository folder here my local folder is “githubrepo”) and run:
				
					git remote set-url origin git@github.com:username/repository.git
				
			

Replace username with your GitHub username and repository with the repository name.

That should resolve the “Permission denied (publickey)” error. And now you are able to push your code into GitHub.

If you continue to experience issues, double-check each step, ensure your key is correctly added to GitHub, and consider reaching out to GitHub support or consulting their documentation for further assistance.

Video:-

Still you have any doubt in any steps, please go through this  video again

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top