Search code examples
gitmacosgitlabatlassian-sourcetree

SourceTree always Permission denied (publickey)


i use soureTree for clone ssh project.

  1. i've already created ssh key
  2. i've already set up gitlab ssh key setting
  3. i've ssh-add "mysshkey"
  4. i've ssh-add -K 'mysshkey'

when i print ssh -T ,i can make it success in the command line. when i git clone, pull ,push in ssh ways...It still works in the command line.(terminal)

but in sourcetree still get error now:

Permission denied (publickey)

how can i solve it?


Solution

  • You are receiving this message because it could not authenticate you with any of the keys that were offered to it by your SSH agent. To verify this is the case, do the following:

    ssh -T 
    

    This will attempt to create a connection to it for Mercurial. You should receive a response similar to the following if your key is correctly loaded.

    If you do not see a message,this can be caused by a couple of factors, but these are the most common:

    - Your public key isn't loaded

    To check to see if your public key is loaded, do the following:

    1. Open a browser and log into origin.

    2. The SSH Keys page displays. It shows a list of any existing keys.

    3. If you do not have any keys listed,set one up.

    - Your identity isn't loaded into your SSH Agent

    If your SSH agent doesn't know to offer a key, the connection will fail. To find out what keys your SSH Agent currently is offering, and add them, do the following:

    $ ssh-add -l
    

    Then, if you don't see your key listed, add it by 

    ssh-add ~/.ssh/identity
    

    For more information, check out our full Troubleshoot SSH issues guide.

    - You do not have permission to create the repository on your local filesystem

    If you get the error "Permission denied", it is also possible that git or mercurial doesn't have permission to create the project folder locally. Check permissions for the directory where you're attempting to check out the project, and make sure you have write access.

    1   Right click and "Run as Administrator".
    2   Type ssh-keygen
    3   Press enter.
    4   It will ask you to save the key to the specific directory.
    5   Press enter. It will prompt you to type password or enter without password.
    6   The public key will be created to the specific directory.
    7   Now go to the directory and open .ssh folder.
    8   You'll see a file id_rsa.pub. Open it on notepad. Copy all text from it.
    9   Go to https://gitlab.com/profile/keys .
    10  Paste here in the "key" textfield.
    11  Now click on the "Title" below. It will automatically get filled.
    12  Then click "Add key".
    

    Found that inside /var/log/gitlab/sshd/current there were multiple occurences of a message: Authentication refused: bad ownership or modes for file /var/opt/gitlab/.ssh/authorized_keys After which I changed ownership of that file from 99:users to git:users with: chown git:users authorized_keys