It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Permission denied (publickey) after restart

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

20 votes
Answer accepted

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.

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 maxamation-ops likes this

Thank you, this fixed my issue.

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?

Thanks Christophe!!!! 
I was going crazy!

Thanks Christophe!!!! 

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

Worked perfectly for me on MacOS Mojave 10.14.3!

Thanks mate. It fixed my issue.

Thank you! Fixed my issue as well.

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

0 votes
jyo Atlassian Team 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

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

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

Oh sorry, the question was actually meant for Jesse.

Thank you anyway :)

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

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.

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)

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.

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

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"

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.

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Sourcetree

Sourcetree for Windows - CVE-2019-11582 - Remote Code Execution vulnerability

A vulnerability has been published today in regards to Sourcetree for Windows.  The goal of this article is to give you a summary of information we have gathered from Atlassian Community as a st...

4,917 views 0 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you