Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Permission denied (publickey) after restart

Maël Frize May 24, 2017

I'm using SourceTree 2.4.1 on Mac with my Bitbucket account. I authenticate with Google and use SSH.

Every time I have restarted my system, I'm getting an error in SourceTree (see below).

I then remove my account in SourceTree and set everything up again (including SSH) to get things working again which is quite frustrating if you need to do it at least once a week.

Can anyone help me out?

-------------------

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch --prune origin

Permission denied (publickey).

fatal: Could not read from remote repository.

 


Please make sure you have the correct access rights

and the repository exists.

Permission denied (publickey).

fatal: Could not read from remote repository.

  

 

 

6 answers

1 accepted

23 votes
Answer accepted
bastones August 15, 2017

If your config file uses UseKeychain yes and AddKeysToAgent yes for Bitbucket/GitHub and you're finding ssh-add -K only works until you reboot your Mac, make sure the Host is set to github.com and bitbucket.org. This is actually the alias for the host record. SourceTree was causing SSH to use the default id_rsa key because it wasn't using the alias name when firing the ssh command.

This fixed the recurring issue for me that forced the awful GitHub Desktop upon me. Thank goodness.

Christophe SAUVEUR January 13, 2018

Thank you for this answer.

I had the same issue and in fact the ssh config file, generated by Sourcetree (v2.6.3) was including this info :

# --- Sourcetree Generated ---
Host BunCBG-Bitbucket
    HostName bitbucket.org
    User bunCBG
    PreferredAuthentications publickey
    IdentityFile /Users/christophe/.ssh/bunCBG-Bitbucket
    UseKeychain yes
    AddKeysToAgent yes
# ----------------------------

You'll notice that the host is in fact the name of my key. I just changed it to bitbucket.org and it seems the problem is gone. My SSH key is now correctly detected.

Like # people like this
Maël Frize February 15, 2018

Thank you, this fixed my issue.

AnSu11 February 15, 2018

This works until you have only one Bitbucket account.

In my case, I've two different accounts - a private and one for ma company.
Of course the host is the same both times (bitbucket.org). Means it will always use the first entry and uses the wrong key, if I want to authenticate with the key of the second entry.

I thought the user field has effect for the request, but if not even the host matches, the user doesn't matter.

I can set the host to user@bitbucket.org, then I've to change the remote address of the repository, which works quite well. The problem then is, that submodules always use git@bitbucket.org, otherwise my colleagues have the wrong username inside their .gitmodules file.

Is there any solution for different Bitbucket accounts? Besides, why SourceTree writes "Host User-Bitbucket" into the ssh config file, if it doesn't work?

Deleted user May 21, 2018

Thanks Christophe!!!! 
I was going crazy!

richard_bravo October 10, 2018

Thanks Christophe!!!! 

Игорь Иванов October 18, 2018

Christophe SAUVEUR: "You'll notice that the host is in fact the name of my key. I just changed it to bitbucket.org and it seems the problem is gone. My SSH key is now correctly detected." 

Thanks! It works for me

janathan March 25, 2019

Worked perfectly for me on MacOS Mojave 10.14.3!

maxamation-ops August 6, 2019

Thanks mate. It fixed my issue.

Patrick Baker August 14, 2019

Thank you! Fixed my issue as well.

Joshua Alzate April 26, 2023

Thanks! I get frustrated every time my machine is rebooted and I have to setup my git credentials again and again.

0 votes
Christophe SAUVEUR January 24, 2019

I've recently discovered something really useful with this issue.

The core.sshCommand config for git. It allows to select the specific SSH key per repository.

More information here: https://superuser.com/a/912281

pentool January 24, 2019

I am using SourceTree. Where would I add the code they mention there?

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
Christophe SAUVEUR January 24, 2019

As you are using macOS, you can do Actions > Open in terminal

Howeever, I suggest adding the --local attribute to the command-line if you want to specify core.sshCommand only for the current repository.

pentool January 24, 2019

Thanks for the feedback. I have solved all my problems by switching to Geico (...err, I mean to HTTPS).

0 votes
pentool January 23, 2019

This is not working for me. When I reboot my machine, I have to add my key to the agent.

I have followed the answer given by @Christophe SAUVEUR on January 13, 2018 and entered the required info in the ssh config file. Yet after reboot, the key is lost and have to re-add it with `ssh-add -K ...`

Using macOS Mojave 10.14.2. Any ideas?

Thanks.

Andreas Sulzermeier January 23, 2019

You're using two accounts of bitbucket I guess? Otherwise the answer you mentioned is working. Maybe delete all the passphrases from your keychain app and try to add the keys per hand afterwards (see commands below).

My workaround is to comment out all the sections where the "Host" is exactly the same which I want to use now, except the latter one.
Then delete and re-add the keys (ssh-add -D and ssh-add -A)

pentool January 24, 2019

Thanks for the reply, but it doesn't work. I only have a single account.

Before, my config file was:

Host *
UseKeychain yes

Then I tried to add:

Host *
UseKeychain yes
AddKeysToAgent yes

None of these worked. I still had to use `ssh-add -K ...` each time I rebooted. I Googled this issue a few times and read various answers on stackoverflow. They all recommended the above solutions.

Then I found this thread recently and changed my config file to this (as per above recommendation) - where the <...> entries represent my actual user name:

Host <user>-Bitbucket
HostName bitbucket.org
User <user>
PreferredAuthentications publickey
IdentityFile /Users/<USERNAME>/.ssh/<my-ssh-keyfile-name>
UseKeychain yes
AddKeysToAgent yes

I have already did `ssh-add -D` and re-added the keys before with `ssh-add -K` and also tried `ssh-add -A`. But after restart, I still have to add them again because they are not remembered.

Totally frustrating.

0 votes
orionnoir April 24, 2018

Older question but still an issue I just encountered. This answer is working for me for multiple accounts on mac osx (sierra):

https://apple.stackexchange.com/questions/48502/how-can-i-permanently-add-my-ssh-private-key-to-keychain-so-it-is-automatically

0 votes
jyo
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 25, 2017

Hi Maël,

If you're on 10.12, I'd recommend entering a few values into your SSH config, which should autoload your keyfile: https://developer.apple.com/library/content/technotes/tn2449/_index.html

Cheers,
Jesse

AnSu11 February 15, 2018

Hi Maël,

do you have any solution for my problem I reported above? I've commented to Ben Stones answer.

Would be nice, because I've the problem since a year and it's really annoying - not only my problem, also to change every time the Host field of the ssh config.

BG
Andreas

Maël Frize February 16, 2018

Hi Andreas,

Since I have only 1 account Ben's solution worked for me.

I don't know how to fix this for multiple accounts, sorry!

- Maël

AnSu11 February 16, 2018

Oh sorry, the question was actually meant for Jesse.

Thank you anyway :)

0 votes
Maël Frize May 24, 2017

I have just tried re-connecting with Google (instead of removing the account) and it worked.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events