ChatGPT解决这个技术问题 Extra ChatGPT

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.
Could be permission issues with .ssh and authoirzed keys. Ssh is pretty strict, google it.
like @bdukes' answer here is the command using https and it will work, Windows example: "PS C:\xampp\htdocs> git clone github.com/algolia/instant-search-demo.git"

b
bdukes

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.


This error is not exclusive to GitHub. I am getting the same error with BitBucket, and I'm scratching my head as to how to resolve it...
The helping part is using ssh -vT git@github.com from the Github help page to help debug what key your repo is using. I suggest you add it in your answer.
The upvote of using HTTPS instead of SSL is a bit disappointing. SSH is far more convenient. The suggestion from @MaximeBernard above solved the issue for me: GIT was not looking at the right place for the .ssh directory. Using ssh -vT git@github.com provides a lot of information as to why it is not working.
per my comment above, a third option is that the command is being sent from an account other than the one with the ssh keys (e.g. running as root).
As @theapache64 mentioned: If you're on a Mac and have already generated an ssh key "ssh-add" may indeed be what you're missing. Worked for me.
S
SwissCodeMen

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.


Prefect! Worked on my MacOS X.
Could not open a connection to your authentication agent.
try to run this: eval ssh-agent -s
@MonaJalal use ssh-add ~/.ssh/github_rsa
actually it worked well with me, but after restart my computer I found the same issue return with me, so do you want me to doing it with every time I need to deploy my project on the server ?!
k
kensai

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

Host github.com 
 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 had generated the ssh-key and was able to work well, for quite some time, with no issues. One fine day (probably after restart), it stopped working, whereas the key was intact at github and on my local system. I added the "config file", specifying the key-mapped-to-domain. Details provided above in igor-ganapolsky-answer. It works fine !
This indeed solves my problem! Many thanks! But I have a question. Before using your method, ssh -T git@github.com responses correctly, while git push -u origin master gives the same error as in the question. Why I can connect to it correctly but cannot push to it or fetch from it?
@EdenHarder I am facing the same issue. Did you find an answer to it?
EVERY tutorial and note I found on the topic assumed id_rsa and id_rsa.pub like it's the first and only SSH key. Thank you for this.
It worked for me ! I had 2 set of keys on my system, and this solution resolved my issue. However it's better explained here
c
cdhowie

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.


Thanks for this...I got an SSH key now generated from GitHub. What command is used to associate the two in the terminal now? Thanks!
There is no terminal command for that. See step 4 in the documentation that I linked in my answer.
I cloned the repository by using HTTPS. Do I need to generate SSH key in this case?
@PabitraDash No. HTTPS does not use SSH keys.
Life saver answer!
r
rmundo

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://git@github.com/YourDirectory/YourProject.git; if you don't see git@github.com, use

git remote set-url origin git://github.com/YourDirectory/YourProject.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.


Be careful with the urls. They differ between https and ssh and the posts on this page don't make it that clear. Each git project has a .git/config file in the project root directory. There you can set the remote and branch information. Setting the remote for https: url = github.com/<yourGitUserName>/<yourGitProject>.git While for git+ssh: url = git@github.com:<yourGitUserName>/<yourGitProject>.git Having the wrong url causes the public-key permission denied error which is probably not a very clear error.
for my case in the final just works with in the part: git remote set-url origin github.com/your_directory/your_project.git
g
george mano

Issue solved if you change the ssh access to https access to the remote repository:

git remote set-url origin https_link_to_repository

git push -u origin master

i get "fatal: No such remote 'origin'" for this
x
xinerd

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 git@github.com:xxx/xxx.github.com.git, 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 "your_email@example.com"

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/id_rsa.pub

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.


This solution worked on ubuntu, $ssh-add /root/.ssh/id_rsa
This resolved a weird messup on my enviornment too, one modification I would suggest is to avoid the xclip dependancy, just use cat ~/.ssh/id_rsa.pub
B
Badr Bellaj

Another solution :

create the SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". This will create both id_rsa and id_rsa.pub files.

Add the id_rsa to ssh list on local computer: ssh-add ~/.ssh/id_rsa.

After generating the keys get the pubkey using :

cat ~/.ssh/id_rsa.pub 

you will get something like :

cat ~/.ssh/id_rsa.pub 

ssh-rsa AAAB3NzaC1yc2EAAAADAQABAAACAQCvMzmFEUPvaA1AFEBH6zGIF3N6pVE2SJv9V1MHgEwk4C7xovdk7Lr4LDoqEcqxgeJftwWQWWVrWWf7q9qCdHTAanH2Q5vx5nZjLB+B7saksehVOPWDR/MOSpVcr5bwIjf8dc8u5S8h24uBlguGkX+4lFJ+zwhiuwJlhykMvs5py1gD2hy+hvOs1Y17JPWhVVesGV3tlmtbfVolEiv9KShgkk3Hq56fyl+QmPzX1jya4TIC3k55FTzwRWBd+IpblbrGlrIBS6hvpHQpgUs47nSHLEHTn0Xmn6Q== user@email.com

copy this key (value) and go to github.com and under the setting (ssh and pgp key) add your public key.


d
d1jhoni1b

First, we need to check for existing ssh keys on your computer. Open up Terminal and run:

ls -al ~/.ssh

#or

cd ~/.ssh
ls

and that will lists the files in your .ssh directory

And finally depending on what you see (in my case was):

 github_rsa  github_rsa.pub known_hosts

Just try setting up your RSA and hopefully that will solve your "git push origin" issues

$ ssh-keygen -lf ~/.ssh/github_rsa.pub

NOTE: RSA certificates are keys-paired so you will have a private and a public certificate, private will not be accessible for you since it belongs to github (in this case) but the public is the one you might be missing when this error happens (at least that was my case, my github account or repo got messed up somehow and i had to "link" the public key, previously generated)


Do I have to copy the SHA output to github from this command?
No, just execute "ssh-keygen -lf ~/.ssh/"
that's why i also attached a command that "Lists the files in your .ssh directory"... so that way you know from which file to extract your key (ssh-keygen). Remember RSA certificates are keys-paired so you will have a private and a public certificate, private will not be accessible for you since it belongs to github (in this case) but the public is the one you might be missing when this error happens (at least that was my case, my github account or repo got messed up somehow and i had to "link" the public key, previously generated, again)
$ ssh-keygen -lf ~/.ssh/github_rsa.pub /home/mona/.ssh/github_rsa.pub: No such file or directory
"No such file or directory" means you dont have that file name or path... what do you get when when execute ls -al ~/.ssh?
m
marcdahan

this worked for me:

1- remove all origins

git remote rm origin  

(cf. https://www.kernel.org/pub/software/scm/git/docs/git-remote.html)

*remote : "Manage the set of repositories ("remotes") whose branches you track.

*rm : "Remove the remote named . All remote-tracking branches and configuration settings for the remote are removed."

2- check all has been removed :

git remote -v  

3- add new origin master

git remote add origin git@github.com:YOUR-GIT/YOUR-REPO.git

that's all folks!


That was helpful.By these commands it start asking me for github username and account password, after that I can easily push to github link by type in terminal: git push -u origin master
j
jfunk

I was getting this error. Turns out I had just upgraded OSX to Sierra and my old key was no longer registered.

At first I thought it was "Upgrading to macOS Sierra will break your SSH keys and lock you out of your own servers"

But I had sidestepped that one. Turns out I just had to re-register my existing key:

ssh-add -K

And type the passphrase... done!


same issue, worked for me: ` ssh-add -K ~/.ssh/id_rsa `
r
roman

I think i have the best answer for you, your git apps read your id_rsa.pub in root user directory

/home/root/.ssh/id_rsa.pub

That's why your key in /home/your_username/.ssh/id_rsa.pub can't be read by git. So you need to create the key in /home/root/.ssh/

$ sudo su
$ ssh-keygen
$ cd ~/.ssh
$ cat id_rsa.pub

Then copy the key in your github account. It's worked for me. You can try it.


This let me to the answer I was looking for. I ran clone with sudo from habit, so it was looking for the wrong credentials. Took off the sudo and my clone ran fine.
N
N. Osil

In case you are not accessing your own repository, or cloning inside a cloned repository (using some "git submodule... " commands):

In the home directory of your repository:

$ ls -a

1. Open ".gitmodules", and you will find something like this:

[submodule "XXX"]
    path = XXX
    url = git@github.com:YYY/XXX.git

Change the last line to be the HTTPS of the repository you need to pull:

[submodule "XXX"]
    path = XXX
    https://github.com/YYY/XXX.git

Save ".gitmodules", and run the command for submodules, and ".git" will be updated.

2. Open ".git", go to "config" file, and you will find something like this:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/YYY/XXX.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[submodule "XXX"]
    url = git@github.com:YYY/XXX.git

Change the last line to be the HTTPS of the repository you need to pull:

    url = https://github.com/YYY/XXX.git

So, in this case, the main problem is simply with the url. HTTPS of any repository can be found now on top of the repository page.


R
Rose Perrone

Make sure ssh-add -l shows a fingerprint of an SSH key that's present in the list of SSH keys in your Github account.

If the output is empty, but you know you have a private SSH key that works with your github account, run ssh-add on this key (found in ~/.ssh. It's named id_rsa by default, so you'll likely run ssh-add id_rsa).

Else, follow these instructions to generate an SSH key pair .


T
T J

I was using github earlier for one of my php project. While using github, I was using ssh instead of https. I had my machine set up like that and every time I used to commit and push the code, it would ask me my rsa key password.

After some days, I stopped working on the php project and forgot my rsa password. Recently, I started working on a java project and moved to bitbucket. Since, I had forgotten the password and there is no way to recover it I guess, I decided to use the https(recommended) protocol for the new project and got the same error asked in the question.

How I solved it?

Ran this command to tell my git to use https instead of ssh: git config --global url."https://".insteadOf git:// Remove any remote if any git remote rm origin Redo everything from git init to git push and it works!

PS: I also un-installed ssh from my machine during the debug process thinking that, removing it will fix the problem. Yes I know!! :)


I don't think it's a good idea to point people toward https, when also ssh is supposed to work. Furthermore, this is unlikely to work for people behind a corporate proxy.
D
Damika

Yes I too had this question :/ I was going to push my project to Github in HTTP type(not in SSH type). I had to enter my username and password in each push. So first I entered code relevant to below type

git remote add origin git@github.com:YOUR-GIT/YOUR-REPO.git

and I got

git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.

So however I solved my problems by doing below methods

git remote rm origin remove your git remote repository git remote now check whether your remote remote repository have been removed

URL = https://github.com//.git

git remote add origin URL Again add your URL to make remote repository git push -u origin master You can push your content to remote repository. In here as you use "-u" with the command you make a tracking branch and with the help of tracking branch in the remote repository you won't to tell git what branch you push in the next steps :) And here if you use linux git will ask username and password before push the content. And give your relevant credentials.

For prevent to give username and password in each push you have to change git config

For list your git config - git config --global --list you will see

user.name=<your_username>
user.email=<your_email>

So you have to add credential.helper attribute to your git config to this

5.git config --global --add credential.helper store add this to your terminal Now you can add new item to your files then git add., git commit -m "<message>", git push
And now too there git will request your username and password and that will be the last time. And for next git pushes git won't request username and password from you :)


super thanks worked for me
x
xproph

OK there are few solutions to this one, some of them might already been mentioned but just to keep them together:

make sure you keys are present, by default another ~/.ssh/ folder, i.e. id.rsa and id.rsa.pub

make sure the keys have correct permissions, you can run chmod: chmod 600 ~/.ssh/id_rsa chmod 644 ~/.ssh/id_rsa.pub

make sure the content of you public key (id_rsa.pub) matches the one uploaded in the remote repository configuration

Finally fix the problems with ssh agent: ssh-add

Some more info: https://itcodehub.blogspot.com/2015/01/ssh-add-problems-with-ssh-agent-and.html


I had copied my ssh key from one computer to another using a USB drive formatted as FAT32. That changed the permissions of the files. On Ubuntu, I’m used to automatically being asked for the SSH password when needed, but that just silently did not happen until I ran chmod like you describe. Thanks.
A
Abhishek Tomar

Solutin for error like this

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

Solution (OSX)

Open your terminal and follow below commands

$ cd ~

$ sudo su

$ ssh-keygen Enter file in which to save the key (/var/root/.ssh/id_rsa): $ id_rsa Enter passphrase (empty for no passphrase): $ hit enter Enter same passphrase again: $ hit enter

Enter file in which to save the key (/var/root/.ssh/id_rsa): $ id_rsa

Enter passphrase (empty for no passphrase): $ hit enter

Enter same passphrase again: $ hit enter

$ cd ~/.ssh

$ cat id_rsa.pub

copy display content.

Open GitHub click your profile icon settings>SSH and GPC Keys

Click on the new ssh key button

enter any title and key that you copied

check now your issue is resolved


It asks sudo password before pushing to remote. How do we remove that?
@rohetoric - Not sure try with "allow write access" property in the GitHub repo.
Solved my access denied problem and can use git without sudo. Thanks a lot!
p
prosti

Allow write access for the key (identity) and then click Add key

https://i.stack.imgur.com/4LMTP.png

If on Windows check for more details in Using Github via SSH.


Where was this screenshot taken?
u
user7506706

I had the same issue recently. This might help if you need a fix immediately, but this needs to be done every time you re-start your system

From terminal, run : ssh-add ~/.ssh/id_rsa

Enter your system password and that should work.


i
imflash217

I would like to add some of my findings:

If you are using GitBash, then make sure the SSH key is stored in ~/.ssh/id_rsa.

By Default GitBash searches for ~/.ssh/id_rsaas default path for SSH key.

Even the file name id_rsa matters. If you save your SSH key in another filename or path, it will throw the Permission Denied(publickey)error.


K
Kevin

If you have already created an SSH key and are still getting the error it is because you need to give the user permissions to read and write to the folder you are cloning into. To do this, sudo chmod 777 <your_folder_name_here>". Of course, this is after you have generated an SSH key and you are still getting this error. Hope this helps future users.

Edit

To add on to this use admin in Windows if you're using the git bash


This. This can also cause the problem.
This was my problem, but I wouldn't recommend setting 777 permissions on a folder. In my case the folder was owned by the root user, and I needed to give it access to my non-root user account. I did this like so: sudo chown -R $USER:$USER <your_folder_name_here>
@David, I also recommend that as well. 777 is pretty hacky and not secure.
H
Han Zhang

TLDR:

make sure you have write access to the repo (configure it from the repo's settings). make sure the public key is in the SSH and GPG keys of your github account.

For me, this error usually occurs when I try to clone some repo from a newly installed machine. When receiving a request, github will first check the public key hash. If the public key does not match any user, github will reject this request. This case is common if the machine is new and your ssh key is newly generated.


Can you be more specific on point 1?
If the repo belongs to you, you will always be able to write to the repo. If the repo does not belong to you, ask the owner to add you to the collaborators.
If you are trying to clone a public repo, it is highly likely that your SSH key is not added to your github account.
My question was meant to ask where exactly in the repo’s settings write access is granted.
v
venkat razesh

you can use Https url to login

i guess you are trying to login with ssh url when you say git push if it as asking only password consider you are connecting through ssh.better you use http url.


N
Nabin

Also in ubuntu, even though there was already SSH key entered in settings in BitBucket, I got this problem. The reason was, I was trying the following:

sudo git push origin master

Not sure why, but it got solved by using

git push origin master

No sudo used.


J
Joomler

For me I tried this -

eval "$(ssh-agent -s)"

then I run

ssh-add ~/.ssh/path-to-the-keyfile

and for generating the key you can run

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

this will generate the pair of keys (Public and private).

you can store this key to github for more read this Adding a new SSH key to your GitHub account

I hope it will help others :)


j
justin

I was having a similar problem to @Batman. However, because I was running this under /usr/local/src/projectname, running without sudo was not an option.

Just add the -E flag to preseve the environment (your ~/.ssh/ path).

$ sudo -E git clone git@your_repo

From man sudo:

-E, --preserve-env Indicates to the security policy that the user wishes to pre‐ serve their existing environment variables. The security policy may return an error if the user does not have permis‐ sion to preserve the environment.


T
Timur Shtatland

I found this page while searching for a solution to a similar error message using git pull on a remote host:

$ git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I was connected from my local machine to the remote host via ssh -AY remote_hostname. This is not a solution to OP's question, but useful for others who come across this page, so posting it here.

Note that in my case, git pull works fine on my local machine (that is, ssh key had been set up, and added to the GitHub account, etc). I solved my issue by adding this to ~/.ssh/config on my laptop:

Host *
     ForwardAgent yes

I then re-connected to the remote host with ssh -AY remote_hostname, and git pull now worked. The change in the config enables to forward my ssh keypair from my local machine to any host. The -A option to ssh actually forwards it in that ssh session. See more details here.


c
coding360

https://i.stack.imgur.com/jLDVs.png

Open terminal as administrator run this command: "ssh-keygen". It generate a ssh key and will show the folder where it has been created. Check my image copy the generated "ssh key" go to your github profile ---> settings --> Click SSH and GPH --> Click ""New SSH Key Button" and paste the "ssh key" and finally "clickthe add Button"


Awesome, I tried to find many solution, but this is straight forward and worked in one go.
j
juliarm

If you are using the GitHub for Mac UI, check preferences to make sure you're logged in.