201

I have followed these instructions below to upload a project.

Global setup:

 Download and install Git
  git config --global user.name "Your Name"
  git config --global user.email tirenga@gmail.com
  Add your public key


Next steps:

  mkdir tirengarfio
  cd tirengarfio
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin git@github.com:tirenga/tirenga.git
  git push origin master

But I get this error:

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

tirenweb
  • 30,963
  • 73
  • 183
  • 303

22 Answers22

129

I had to add my public key to github. https://help.github.com/articles/generating-ssh-keys

Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123
tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • 3
    In case if you are running a linux machine, copying the content of id_rsa.pub file might be difficult if you are using emacs or nano. Try copying with a text editor (Notepad, TextEdit, or gedit will do just fine). If you don't have any of these, then you can use vim. – Gokul N K Mar 03 '12 at 05:42
  • 4
    @GokulNK The other alternative is to use xclip, as the page recommends. – w4etwetewtwet Mar 06 '13 at 21:09
  • 7
    `cat ~/.ssh/id_rsa.pub` might be an option? :p – torr Dec 27 '13 at 18:43
  • you can use `pbcopy < ~/.ssh/id_rsa.pub` in OS X and then paste it (`cmd+v`) in the github page. – user3648895 Feb 17 '17 at 22:18
29

after you created the RSA key pair, you must to add it to SSH using:

ssh-add ~/.ssh/id_rsa

or wherever you created your rsa key pair.

Sergio Viera
  • 581
  • 6
  • 5
  • Lifesaver! You hit the nail on the head. Thank you – Tash Pemhiwa Jan 19 '16 at 08:04
  • Agreed, it's important to first check whether ssh-agent is running (GitHub's documentation has one do it), and whether the key is added in its keyring. Something else worth checking though, for it may prevent the key to be used even if it's in ssh-agent's keyring, is the right access mode for the *private* key: it should be 0600 (`chmod 600 ~/.ssh/id_rsa`, basically). – chikamichi Jul 16 '16 at 20:16
28

Yes, It's a public key Problem. I'm a windows user,and the page below help me resolve this problem.

http://help.github.com/win-set-up-git/

more precisely this link should be helpful

https://help.github.com/articles/error-permission-denied-publickey

Novellizator
  • 13,633
  • 9
  • 43
  • 65
viprs
  • 337
  • 3
  • 4
  • 57
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Lix May 29 '12 at 08:18
27

For me the problem was the execution of clone via sudo.

If you clone to a directory where you have user permission ( /home/user/git) it will work fine.

(Explanation: Running a command as superuser will not work with the same public key as running a command as user. Therefore Github refused the connection.)

This solution requires a SSH key already to be set up: https://help.github.com/articles/generating-ssh-keys

Smundo
  • 521
  • 5
  • 8
18

Type the following command using your username and repository name:

git clone https://github.com/{user name}/{repo name}

in Ubuntu this works perfectly.

sam-w
  • 7,478
  • 1
  • 47
  • 77
rahul_pratap
  • 1,065
  • 8
  • 13
  • I think the https protocol is needed to have a secure connection, this is why it works. Git@github.. is NOT secure for the machine. – Timo Jun 06 '14 at 15:33
16

I got a solution after a long time in tutorials.

I followed the github tutorial on this link -> https://help.github.com/articles/error-permission-denied-publickey and I was able to connect in every step. But when I was trying to git push -u origin master I got this error:

Permission denied (publickey). fatal: Could not read from remote repository.

Please make sure you have the correct access rights

Thats how I`ve fixed it!! Go to the project directory using the Terminal and check it out

$git remote -v

You will get something like this:

origin  ssh://git@github.com/yourGithubUserName/yourRepo.git (fetch)
origin  ssh://git@github.com/yourGithubUserName/yourRepo.git (push)

If you are using anything different then git@github.com, open the config file on git directory by typing the command:

vi .git/config

And configure the line

[remote "origin"]

url = ssh://git@github.com/yourGithubUserName/yourRepo.git

fetch = +refs/heads/*:refs/remotes/origin/
cgmb
  • 4,284
  • 3
  • 33
  • 60
rafaeljuzo
  • 379
  • 2
  • 8
  • For me it actually worked by replacing git@github.com with @github.com – Eduard Feicho Jul 28 '14 at 09:26
  • The "git remote -v" command is not one I'd seen before, and it turned out to be the key to understanding why one of my repos was working, while the other one was returning "Permission denied (publickey)" when I tried running "git ls-remote". Very useful. Thanks. – Kevin Jan 13 '18 at 00:25
6

In my case, I had to setup the public key for another user, as I already had one for my main user. once I switched users and performed the commands in the link above I was able to perform the setup for my test server without a problem.

Dan Power
  • 1,141
  • 1
  • 12
  • 18
  • Similar problem - I was signed in as root (and overlooked this) not as the actual user that was setup with SSH. Couldn't get a commit to work for anything. Changing back to correct user resolved problem! – RidingTheRails Apr 19 '12 at 14:32
  • 1
    Definite lack of explanation for how to actually perform the fix. – Richard Jan 06 '13 at 15:11
  • I shouldn't have to spell it out, this is for you Richard! I was using my non-root account. I had to perform the command from the github help url (https://help.github.com/articles/generating-ssh-keys#platform-linux) with the sudo prefix. – Dan Power Jan 09 '13 at 13:46
5

I had this problem, but none of the solutions above worked. I could clone and fetch but couldn't push. Eventually, I figured out the problem was in the url in my .git/config, it should be:

git@github.com:<username>/<project>

(not ssh://github.com/<username>/<project>.git or https://github.com/<username>/<project>.git).

eold
  • 5,972
  • 11
  • 56
  • 75
5

Given that none of the answers here worked for me, I finally tracked down my issue connecting to Bitbucket (or Github, doesn't matter in this case) with ssh -vT git@bitbucket.org.

In my case, the failure was due to using a DSA key instead of RSA, and apparently my SSH client no longer allows that.

debug1: Skipping ssh-dss key /c/Users/USER/.ssh/id_dsa for not in PubkeyAcceptedKeyTypes

The solution was to add this to .ssh/config:

Host *
    PubkeyAcceptedKeyTypes +ssh-dss

This elegantly appends the ssh-dss key type to all existing accepted public key types and after this was done, git can now ssh into Bitbucket no problem.

Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
  • This was the solution for me on a Debian unstable machine in December 2015. I restricted it to "Host bitbucket.org" since I already had an entry for that to force my identity selection. – bitmusher Dec 28 '15 at 18:05
3

My issue was that I was trying to give my ssh key a SPECIFIC NAME every time I entered ssh-keygen on my mac terminal.

I solved the issue by just leaving the name that "ssh-keygen" generates = id_rsa. You'll end up with 2 keys in your .ssh folder on a mac, id_rsa, which is your private key, and the id_rsa.pub, which is your public key. Then I copied and saved the code from id_rsa.pub into my GitHub account settings, and that was it. Problem solved.

paopao
  • 39
  • 8
2

Adding public key is the solution.For generating ssh keys: https://help.github.com/articles/generating-ssh-keys has step by step instructions.

However, the problem can persist if key is not generated in the correct way. I found this to be a useful link too: https://help.github.com/articles/error-permission-denied-publickey

In my case the problem was that I was generating the ssh-key without using sudo but when using git commands I needed to use sudo. This comment in the above link "If you generate SSH keys without sudo, then when you try to use a command like sudo git push, you won't be using the SSH key you generated." helped me.

So, the solution was that I had to use sudo with both key generating commands and git commands. Or for others, when they don't need sudo anywhere, do not use it in any of the two steps. (key generating and git commands).

learner_19
  • 3,851
  • 1
  • 19
  • 8
2

A good one if you have installed git on your computer:

http://help.github.com/mac-set-up-git/

Jamal Khan
  • 9,371
  • 6
  • 23
  • 23
1

I faced a similar issue when running SSH or Git Clone in Windows. Following findings helps to solve my problem:

  • When you run “rhc setup” or other ssh methods to generate ssh key, it will create the private key file id_rsa in .ssh folder in your home folder, default is C:\User\UserID
  • Git for windows has its own .ssh folder in its installation directory. When you run git/ssh, it will look for private key file id_rsa in this folder
  • Solved the problem by copying id_rsa from the home folder .ssh folder to the .ssh folder in the git installation directory

Also, I think there a way to “tell” git to use the default .ssh folder in home folder but still need to figure out how.

gkl
  • 81
  • 4
1

For me, it worked like this:

In GitHub I changed the ssh link to https, and then gave the following commands:

$ git init
$ git remote add origin https:...
$ git add .
$ git commit -m "first commit"
$ git push origin master
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
1

You can try change your type connection to branch from ssh to https.

  1. nano project_path/.git/config
  2. Replace git@github.com:username/repository.git to https://username@github.com/username/repository_name.git
  3. Save file ctrl + o

After that you can try git pull without publickey

Maxim Niko
  • 19
  • 3
0

Use ubuntu on windows store

windows subsystem for linux

, your git push will work across different accounts.

MayurKubavat
  • 341
  • 4
  • 10
0

If you are using a linux machine then check,

  • do you have Openssh installed
  • if you do, then do you have an existing public key otherwise you'll have to generate one.
  • is your public key added to your github account.

Generating new SSH keys and adding them to my Github account solved my problem. You can look into this page for more details. GitHelp.

-1

You need to set up SSH keys.

This GitHub page explains how to generate keys.

If you have an existing key, you copy $HOME/.ssh/id_rsa.pub and paste it into the GitHub SSH settings page.

Richard
  • 56,349
  • 34
  • 180
  • 251
-1

A quick way to fix this if you're using a Mac is to sign out of the OSX app and log back in.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
-1

My problem was that it didn't work with a passphrase on my private key.

Hope that helps someone.

Dormouse
  • 1,617
  • 1
  • 23
  • 33
-2

I also have this problem today. The solution is setting your "ssh key". Click the url below, follow the steps, then you will sovle it.

http://github.com/guides/providing-your-ssh-key

-4

I had the same issue on windows. I switched from SSH to HTTPS and ran a Git PUSH.

git push -u origin master
Username for 'https://github.com': <Github login email>
Password for <Github login>: xxx

Successful! hope this helps.

erukumk
  • 77
  • 8