How to solve Permission denied (publickey) error when using Git?

后端 未结 30 1774
野性不改
野性不改 2020-11-22 08:07

I\'m on Mac Snow Leopard and I just installed git.

I just tried

git clone git@thechaw.com:cakebook.git

but that gives

相关标签:
30条回答
  • 2020-11-22 08:10

    This error can happen when you are accessing the SSH URL (Read/Write) instead of Git Read-Only URL but you have no write access to that repo.

    Sometimes you just want to clone your own repo, e.g. deploy to a server. In this case you actually only need READ-ONLY access. But since that's your own repo, GitHub may display SSH URL if that's your preference. In this situation, if your remote host's public key is not in your GitHub SSH Keys, your access will be denied, which is expected to happen.

    An equivalent case is when you try cloning someone else's repo to which you have no write access with SSH URL.

    In a word, if your intent is to clone-only a repo, use HTTPS URL (https://github.com/{user_name}/{project_name}.git) instead of SSH URL (git@github.com:{user_name}/{project_name}.git), which avoids (unnecessary) public key validation.


    Update: GitHub is displaying HTTPS as the default protocol now and this move can probably reduce possible misuse of SSH URLs.

    0 讨论(0)
  • 2020-11-22 08:10

    Guys this is how it worked for me:

    1. Open terminal and go to user [See attached image]
    2. Open .ssh folder and make sure it doesn't have any file like id_rsa or id_rsa.pub otherwise sometimes it wont properly rewrite files
    3. git --version [Check for git installation and version]
    4. git config --global user.email "your email id"
    5. git config --global user.name "your name"
    6. git config --list [make sure you have set your name & email]
    7. cd ~/.ssh
    8. ssh-keygen, it prompts for saving file, allow it
    9. cat ~/.ssh/id_rsa.pub [Access your public key & copy the key to gerrit settings]

    Note: You should not be using the sudo command with Git. If you have a very good reason you must use sudo, then ensure you are using it with every command (it's probably just better to use su to get a shell as root at that point). If you generate SSH keys without sudo and then try to use a command like sudo git push, you won't be using the same keys that you generated

    0 讨论(0)
  • 2020-11-22 08:10

    I was getting this error because I generated the ssh keys with the wrong email. I was able to connect using ssh, but not using git. The solution was to regenerate the keys using the main email address of my github account.

    0 讨论(0)
  • 2020-11-22 08:11

    If the user has not generated a ssh public/private key pair set before

    This info is working on theChaw but can be applied to all other git repositories which support SSH pubkey authentications. (See gitolite, gitlab or github for example.)

    First start by setting up your own public/private key pair set. This can use either DSA or RSA, so basically any key you setup will work. On most systems you can use ssh-keygen.

    • First you'll want to cd into your .ssh directory. Open up the terminal and run:

      cd ~/.ssh && ssh-keygen

    • Next you need to copy this to your clipboard.
      • On OS X run: cat id_rsa.pub | pbcopy
      • On Linux run: cat id_rsa.pub | xclip
      • On Windows (via Cygwin/Git Bash) run: cat id_rsa.pub | clip
    • Add your key to your account via the website.
    • Finally setup your .gitconfig.
      • git config --global user.name "bob"
      • git config --global user.email bob@... (don't forget to restart your command line to make sure the config is reloaded)

    That's it you should be good to clone and checkout.

    Further information can be found at https://help.github.com/articles/generating-ssh-keys (thanks to @Lee Whitney) -

    If the user has generated a ssh public/private key pair set before

    • check which key have been authorized on your github or gitlab account settings
    • determine which corresponding private key must be associated from your local computer

    eval $(ssh-agent -s)

    • define where the keys are located

    ssh-add ~/.ssh/id_rsa

    0 讨论(0)
  • 2020-11-22 08:11

    Are you in a corporate environment? Is it possible that your system variables have recently changed? Per this SO answer, ssh keys live at %HOMEDRIVE%%HOMEPATH%\.ssh\id_rsa.pub. So if %HOMEDRIVE% recently changed, git doesn't know where to look for your key, and thus all of the authentication stuff.

    Try running ssh -vT git@github.com. Take note of where the identity file is located. For me, that was pointing not to my normal \Users\MyLogin but rather to a network drive, because of a change to environment variables pushed at the network level.

    The solution? Since my new %HOMEDRIVE% has the same permissions as my local files, I just moved my .ssh folder there, and called it a day.

    0 讨论(0)
  • 2020-11-22 08:11

    One of the easiest way

    go to terminal-

      git push <Git Remote path> --all
    
    0 讨论(0)
提交回复
热议问题