How to solve Permission denied (publickey) error when using Git?
How to solve Permission denied (publickey) error when using Git?
Question
I'm on Mac Snow Leopard and I just installed git
.
I just tried
git clone [email protected]:cakebook.git
but that gives me this error:
Initialized empty Git repository in `/Users/username/Documents/cakebook/.git/`
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
What am I missing?
I've also tried doing ssh-keygen
with no passphase but still same error.
Accepted Answer
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 [email protected]
(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
Read more… Read less…
More extensive troubleshooting and even automated fixing can be done with:
ssh -vT [email protected]
Source: https://help.github.com/articles/error-permission-denied-publickey/
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 ([email protected]:{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.
The github help link helped me sort out this problem. Looks like the ssh key was not added to the ssh-agent. This is what i ended up doing.
Command 1:
Ensure ssh-agent is enabled. The command starts the ssh-agent in the background:
eval "$(ssh-agent -s)"
Command 2:
Add your SSH key to the ssh-agent:
ssh-add ~/.ssh/id_rsa
Got same error report.
Fixed with using HTTP instead. Since I don't want set "SSH keys" for a test PC.
Change URL to HTTP when clone:
git clone https://github.com/USERNAME/REPOSITORY.git
My problem is a little bit different: I have URL set when adding a existing local repo to remote, by using:
git remote add origin ssh://github.com/USERNAME/REPOSITORY.git
To fix it, reset URL to HTTP:
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
BTW, you may check your URL using command:
git remote -v
origin https://github.com/USERNAME/REPOSITORY.git (fetch)
origin https://github.com/USERNAME/REPOSITORY.git (push)
Hope this will help some one like me. :D
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.
- Open Terminal.
- 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
- Open Terminal.
- Enter followong command with you valid email address that you use for github
ssh-keygen -t rsa -b 4096 -C "[email protected]"
- 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 toEnter 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. Enter passphrase (empty for no passphrase): [Type a passphrase]
press enter if you don't want toEnter 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
- Interminal type
eval "$(ssh-agent -s)"
- 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
- 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.