GitHub Error Message - Permission denied (publickey)


Anybody seen this error and know what to do?

I'm using the terminal, I'm in the root, the GitHub repository exists and I don't know what to do now.

> git push -u origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
9/9/2016 3:59:04 AM

Accepted Answer

GitHub isn't able to authenticate you. So, either you aren't setup with an SSH key, because you haven't set one up on your machine, or your key isn't associated with your GitHub account.

You can also use the HTTPS URL instead of the SSH/git URL to avoid having to deal with SSH keys. This is GitHub's recommended method.

Further, GitHub has a help page specifically for that error message, and explains in more detail everything you could check.

10/17/2012 6:15:18 PM

Did you create a config file in your ~/.ssh directory? It should have contents like these:

 IdentityFile ~/.ssh/github_rsa

Assuming that you created an ssh key named github_rsa

and uploaded it to GitHub...

NOTE: You must follow this way of explicit configuration if you have more than 1 key (2 and more) in your ~/.ssh/ directory. If you don't specify key this way, then first key in order is taken and used for github authentication, so it depends on the key file name then.


I know about this problem. After add ssh key, add you ssh key to ssh agent too (from official docs)

ssh-agent -s
ssh-add ~/.ssh/id_rsa

After it all work fine, git can view proper key, before couldn't.


You need to generate an SSH key (if you don't have one) and associate the public key with your Github account. See Github's own documentation.


This happened to me. For some reason my origin got messed up without my realizing it:

Check if your settings are still correct

git remote -v

the url needs to be something like ssh://[email protected]/YourDirectory/YourProject.git; if you don't see [email protected], use

git remote set-url origin git://

to set it right. Or you could use the github app to check and set the Primary Remote Repository url in the settings panel of your particular repository.


Assuming you are connecting GitHub over SSH, you can run below command to confirm this.

$git config --get remote.origin.url

If you get a result has following format [email protected]:xxx/, then you should do the following.

Generate a SSH key(or use existing one). if you had one, you just need to add your key to the ssh-agent (step 2)and to your GitHub account(step 3).

below are for those who don't have SSH key.

Step 1 Generating public/private rsa key pair.

$ssh-keygen -t rsa -b 4096 -C "[email protected]"

You'll be asked to confirm where to save the SSH key and what passphrase you want to use.

Step 2 Add your key to the ssh-agent

  • Ensure ssh-agent is enabled

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

  • Add your SSH key to the ssh-agent:

    $ssh-add ~/.ssh/id_rsa

Step 3 Add your SSH key to your account

$sudo apt-get install xclip

$xclip -sel clip < ~/.ssh/

Then add the copied key to GitHub

Go to Settings->SSH keys(Personal settings side bar)->Add SSH key->fill out form(key is on your clipboard, just use ctrl+v)->Add key

After going through above steps, you should solve the permission problem.

Reference Link: Generating SSH keys.