1077

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.
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
webwrks
  • 11,158
  • 5
  • 24
  • 21
  • 11
    https://help.github.com/articles/generating-ssh-keys – Muhammad Umer May 26 '14 at 15:19
  • 2
    Similar question: http://stackoverflow.com/questions/16074832/cannot-push-to-git-repository-on-bitbucket – JMoran Jan 24 '16 at 07:50
  • 1
    Could be permission issues with .ssh and authoirzed keys. Ssh is pretty strict, google it. – basickarl Sep 16 '16 at 14:28
  • Possible duplicate of [Cannot push to git repository on bitbucket](https://stackoverflow.com/questions/16074832/cannot-push-to-git-repository-on-bitbucket) – james.garriss Jun 19 '17 at 11:40
  • like @bdukes' answer here is the command using https and it will work, Windows example: "PS C:\xampp\htdocs> git clone https://github.com/algolia/instant-search-demo.git" – Dung Jul 07 '17 at 17:20
  • rather than posting a detailed answer, I just want to point out that this CAN be caused by user permissions on your local computer. if you are logged in as root, you may try accessing git as your standard user, or vice versa. It can depend on which you used initially. – BasilBrush Oct 10 '20 at 19:35
  • Does this answer your question? [How to solve Permission denied (publickey) error when using Git?](https://stackoverflow.com/questions/2643502/how-to-solve-permission-denied-publickey-error-when-using-git) – Cees Timmerman Apr 14 '21 at 14:15
  • https://jhooq.com/github-permission-denied-publickey/ There is a good and detail solution for this problem. – Hu Xixi Sep 28 '21 at 08:09
  • I just use `git push -u origin main` on Dec 27, 2021, and that's ok. But when it comes to Nov 21 2022, that output just the question. Then I just use this to fix the problem. Via "git pull --rebase && git push -u origin main". – Vittore Marcas Nov 22 '22 at 00:39
  • Update EST 08:34 01 December 2022, We should try to connect the remote Git repository via ssh again, e.g: `ssh -T git@sofija_personal-github.com` . OVER! – Vittore Marcas Dec 01 '22 at 10:36

52 Answers52

890

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.

bdukes
  • 152,002
  • 23
  • 148
  • 175
  • 68
    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... – IgorGanapolsky Feb 21 '14 at 14:57
  • 51
    The helping part is using `ssh -vT git@github.com` from the [Github help page](https://help.github.com/articles/error-permission-denied-publickey/) to help debug what key your repo is using. I suggest you add it in your answer. – MaximeBernard Dec 22 '15 at 09:54
  • I need to remember that on GitHub if you switch from HTTPS and SSH, it remembers this setting for all the repos! I tend to switch between the two, as I use TeamCity, which uses SSH and for my local repo I tend to just use HTTPS. I guess setting up SSH locally will prevent me from getting the same 'issue' in the future! Thanks @bdukes – hatsrumandcode Apr 01 '16 at 08:58
  • 41
    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. – Christophe Jul 02 '16 at 09:07
  • @Christophe thanks for the `ssh -vT` - it was looking in `/var/root/.ssh/config`, then I tried `echo $USER` and realized I was working as the root user (having previously done `sudo -s`). – David LeBauer Jul 20 '16 at 18:57
  • 4
    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). – David LeBauer Jul 20 '16 at 18:58
  • @PatrickRead how do you use https? Do you git remote set-url origin "https://...."? Can't find any information on that. – Sydney Oct 16 '16 at 15:25
  • @Sydney Try this: https://help.github.com/articles/which-remote-url-should-i-use/#cloning-with-https-urls-recommended In general, use an HTTPS url, and use a credential helper to cache credentials locally if needed. – Patrick Read Oct 17 '16 at 12:23
  • 1
    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. – Digital Impermanence Jan 13 '17 at 02:55
  • 1
    Thanks @David This was my resolution. I was using `sudo git clone...` because I didn't set the owners correct of the WWW folder. Because of the sudo it would look in the root folder for the ssh file :). Doing a `whoami` and `sudo whoami` made it clear which user was called up on where. Changing the folder owner allowed me to not do the `sudo git clone` (which is silly anyway) and now it is cloning like a baby. Thanks – Mattijs May 25 '17 at 11:36
  • This pointed me to the Github Docs which really made it simple. Why did I think googling for an hour would be better than heading straight to the source? : ) Thanks. – Belder Oct 06 '17 at 13:10
  • If following [githubs troubleshooting guide](https://help.github.com/articles/error-permission-denied-publickey/) leads to `The agent has no identities.`, indeed `ssh-add ~/.ssh/id_rsa` is your most likely cure. Verifiable by another `ssh-add -l -E md5`. →[**Explanations here**](https://stackoverflow.com/a/28444641/444255) – Frank N Apr 10 '18 at 03:39
  • use full url `git clone https://github.com/Tatsh/kate-wakatime.git` instead of `git clone git@github.com:Tatsh/kate-wakatime.git` – Rγσ ξηg Lιαη Ημ 雷欧 May 25 '21 at 09:46
  • Some people use https, so ssh isn't an option (plus it's a tedious setup). If you get this error message just set `git remote set-url origin https://github.com/username/repository.git` – 6rchid Oct 12 '21 at 09:24
  • If you are trying to reploy a repo from a 3rd party site, you may need to edit your GitHub authorizations/App Settings: https://github.com/settings/installations – James L. Oct 27 '21 at 21:44
  • 1
    I followed this link https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection but first checked if I had an ssh key generated first and then made sure it was added to my account using the linked help pages on the top of this guide. I then ran `ssh -T git@github.com` which told me I was successfully authenticated. I could then successfully push to my repo. Hope this helps someone! :) – Lushawn Jun 14 '22 at 10:09
255

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.

VelikiiNehochuha
  • 3,775
  • 2
  • 15
  • 32
151

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.

kensai
  • 943
  • 9
  • 16
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • 2
    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 ! – parasrish Jun 04 '16 at 19:38
  • 4
    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? – Eden Harder Nov 23 '16 at 22:07
  • @EdenHarder I am facing the same issue. Did you find an answer to it? – Vreddhi Bhat Jul 25 '18 at 06:13
  • 3
    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. – jg3 Feb 22 '21 at 04:29
  • 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](https://stackoverflow.com/questions/26266778/ssh-config-file-for-windows-git/49352601#answer-49687394) – Cristian F. May 27 '21 at 18:50
  • @LalitMohan It works for multiple keys. You have to make sure your **config** file is properly formatted and indented. – IgorGanapolsky Aug 10 '21 at 15:56
  • It has resolved my case today, a config file to configure multiple repositories with multiple ssh keys depending on each ones. – BendaThierry.com May 09 '22 at 12:21
  • 1
    For gists I had to add a wildcard prefix: `Host *.github.com` – veganaiZe Aug 10 '22 at 15:15
  • 1
    use this command to add this cd ~/.ssh/ nano config Host github.com IdentityFile ~/.ssh/github_rsa Save and exit the file by pressing Ctrl + X, then Y, and finally Enter. – Mr.Senhaji Apr 21 '23 at 05:42
  • 1
    You saved me @IgorGanapolsky Thanks – sankar banerjee Aug 18 '23 at 11:25
72

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.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
68

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.

rmundo
  • 788
  • 5
  • 4
  • 9
    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 = https://github.com//.git While for git+ssh: url = git@github.com:/.git Having the wrong url causes the public-key permission denied error which is probably not a very clear error. – darKoram Dec 25 '14 at 15:52
  • for my case in the final just works with in the part: git remote set-url origin https://github.com/your_directory/your_project.git – rld Sep 14 '15 at 19:38
41

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
george mano
  • 5,948
  • 6
  • 33
  • 43
35

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.

xinerd
  • 391
  • 3
  • 7
  • This solution worked on ubuntu, $ssh-add /root/.ssh/id_rsa – 0xFK Feb 03 '17 at 22:42
  • 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` – Pogrindis Dec 18 '18 at 02:06
30

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.

Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44
22

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)

d1jhoni1b
  • 7,497
  • 1
  • 51
  • 37
  • Do I have to copy the `SHA` output to github from this command? – IgorGanapolsky Apr 12 '16 at 15:42
  • No, just execute "ssh-keygen -lf ~/.ssh/" – d1jhoni1b Oct 04 '16 at 21:04
  • 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) – d1jhoni1b Oct 04 '16 at 21:09
  • $ ssh-keygen -lf ~/.ssh/github_rsa.pub /home/mona/.ssh/github_rsa.pub: No such file or directory – Mona Jalal Oct 27 '16 at 20:48
  • "No such file or directory" means you dont have that file name or path... what do you get when when execute ls -al ~/.ssh? – d1jhoni1b Oct 29 '16 at 07:46
21

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!

marcdahan
  • 2,654
  • 25
  • 25
  • 1
    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 – Noha Salah Sep 16 '18 at 11:46
16

If you having issues to make connection with your enterprise Github account then follow the below steps

Solution #1

ERROR: 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
  • $ cd ~/.ssh

  • $ cat id_rsa.pub

  • copy display content. enter image description here

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

  • Click on the new ssh key button enter image description here

  • enter any title and key that you copied enter image description here

  • check now your issue is resolved

Solution #2

ERROR 2: remote: Password authentication is not available for Git operations.
remote: You must use a personal access token or SSH key.
remote: See https://github.compnay.com/settings/tokens or https://github.compnay.com/settings/ssh
fatal: unable to access 'https://github.company.com/repo/app.git/': The requested URL returned an error: 403

Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

https://namespaceit.com/blog/remote-support-for-password-authentication-was-removed-on-august-13-2021-please-use-a-personal-access-token-instead

Abhishek Tomar
  • 827
  • 1
  • 10
  • 20
15

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!

jfunk
  • 7,176
  • 4
  • 37
  • 38
12

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

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

URL = https://github.com/<username>/<repository>.git

  1. git remote add origin URL Again add your URL to make remote repository
  2. 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 :)

Damika
  • 622
  • 2
  • 8
  • 17
11

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.

roman
  • 788
  • 1
  • 10
  • 23
  • 1
    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. – Taekahn Jun 08 '18 at 17:07
10

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 .

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
10

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.

N. Osil
  • 494
  • 7
  • 13
9

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?

  1. Ran this command to tell my git to use https instead of ssh:

    git config --global url."https://".insteadOf git://
    
  2. Remove any remote if any

    git remote rm origin
    
  3. 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!! :)

T J
  • 42,762
  • 13
  • 83
  • 138
3AK
  • 1,223
  • 15
  • 22
  • 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. – Igor Stoppa Oct 25 '16 at 03:21
6

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

xproph
  • 1,171
  • 11
  • 7
  • 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. – lydell Feb 02 '20 at 14:50
5

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.

user7506706
  • 51
  • 1
  • 1
5

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

enter image description here

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

prosti
  • 42,291
  • 14
  • 186
  • 151
4

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

Kevin
  • 108
  • 1
  • 12
  • This. This can also cause the problem. – KevinO Mar 27 '18 at 19:17
  • 1
    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 ` – David Sep 13 '18 at 19:39
  • @David, I also recommend that as well. 777 is pretty hacky and not secure. – Kevin Sep 27 '18 at 15:31
4

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.

imflash217
  • 193
  • 10
4

TLDR:

  1. make sure you have write access to the repo (configure it from the repo's settings).
  2. 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.

Han Zhang
  • 352
  • 2
  • 6
  • Can you be more specific on point 1? – 2540625 Jun 19 '22 at 22:23
  • 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. – Han Zhang Jun 21 '22 at 12:52
  • If you are trying to clone a public repo, it is highly likely that your SSH key is not added to your github account. – Han Zhang Jun 21 '22 at 12:56
  • My question was meant to ask where exactly in the repo’s settings write access is granted. – 2540625 Jun 25 '22 at 15:55
3

enter image description hereIf you are using Widows-10, follow this instruction. It works for me.

  1. Open terminal as administrator

  2. run this command: "ssh-keygen". It generate a ssh key and will show the folder where it has been created. Check my imageenter image description here

  3. copy the generated "ssh key"

  4. 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"

coding360
  • 161
  • 1
  • 2
3

In case somebody else needs this. I created an ssh key with a different name like ar-2022-ssh in the user's .ssh folder. The problem with this is that Checking for existing SSH keys specifies that supported public keys for GitHub are one of the following.

id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

Once I changed my ssh key name to one of those when generating a key, it worked connecting to GitHub

kenshima
  • 401
  • 4
  • 12
3

I faced the same problem.

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

You can track these steps if using the linux:

1. Check for existing SSH keys:

ls -al ~//.ssh/

2. Generate a new SSH key:

ssh-keygen -t ed25519 -C "<your_email_address>"

3. Add your SSH key to ssh agent:

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_ed25519

4. Add your public SSH key to your GitHub account:

cat < ~/.ssh/id_ed25519.pub

Now you are able to push again.

2

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.

2

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.

Nabin
  • 11,216
  • 8
  • 63
  • 98
2

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 :)

Joomler
  • 2,610
  • 3
  • 30
  • 37
2

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.

justin
  • 651
  • 1
  • 8
  • 18
2

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.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
1

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

juliarm
  • 51
  • 3
1

I was getting same error during 'git push'. In client side I had two origin and master. I removed one, then it worked fine.

Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
1

Once scenario where this will happen is when you follow GitHub instructions after you create your repository. Git will instruct you to add your remote with something like this.

git remote add origin git@github.com:<user>/<project>.git

Replace what's in <> with values related to your account.

The solution is to remove the .git suffix. Add the remote as follows:

git remote add origin git@github.com:<user>/<project>

Litehouse
  • 864
  • 8
  • 14
1

tl;dr

in ~/.ssh/config put

PubkeyAcceptedKeyTypes=+ssh-dss

Scenario If you are using a version of openSSH > 7, like say on a touchbar MacBook Pro it is ssh -V
OpenSSH_7.4p1, LibreSSL 2.5.0

You also had an older Mac which originally had your key you put onto Github, it's possible that is using an id_dsa key. OpenSSH v7 doesn't put in by default the use of these DSA keys (which include this ssh-dss) , but you can still add it back by putting the following code into your ~/.ssh/config

PubkeyAcceptedKeyTypes=+ssh-dss

Source that worked for me is this Gentoo newsletter

Now you can at least use GitHub and then fix your keys to RSA.

pjammer
  • 9,489
  • 5
  • 46
  • 56
1

This Worked for me

There are 2 options in github - HTTPS/SSH

I had selected SSH by mistake due to which the error was occuring -_-

Switch it to HTTPS and then copy the url again and try :)

Ashutosh Tiwari
  • 1,496
  • 11
  • 20
  • remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead. remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information. Switching to HTTPS did not work for me anymore. – Christian Gold Sep 16 '21 at 06:37
1

I had the same issue but none of the answers worked but a simple solution worked. But please note that I did add the ssh key as suggested by the answers first. Not sure if that really helped. Anyway this is what finally worked:

Change your remote's URL from HTTPS to SSH with the git remote set-url command. $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git. Verify that the remote URL has changed.

S.Perera
  • 874
  • 3
  • 9
  • 24
1

I also got this problem since I use several GIT accounts and also different ones for work and university, this worked for me:

-> go to your ssh config

cd ~/.ssh/
vim config

-> copy the following block and fill in the host and user you want to push to/with

Host [Hostname]
User [User]
PubkeyAcceptedAlgorithms +ssh-rsa
HostkeyAlgorithms +ssh-rsa

I got this problem for a few hostnames so now i have several of those configurations in my ssh config file.

1

This could be caused by you SSH Key/ GPG needing to be update on you organization github or gitbucket site. Follow these steps to sole it:

  1. on cmd: type>> $ ssh-keygen -t rsa -C "your_email@example.com" (results in creationof id_rsa and id_rsa.pub)
  2. $ ssh-add ~/.ssh/id_rsa
  3. cat ~/.ssh/id_rsa.pub and copy the key and go to you org bitbucket/ github site and paste it on the ssh keys
Luthoz
  • 351
  • 3
  • 6
1

In ssh mode, github should recognize your machine. for any action (cloning) about github should recognize you. In the first step, you should Generate a new SSH key in your machine. use this link.

and use this to add it to github.

AliTN
  • 96
  • 5
0

Maybe your ssh-agent is not enable You can try it

  1. Download git

http://git-scm.com/

  1. Install it

  2. Enable ssh-agent

C:\Program Files\Git\cmd

start-ssh-agent

徐啟倫
  • 19
  • 4
0

you can try this way...

enter image description here

enter image description here

choose another links

Cù Đức Hiếu
  • 5,569
  • 4
  • 27
  • 35
0

Go to your GitHub account dashboard, find your project repository, click Settings tab - under Deploy keys you'll have to add your SSH key. Open Terminal and type:

cat ~/.ssh/id_rsa.pub | pbcopy 

This will copy the key from your id_rsa.pub file. So just go back to GitHub dashboard, paste it, click Add Key and that's it.

The same solution applies to Bitbucket accounts.

ajlaracast
  • 53
  • 7
0

I had 2 Github identity files loaded in the ssh agent. My personal key and another for a client project.

I removed the key that was causing confusion:

ssh-add -d id_rsa_github_somekey

Alex Aguilar
  • 980
  • 5
  • 9
0

Using Https is fine, run git config --global credential.helper wincred to create a Github credential helper that stores your credentials for you. If this doesn't work, then you need to edit your config file in your .git directory and update the origin to the https url.

See this link for the github docs.

Ryan Walker
  • 715
  • 2
  • 10
  • 23
0

I had the same issue with AWS CodeCommit. All was working on one laptop but it wasn't on the other. The solution was to remove 'PreferredAuthentications publickey' line from .ssh/config in the section with the AWS config.

Strabek
  • 2,391
  • 3
  • 32
  • 39
0

I tried to fix similiar problem with hours. In the end I deleted all keys and made new keys without passphrases and finally get rid of the problem.

The problem was with the SSH passphrase, which need to be removed!

Simba
  • 303
  • 1
  • 4
  • 17
0

Complete steps to setting SSH access to GitHub was already posted on ServerFault.

Check the answer from @Alex: https://serverfault.com/a/1088209/416091

At 24 Jul 2022 , I should set the server and port to ssh.github.com:443 in SSH user config file ( ~/.ssh/config).

X00D45
  • 61
  • 3
0

This answer is a little different than the others, but there are other instances where you can have properly configured SSH keys on your system and still run into this.

I was encountering this issue when running git commands in a python subprocess inside an isolated environment (e.g. using Tox). I fixed it by passing through SSH_AUTH_SOCK and SSH_AGENT_PID env variables.

Brendan
  • 1,905
  • 2
  • 19
  • 25
0

Steps to troubleshoot here

This happen with me on SourceTree In my case, something wrong with my pair public-private key. So I delete this pair and generate new one fix my problem

Huy Hóm Hỉnh
  • 597
  • 7
  • 18
0

The way I was able to fix this issue for me was using the "Caching Your GitHub credentials in git

In the command line, enter { gh auth login }, then follow the prompts

you can refer to the documents with in GitHub

https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33849855) – user12256545 Feb 19 '23 at 14:21
-1

Use the URL for this page when adding GitHub as a remote. it wont cause any such error and push the code