21

I'm trying to connect to GitHub through SourceTree on OS X using SSH. Even though I can connect fine command line, I can't seem to work with any repo through SSH in SourceTree with the error:

Permission denied (publickey).

All the help topics I have found online suggest configuring the SSH client in SourceTree. However, all of that documentation is for the Windows version. How can I access the SSH Preferences on the OS X version of SourceTree?

30

It was hard to find this answer, but it looks like the way to do this is to just add the ssh key to your keychain in Mac OS. I found an answer here: https://wiki.hpcc.msu.edu/display/hpccdocs/Adding+a+Private+Key+to+Your+Mac+OSX+Keychain

From the link:

On Mac OSX, the native SSH client can use the built-in keychain directly. To add your private key to the keychain simply use the command:

ssh-add -K /path/of/private/key

As an example if your private key is stored at ~/.ssh and is named id_rsa, you would use the command:

ssh-add -K ~/.ssh/id_rsa

You will then be prompted for your passcode, which will be stored in your keychain. After this you should be ready for a password-less login.

After I added my SSH key, SourceTree started authenticating correctly.

1/18/2017 EDIT: Apparently OSX Sierra made it so you don't get your keys back when you login, so you'll have to do this every time you start your computer. BUT, there's a nice way to fix this from the Apple stack exchange: https://apple.stackexchange.com/questions/48502/how-can-i-permanently-add-my-ssh-private-key-to-keychain-so-it-is-automatically

Just follow the steps in the answer with "Update Jan 2017"

0

I tried to google this as well, since adding custom SSH Keys with OpenSSH is available in the Tools section in SourceTree(ST) for Windows.

Although I was able to ssh -T successfully from terminal, ST for Mac was denying access.

Finally, I thought of adding the keys to my agent again. Now, when I checked the list of identities added - there were none (ssh-add -l). Just add your ssh key to the agent and ST will automatically pick your key and grant access.

0

I had the same problem, except the error message I got sounded:

   Permission denied (publickey,password)

Instead of trying and erring with lots of different answers, I went after the root cause of the problem. I found out that the ssh configuration file, which is ~/.ssh/config, plays a role in this matter. In my case it contained two records:

# --- ssh to OUNL  ---
Host *
    HostName 145.20.188.96
    User sjo
    PreferredAuthentications publickey
    IdentityFile /Users/sjo00577/.ssh/id_ou_rsa
    UseKeychain yes
    AddKeysToAgent yes
# --- Sourcetree Generated ---
Host *
    HostName github.com
    User stefjoosten
    PreferredAuthentications publickey
    IdentityFile /Users/sjo00577/.ssh/id_rsa
    UseKeychain yes
    AddKeysToAgent yes
# ----------------------------

When I swapped both records, ssh to github worked again, but ssh to 145.20.188.96 did not work anymore. This prompted me to read a manual for the ssh config file. Apparently, the first occurrence of host * is used for every host, so the github host is shadowed entirely. The following solution solved the problem: Just let the first record refer only to the designated host instead of all hosts:

# --- ssh naar OUNL  ---
Host 145.20.188.96
    User sjo
    PreferredAuthentications publickey
    IdentityFile /Users/sjo00577/.ssh/id_ou_rsa
    UseKeychain yes
    AddKeysToAgent yes
# --- Sourcetree Generated ---
Host *
    HostName github.com
    User stefjoosten
    PreferredAuthentications publickey
    IdentityFile /Users/sjo00577/.ssh/id_rsa
    UseKeychain yes
    AddKeysToAgent yes
# ----------------------------

This works fine and it solved the problem. Even better is to confine github to the correct host too. So I guess that you want to look for the problem in ~/.ssh/config. I reckon that this solution will also work in situations other than Sourcetree on MacBook only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.