Atlassian / BitBucket Sourcetree SSH Public Key Denied
Asked Answered
E

7

40

I'm working on a project in a private repository on https://www.bitbucket.com.

I'm coding it locally, then staging, commiting and pushing the update via BitBucket's / Atlassian's Windows Git client, Sourcetree.

After that, I'm pulling the files from a remote shared server, which requires SSH authentication.

I've tried the following in order to connect the git repo with the BitBucket account:

  1. Launched the embedded Sourcetree SSH agent (Pageant)
  2. Added my .ppk (PuTTY Private Key) to Pageant using my passphrase
  3. Opened the SourceTree Terminal in the location of my project, let's call it C:/Project.
  4. Used git init to initialize the repository.
  5. Used git push -u origin --all in order to push the repo from my computer to BitBucket.
  6. Got this error: Permission denied (publickey).
  7. Tried to see if I can get some more verbose output by doing ssh -Tv [email protected] - Still, Permission denied.

I'm trying to figure out what exactly went wrong - did Sourcetee find my SSH key at all? Is the key not loaded in some specific place, causing this behavior?

Note: I have loaded the public key in my profile on BitBucket.

Experimental answered 25/2, 2014 at 17:13 Comment(0)
B
87

To whom may have the same issue on Mac with new Sierra. Solution would be to add private key to SSH agent via:

ssh-add -K ~/.ssh/id_rsa

It looks like that identity[id_rsa] doesn't persist by SSH agent.

Note this is not a permanent solution .. You would need to do that each time you clone a new repository.At least then no need to provide private key for each push to remote.

-------- Update 28.Sep.2017 --------

Permanent solution ( On Sierra):

Steps:

1- Be sure that you have a running ssh-agent in background before doing anything.

To check if ssh-agent is running by:

pgrep 'ssh-agent'

That command returns PID (ProcessID) of that process if it's running. If you see a value.. Go to Step#2. if not, so you need to run that agent in background by:

eval "$(ssh-agent -s)"

2- Edit ~/.ssh/config (Create if it doesn't exist as su ):

Host *
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa

3- Then add that key agent ( that would be once ):

ssh-add -K ~/.ssh/id_rsa

That's it.

Actually Step#2 is the crucial one. I just want to provide a complete guide.

I hope that may help you.'.

Boothe answered 10/2, 2017 at 4:47 Comment(6)
I am new to git so don't know where to write these? tried writing exact same in terminal, it said: No such file or directoryCurious
@ChanchalRaj, I think that happened because you don't have SSH key at all @ ~/.ssh/id_dsa .To generate SSH key try: help.github.com/articles/…. Here we are solving something else.Boothe
This solved my issue for osX. Whats a more permanent solution to this?Tuba
@YahyaUddin you need to do that for each repository you clone .. which is not common and that is annoying .. Im gonna try to find better solution. It could be config or file permissionsBoothe
@Maher Abuthraa If u found any permanent solution. pls update the answer.Bondstone
@YahyaUddin I've updated my answer as you requested 🙂Boothe
U
18

I needed to further modify these settings SourceTree:enter image description here

Unreliable answered 4/12, 2014 at 21:53 Comment(5)
This was exactly what I needed - I have looked everywhere but this solved it.Partiality
The SSH Client Configuration section is missing in the Mac Sourcetree. Any clue how I can add custom SSH keys to SourceTree in Mac?Bismuthic
@ScorpionKing2k5, you can do this by opening up the terminal, add writing ssh-add /path/to/your/private/key which will add it to your list of available keys on your system. SourceTree will then check it when you try and push.Beefeater
@Beefeater - yes, that's what I had assumed how it would work. I created a new key and added it to the agent (like the git page - help.github.com/articles/generating-ssh-keys says), but still Sourcetree wouldn't pick it. But after trying to find this option in vain, I listed the keys in the ssh-agent, and surprisingly all the ssh keys in the agent were removed! Adding it again (how you've mentioned), did finally work!Bismuthic
Also note that github for mac will automatically add a public key to your agent on osx so if you just open it every once and awhile (every week it seems) then source tree will work.Abulia
E
10

Well - I've found the solution after much trial and error. Apparently, using PUTTY's keygen and storing the new public key in my .ssh folder doesn't work. However, using ssh-keygen IN GIT BASH and then trying to login using the new generated key pair, works just fine. Out of curiosity though more than anything - I would love to know why this is the case.

Experimental answered 25/2, 2014 at 17:13 Comment(4)
I think PuTTY might generate SSH keys in a different format than what some other tools might use (the more popular cross-platform alternative being OpenSSH). I'm not sure if that's actually the cause of the issue, but I'm just throwing the idea out there.Delapaz
Seems true - PuTTY puts out .ppk for it's private keys, which seems to only work with PuTTY.Experimental
That is correct -- PuTTY's private key format won't work with anything. OpenSSH, and hence Bitbucket (and anything else that uses OpenSSH) requires you to have an OpenSSH format private key -- I'm a little surprised that Pageant didn't complain about it.Darksome
@fwilson same here friens. I kinda hoped for more out of Pageant.Experimental
G
10

I know this is resolved, but to add to the accepted answer, you don't actually need Git Bash in order for this to work. As Cupcake said, it is true that PuTTY puts out .ppk files for private keys, which won't work outside the usual PuTTY/Pageant context.

This is because the terminal expects by default to find a file named id_rsa in ~/.ssh and PuTTY doesn't generate that by default. Git Bash's ssh-keygen does though, which is why Tom Granot's solution works. You CAN, though, through the PuTTY key generator, export the same exact file through the Conversions/Export OpenSSH Key... option in the menu. Just be sure to name it id_rsa and have it sit next to your id_rsa.pub file, and everything should work fine.

Guideline answered 13/11, 2015 at 4:43 Comment(0)
W
5

This is an old question, but every time there is a MacOS update, I came across this issue. Catalina was no exception.

First, you should check your config file to confirm if your old keys still there... it should:

~ ls -al ~/.ssh

Then you have to add again your key(s) by the name listed above:

~ ssh-add -K ~/.ssh/[key-name]
Waite answered 15/10, 2019 at 15:11 Comment(2)
This fixed it for me! Thank you so much.Label
After a while searching. This solved for me. Thanks!Ornery
Q
4

This happened to me after updating my Sourcetree on my Mac. What worked for me was just simply removing everything in the ~/.ssh/ folder except the .config and the known_hosts folders. Then simply go to sourcetree under preferences, remove your github account, re-add the profile again after deleting all the .ssh public and private keys and sourcetree should re-add the keys locally and to GitHub

Quicktempered answered 5/3, 2018 at 8:56 Comment(0)
C
0

you’ll need to reset the permissions to default:

sudo chmod 600 ~/.ssh/id_rsa
sudo chmod 600 ~/.ssh/id_rsa.pub

If you are getting another error:

Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/geek/.ssh/known_hosts).

This means that the permissions on that file are set incorrectly, and can be fixed with the next:

sudo chmod 644 ~/.ssh/known_hosts

And you should to adjust the directory permissions:

sudo chmod 755 ~/.ssh

Maybe you still got an error access denied on mac, try this one:

ssh-add --apple-use-keychain ~/.ssh/id_rsa
Condemn answered 26/8, 2022 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.