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

后端 未结 30 1841
野性不改
野性不改 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:32

    Another possibility on Windows, which is not covered in any of these answers, and is not covered in the git or github docs on troubleshooting:

    git may be using a different openssh executable than you think it is.

    I was receiving the Permission denied (public key) error when trying to clone or pull from github and ssh.dev.azure.com, and I'd followed all the instructions and verified that my SSH keys were setup correctly (from SSH's standpoint) using ssh -vT git@github.com and ssh -vT git@ssh.dev.azure.com. And was still getting these errors:

    git@github.com: Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    I eventually figured out that the problem is that Git for Windows, and Windows, both have their own versions of openssh. This is documented here: https://github.com/desktop/desktop/issues/5641

    I was relying on the Windows ssh-agent service to store my ssh key passphrases, so git (with it's separate version of openssh) couldn't read my private keys. I consider it a bug that this error message is used - it's misleading.

    The fix was:

    git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'"
    

    Or in your ~/.gitconfig:

    [core]
        sshCommand = 'C:\\Windows\\System32\\OpenSSH\\ssh.exe'
    

    Perhaps this will be fixed in git for Windows soon, but this is the 2nd time I've wasted time on this issue.

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

    I had a slight different situation, I was logged on to a remote server and was using git on the server, when I ran any git command I got the same message

       Permission denied (publickey).
       fatal: The remote end hung up unexpectedly
    

    The way I fixed it was by changing the file /etc/ssh_config on my Mac. from

    ForwardAgent no 
    

    to

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

    In my MAC I solved this with:

    cp ~/.ssh/github_rsa ~/.ssh/id_rsa
    

    For some reason my git stopped to find the private key in the github_rsa file. This happened in a specific repo. I mean that in other repositories git kept working normally.

    I think it's a bug.

    I could find this behavior running ssh -vT git@github.com

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

    I hit this error because I needed to give my present working directory permissions 700:

    chmod -R 700 /home/ec2-user/
    
    0 讨论(0)
  • 2020-11-22 08:36

    I was struggling with same problem that's what i did and i was able clone the repo. I followed these procedure for iMac.

    First Step : Checking if we already have the public SSH key.

    1. Open Terminal.
    2. Enter ls -al ~/.ssh to see if existing SSH keys are present:

    Check the directory listing to see if you already have a public SSH key.Default public are one of the following d_dsa.pub,id_ecdsa.pub,id_ed25519.pub,id_rsa.pub

    If you don't find then go to step 2 otherwise follow step 3

    Step 2 : Generating public SSH key

    1. Open Terminal.
    2. Enter followong command with you valid email address that you use for github ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    3. You will see following in terminal Generating public/private rsa key pair. When it prompts to"Enter a file in which to save the key," press Enter. This accepts the default file location. When it prompts to Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Just press enter again. At the prompt, type a secure passphrase.
    4. Enter passphrase (empty for no passphrase): [Type a passphrase] press enter if you don't want to Enter same passphrase again: [Type passphrase again] press enter again

    This will generate id_rsa.pub

    Step 3: Adding your SSH key to the ssh-agent

    1. Interminal type eval "$(ssh-agent -s)"
    2. Add your SSH key to the ssh-agent. If you are using an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file. Enter this command $ ssh-add -K ~/.ssh/id_rsa

    Now copy the SSH key and also add it to you github account

    1. In terminal enter this command with your ssh file name pbcopy < ~/.ssh/id_rsa.pub This will copy the file to your clipboard Now open you github account Go to Settings > SSH and GPG keys > New SSH key Enter title and paste the key from clipboard and save it. Voila you're done.
    0 讨论(0)
  • 2020-11-22 08:36

    In MAC, go to Terminal

    1) Navigate to Home Directory using command - cd ~

    2) cd .ssh && ssh-keygen (For Defaults, click on Enter/Return key for both inputs)

    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/username/.ssh/id_rsa):      
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /Users/usernmae/.ssh/id_rsa.
    

    3) After that , Do "ls". you will find id_rsa.pub file.

    4) Copy the contents in the id_rsa.pub file (read using the cat command - cat id_rsa.pub)

    5) Navigate to BitBucket or any version tool which supports the SSH keys. Paste the contents using the Add Key Option

    That's it. Try to commit and push now.

    0 讨论(0)
提交回复
热议问题