How to fix - git@github.com permission denied (publickey). fatal could not read from remote repository



git@github.com: Permission denied (public key).fatal: Could not read from remote repository. - It means GitHub is rejecting your connection because -

  1. It is your private repo
  2. GitHub does not trust your computer because it does not have the public key of your computer.

And when you try to clone the repo you get the following error message -

1git@github.com: Permission denied (public key).
2fatal: Could not read from remote repository.
3
4Please make sure you have the correct access rights
5and the repository exists.

Not only github but you can face this issue on the platform like GitLab, DigitalOcean, here I have composed the list of possible solutions -

  1. GitHub - How to fix git@github.com: Permission denied (public key)?
  2. GitLab - How to fix gitlab.com: permission denied (publickey) fatal: Could not read from remote repository
  3. BitBucket - Permission denied (public key), can’t clone/clone to bitbucket



1. GitHUb - How to fix this issue?

Step 1 - Create SSH key pair

One of the easiest ways for you to generate a key pair is by running ssh-keygen utility.

Open the command prompt and type in the following

1ssh-keygen

(Note - If you do not have ssh-keygen installed on the window then Click here.)

To keep the ssh-keygen simple, do not enter any key name or passphrase.

 1Generating public/private rsa key pair.
 2Enter file in which to save the key (/Users/rahulwagh/.ssh/id_rsa):
 3Enter passphrase (empty for no passphrase): 
 4Enter same passphrase again: 
 5Your identification has been saved in /Users/rahulwagh/.ssh/id_rsa.
 6Your public key has been saved in /Users/rahulwagh/.ssh/id_rsa.pub.
 7The key fingerprint is:
 8SHA256:Okq3w+SesCGLQVToSBQru8RdUZtT2EIIrzH5MQ67DWA rahulwagh@local
 9The key's randomart image is:
10+---[RSA 3072]----+
11|.ooo..+oo.       |
12| oo o..o+.       |
13|=E = = +.        |
14|*oo X o .        |
15|.+ = o  S        |
16|o.  + ..         |
17|o ..+=+          |
18| o + *++         |
19|. . o.+.         |
20+----[SHA256]-----+

Where to find the key pair

The file will be generated at - /Users/rahulwagh/.ssh/

Name of the file - id_rsa.pub

(To generate SSH keys on Windows machine click here)


Step 2 - Adding SSH key to your GitHub account

  1. Goto your GitHub Account -> Settings

  1. Then look for SSH and GPG keys under **Account Settings -> SSH and GPG keys **

  1. After that click on New SSH Key. Assign some meaningful name to your key

  1. To get the key goto to your command prompt and switch directory path

Windows - C:\Users\rahulwagh.ssh\id_rsa.pub

Linux - /Users/rahulwagh/.ssh/id_rsa.pub

Run the following command

1cat id_rsa.pub

  1. Copy the content of the key

  2. Paste the key inside your GitHub account

Now you can clone your GitHub repo and it will not complain about git@github.com permission denied (publickey). fatal could not read from remote repository


Benefits of this approach
  1. You do not need to supply you GitHub Username and password
  2. More secured

Drawback
  1. The process of generating SSH keys is little troublesome.
  2. If you switch your laptop/desktop then you need re-generate the SSH keys again and perform the same steps onto your new laptop/desktop.


Approach 2 : Using GitHub HTTPS urls

This is one more approach in which you do not need to worry about SSH key generation.

You need following -

  1. GitHub repo URL with HTTPS (Ex - https://github.com/rahulwagh/example.git)
  2. Your GitHub Username
  3. Your GitHub Password

Run the following command to set git remote

1git clone origin https://github.com/rahulwagh/example.git 

After that it will ask for your username and password, supply your username and password.

Once you are authenticated then you can perform your Git operations.


Benefits of this approach
  1. You do not need to generate SSH keys
  2. You do not need to copy your SSH keys to GitHub accounts
  3. This approach can be used from any Laptop, Desktop. You only need to provide your GitHub credentials with this approach.

Drawback
  1. The only drawback which I can see is you need to input your GitHub credentials everytime.


2. GitLab: How to fix gitlab.com: permission denied (publickey) fatal: Could not read from remote repository

2.1 Multiple public keys inside ~/.ssh directory?

When you are working with multiple github repositories then there is high probability that you might have incorrect public key mentioned inside your ~/.ssh directory.

For example I have generated the public key for my GitLab project by the name ~/.ssh/id_rsa.gitlab.pub

But when I checked my ~/.ssh/config file I noticed the incorrect name of public key for the gitlab.com

Here is the content of my ~/.ssh/config file where the public key name is ~/.ssh/id_rsa instead of ~/.ssh/id_rsa.gitlab.pub

1Host gitlab.com
2      Preferredauthentications publickey
3      IdentityFile ~/.ssh/id_rsa 

How to fix?

To fix this issue you should correct the public key name -

1Host gitlab.com
2      Preferredauthentications publickey
3      IdentityFile ~/.ssh/id_rsa.gitlab.pub


3. Bitbucket - Permission denied (public key), can’t clone/clone to bitbucket

There could be multiple reasons behind the issue -


3.1 You did not add your local SSH keys to BitBucket Settings

SSH keys could be one of the most probable reasons which are causing the issue while working with your BitBucket repository.

You might see the following errors -

1Permission denied (publickey).

or

1abort: Permission denied: 

or (In case you are working with mercury then you might see this error message)

1remote: Permission denied (publickey).
2abort: no suitable response from remote hg! 


How to fix?

Before we start debugging the issue, let’s first debug the issue to know the exact reason behind this error.

Run the following command from your terminal (put your correct repository name) -

1ssh -T your_bitbucket_repo@domain.com
2

The above command should return you with Username and in case you did not see your username then you need to Create SSH keys and add to your BitBucket account.

Step 1 - Create your Public SSH Keys

Step 2 - Goto Your Account -> Manage Account -> SSH Keys

Step 3 - Click on Add Key

Step 4 - Paste your key which you have generated in Step 1.

Step 5 - Finally click on Add Key

It should work after adding the SSH keys to your BitBucket account.

(Note - Here are some official links from bitbucket if you are interested more - click here)



3.2 Permission on your bitbucket directory

There could also be a possibility that you do not have permission to access the BitBucket repository. Check with your bitbucket administrator with regards to your permission.

If it is the case of permission then your bitbucket administrator could grant the permission.

1.1 Generate ssh-keygen on Windows 10

If you are working on a windows machine then you need to install the OpenSSH.

Goto: Apps > Apps and Features > Manage Optional Features

Verify the list of the apps, if it already installed. If not then go at the top of the page then select Add a Feature afterwords -

  • OpenSSH Client : Locate OpenSSH Client, then click Install.
  • OpenSSH Server : Locate OpenSSH Server, then click Install.


Install OpenSSH using PowerShell on Windows 10

If you like using Powershell then first make sure OpenSSH features are available to install -

Copy and run the following command

1Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
2
3# This should return the following output:
4
5Name  : OpenSSH.Client~~~~0.0.1.0
6State : NotPresent
7Name  : OpenSSH.Server~~~~0.0.1.0
8State : NotPresent

After that install server and client features using the following command -

 1# Install the OpenSSH Client
 2Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
 3
 4# Install the OpenSSH Server
 5Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
 6
 7# Both of these should return the following output:
 8`
 9Path          :
10Online        : True
11RestartNeeded : False

1.2 Verify OpenSSH client is installed
  1. Goto settings panel

  1. Click on Optional Features and then look for OpenSSH client, if you can find the OpenSSH client which means you have installed it correctly.

1.3 Generate ssh keys on windows 10
  1. Next we need to run the ssh-keygen and for that goto window’s command prompt and open it in administrator mode.

  1. In the command prompt type the following command
1ssh-keygen
 1Generating public/private rsa key pair.
 2Enter file in which to save the key (C:\Users\rahulwagh/.ssh/id_rsa):
 3Enter passphrase (empty for no passphrase): 
 4Enter same passphrase again: 
 5Your identification has been saved in C:\Users\rahulwagh/.ssh/id_rsa.
 6Your public key has been saved in C:\Users\rahulwagh/.ssh/id_rsa.pub.
 7The key fingerprint is:
 8SHA256:Okq3w+SesCGLQVToSBQru8RdUZtT2EIIrzH5MQ67DWA rahulwagh@local
 9The key's randomart image is:
10+---[RSA 3072]----+
11|.ooo..+oo.       |
12| oo o..o+.       |
13|=E = = +.        |
14|*oo X o .        |
15|.+ = o  S        |
16|o.  + ..         |
17|o ..+=+          |
18| o + *++         |
19|. . o.+.         |
20+----[SHA256]-----+
21 


1.4 Where to find the key pair

The file will be generated at - C:\Users\rahulwagh.ssh\id_rsa

Name of the file - id_rsa

Anchor-17-feb-2021