Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix "Permission denied (publickey)" error when pushing with Git

"Help, I keep getting a 'Permission Denied (publickey)' error when I push!"

This means, on your local machine, you haven't made any SSH keys. Not to worry. Here's how to fix:

  1. Open git bash (Use the Windows search. To find it, type "git bash") or the Mac Terminal. Pro Tip: You can use any *nix based command prompt (but not the default Windows Command Prompt!)
  2. Type cd ~/.ssh. This will take you to the root directory for Git (Likely C:\Users\[YOUR-USER-NAME]\.ssh\ on Windows)
  3. Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that tell your computer how to communicate with GitHub, BitBucket, or any other Git based service. Type ls to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". This will create both id_rsa and id_rsa.pub files.
  5. Now, go and open id_rsa.pub in your favorite text editor (you can do this via Windows Explorer or the OSX Finder if you like, tpying open . will open the folder).
  6. Copy the contents--exactly as it appears, with no extra spaces or lines--of id_rsa.pub and paste it into GitHub and/or BitBucket under the Account Settings > SSH Keys. NOTE: I like to give the SSH key a descriptive name, usually with the name of the workstation I'm on along with the date.
  7. Now that you've added your public key to Github and/or BitBucket, try to git push again and see if it works. It should!

More help available from GitHub on creating SSH Keys and BitBucket Help.

@MustafaOrkunAcar

This comment has been minimized.

Copy link

commented Apr 28, 2014

Adding the SSH Key did not work for me. Using Git Bash on Windows 8, I still get the same error.

@Nadye

This comment has been minimized.

Copy link

commented Jun 20, 2014

for me as well, on the step 6 GIThub tells me that the key is in an invalid format

@ghost

This comment has been minimized.

Copy link

commented Jun 27, 2014

And more also. On Windows 7, I get:

$ git clone git@github.scm.corp.ebay.com:Magento/devdocs_internal.git
Cloning into 'devdocs_internal'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I've tried a few things from stackoverflow with same results.

@RohitUdayTalwalkar

This comment has been minimized.

Copy link

commented Jul 23, 2014

Thanks for the post - this resolved my issue!
If its not working, you might need to try running ssh-keygen again, don't specify any filename and let it overwrite the existing key - just in case the existing file is corrupt.

@Shaniqua701

This comment has been minimized.

Copy link

commented Aug 5, 2014

Finally, i had been trying to figure this one out all day. This is a God-send!

@brendan-rice

This comment has been minimized.

Copy link

commented Nov 23, 2014

Thank you, you saved me hours

@lz56

This comment has been minimized.

Copy link

commented Dec 1, 2014

works for me.
in my case, i need to reopen git bash.
Thanks!!!

@jartaud

This comment has been minimized.

Copy link

commented Feb 26, 2015

This works for me. Thank you!

@husni

This comment has been minimized.

Copy link

commented Apr 6, 2015

It works... Thanks!!

@subvi

This comment has been minimized.

Copy link

commented Apr 9, 2015

Works for me!

@cozmik

This comment has been minimized.

Copy link

commented Aug 29, 2015

finally....thanks

@a1iraxa

This comment has been minimized.

Copy link

commented Nov 1, 2015

ThankYou. You saved my life

@renatoargh

This comment has been minimized.

Copy link

commented Nov 10, 2015

You might have to create a config file (yeap, extension-less) under ~/.ssh/config with the following contents

Host bitbucket.org
    IdentityFile ~/.ssh/id_rsa_your_bitbucket_private_key

That immediately solved my problem!

@zelds

This comment has been minimized.

Copy link

commented Dec 29, 2015

Try in your local repo git remote set-url origin
(if you using httpss the must be git@github.com:USERNAME/NAMEREPO.git )

@honarmandali

This comment has been minimized.

Copy link

commented Dec 31, 2015

helpful, saved a lot of time, thanks

@androchentw

This comment has been minimized.

Copy link

commented Jan 8, 2016

Works on Mac. Thanks! saved my day!

@BenGedi

This comment has been minimized.

Copy link

commented Jan 24, 2016

Thanks man! you saved me a lot of time!

@technetbytes

This comment has been minimized.

Copy link

commented Feb 8, 2016

Solution work for me and I am successfully pushing files in github

@Nivedhithar

This comment has been minimized.

Copy link

commented Mar 8, 2016

Thank you,this works for me

@BirgitPohl

This comment has been minimized.

Copy link

commented May 10, 2016

Doesn't work for me. Terminal won't open .pup because there is no program to open it.

@ChannelJuanNews

This comment has been minimized.

Copy link

commented May 26, 2016

SOLUTION FOR DIGITAL OCEAN USERS WHO MIGHT ENCOUNTER THIS PROBLEM

I had the same problem even AFTER I had followed the instructions on the gitub page over 10 times and I finally figured out what was wrong.

Background: I have a digitalocean droplet running that I am SSH'd into. I have two users (root and admin)

My Problem

  • I ssh as admin@domain.com
  • I create ssh keys for /home/admin/.ssh/id_rsa.pub
  • I add this key to my github account
  • I verify my key by SSHing into ssh -T git@github.com and everything goes down just great
  • I cd into / and run $ sudo mkdir node_sites since I cannot create a top level directory without sudo priveleges
  • I cd into node_sites and run $ git clone git@github.com:ChannelJuanNews/myrepo.git and I get
permission denied (need sudo privileges)
  • I then run $ sudo git clone git@github.com:ChannelJuanNews/myrepo.git
Cloning into 'myrepo'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  • I repeat steps 1-7 for about 3 hours
  • I then realize my simple mistake

My Solution

  • I realized that when you create a directory with the sudo command it requires that you have sudo privileges when you do operations on that directory. (i.e. you cannot run rm -rf test_directory if you created it with sudo privleges. You would need to run sudo rm -rf test_directory ). This means that you must be sudoed if you want to clone a git repo. This lead to my second discovery
  • when you run the sudo command and use the ssh program it invokes the ssh keys from your root user.

My workaround was to just copy my ssh keys from /home/admin/.ssh/ into /root/.ssh. Whenever I run sudo, I now use my admin ssh keys instead of my root ssh keys. I am sure there is a better work around but if you needed to get your code up onto your server this one solution.

How to avoid this

Another solution would be to NOT create a directory with sudo priveleges (i.e. DO NOT create a direcotry with sudo mkdir test_dir). So when you run git clone git@github.com:ChannelJuanNews/myrepo.git it will look for YOUR ssh keys and not the root ssh keys.

@MarshallGarey

This comment has been minimized.

Copy link

commented Jun 24, 2016

Thanks! It worked perfectly in less than 5 minutes.

@CoderXpert

This comment has been minimized.

Copy link

commented Jul 13, 2016

👍

@neocamel

This comment has been minimized.

Copy link

commented Jul 20, 2016

Any reason why the generated key must be named 'id_rsa'?

It works, but I'm just curious as to why only that name works...

@george1100

This comment has been minimized.

Copy link

commented Aug 17, 2016

It missed one step between step 4 and 5: Add to ssh list on local computer.

So the complete protocol should be:

This means, on your local machine, you haven't made any SSH keys. Not to worry. Here's how to fix:

  1. Open git bash (Use the Windows search. To find it, type "git bash") or the Mac Terminal. Pro Tip: You can use any *nix based command prompt (but not the default Windows Command Prompt!)
  2. Type cd ~/.ssh. This will take you to the root directory for Git (Likely C:\Users[YOUR-USER-NAME].ssh\ on Windows)
  3. Within the .ssh folder, there should be these two files: id_rsa and id_rsa.pub. These are the files that tell your computer how to communicate with GitHub, BitBucket, or any other Git based service. Type ls to see a directory listing. If those two files don't show up, proceed to the next step. NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
  4. To create the SSH keys, type ssh-keygen -t rsa -C "your_email@example.com". This will create both id_rsa and id_rsa.pub files.
  5. Add the id_rsa to ssh list on local computer: ssh-add ~/.ssh/id_rsa.
  6. Now, go and open id_rsa.pub in your favorite text editor (you can do this via Windows Explorer or the OSX Finder if you like, tpying open . will open the folder).
  7. Copy the contents--exactly as it appears, with no extra spaces or lines--of id_rsa.pub and paste it into GitHub and/or BitBucket under the Account Settings > SSH Keys. NOTE: I like to give the SSH key a descriptive name, usually with the name of the workstation I'm on along with the date.
  8. Now that you've added your public key to Github and/or BitBucket, try to git push again and see if it works. It should!
@Caroline9L

This comment has been minimized.

Copy link

commented Aug 18, 2016

Absolutely nothing here worked for me. I've been trying to fix this for two weeks and I'm running out of hair to tear out of my head....

@vksgaikwad3

This comment has been minimized.

Copy link

commented Aug 19, 2016

Thanks..It works for me !!!

@chalsipher

This comment has been minimized.

Copy link

commented Sep 2, 2016

ChannelJuanNews solution worked for me. Thanks!

@redleafar

This comment has been minimized.

Copy link

commented Sep 4, 2016

Thanks!!

@surabhi31

This comment has been minimized.

Copy link

commented Sep 14, 2016

Thanks

@mgd216

This comment has been minimized.

Copy link

commented Oct 16, 2016

To the very kind soul that wrote this gist, THANK YOU!!

@viacreativedev

This comment has been minimized.

Copy link

commented Oct 19, 2016

@ChannelJuanNews Thank you so much, solved my issue!

@mcolic

This comment has been minimized.

Copy link

commented Oct 22, 2016

Generating a personal token worked for me. Use personal token instead of your password, and it should work.

@pierrozone

This comment has been minimized.

Copy link

commented Nov 23, 2016

For anyone using Ubuntu Bash 16.04 on Windows 10, this is what worked for me after trying everything possible:
sudo bash
su
Generate new ssh key as root is the only thing that worked.

@Oliviercreativ

This comment has been minimized.

Copy link

commented Nov 28, 2016

Thank you @george1100 ;)

@ZenBushido

This comment has been minimized.

Copy link

commented Jan 7, 2017

Wow...yes, steps 4 and 5 were the trick. Spent hours bouncing all over the web trying to figure out what appeared to obvious from other posts. THANKS!!!

@paullintilhac

This comment has been minimized.

Copy link

commented Jan 19, 2017

none of the above worked for me. but then I tried the following:
$ eval ssh-agent -s
$ ssh-add ~/.ssh/id_rsa
and it worked!

@christophemarois

This comment has been minimized.

Copy link

commented Jan 23, 2017

For me, it was adding a SSH identity in ~/.ssh/config that solved it:

Host github.com
  HostName github.com
  User git
  IdentityFile /Users/myusername/.ssh/my_github_ssh_private_key_registered_on_github
@jaquinof

This comment has been minimized.

Copy link

commented Jan 31, 2017

THank you so much!!! it works!
Gracias!!!! If i can I would say the same thing in different languages XD.

@ABnyc

This comment has been minimized.

Copy link

commented Feb 21, 2017

Thank You @george1100 This worked!!

@sam-karis

This comment has been minimized.

Copy link

commented Feb 22, 2017

Thanks it saved big time @adamjohnson

@cccg03

This comment has been minimized.

Copy link

commented Apr 11, 2017

worked perfectly - thanks!

@kuiwang

This comment has been minimized.

Copy link

commented Apr 26, 2017

Thanks You @renatoargh
It Does Works!

@danielmorrisphoto

This comment has been minimized.

Copy link

commented May 8, 2017

That worked great for me. On a mac. :)

@MystklFrets

This comment has been minimized.

Copy link

commented Jun 1, 2017

This worked for me after PublicKey permission denied error. Thanks!

@jrgarrigues

This comment has been minimized.

Copy link

commented Jun 18, 2017

Big help; thanks Adam.

@Stephquan

This comment has been minimized.

Copy link

commented Jun 20, 2017

I feel obligated to comment here, because I feel quite dumb and hopefully my error helps others.
If you happen to be trying to re-use keys across machines, make sure you don't mix up your id_rsa and id_rsa.pub in the process!
Took me ages to realise that the only difference across machines was the sizes were reversed.
Let my shame help others. Also, sharing keys is probably bad and you shouldn't do it.

@faverill

This comment has been minimized.

Copy link

commented Jun 21, 2017

Thanks Adam. Worked for me. The main help was telling me about "git bash." I didn't even know that existed. It put me back into the Linux world that I was use to. Very helpful!

@alexgorbatchev

This comment has been minimized.

Copy link

commented Jun 25, 2017

Under Windows 10 bash, I added the following to /etc/ssh/ssh_config

Host *
    IdentityFile /mnt/c/Users/USERNAME/.ssh/id_rsa
@DilipRajkumar

This comment has been minimized.

Copy link

commented Jun 26, 2017

Works great on mac :) Thank you!

@AhmedSalahBasha

This comment has been minimized.

Copy link

commented Jul 8, 2017

Perfect ;) Thanks man

@nodejh

This comment has been minimized.

Copy link

commented Jul 24, 2017

ssh-keygen -t rsa -C "your_email@example.com" will generate ssh key for the current user. Eg:

$ whoami
nodejh

# It will generate the ssh key for user: nodejh
$ ssh-keygen -t rsa -C "jianghangscu@gmail.com

# Check that you are connecting to the correct server 
$ ssh -T git@github.com
Hi username! You've successfully authenticated...

sudo ssh-keygen -t rsa -C "jianghangscu@gmail.com will generate ssh key for root. So that ssh -T git@github.com will return Permission Denied (publickey), but sudo ssh -T git@github.com works fine.

If you want to generate ssh key for the user: admin, you can change current user to admin then generate ssh key.

# change the current user to admin
$ su admin
# generate ssh key for `admin`
$ ssh-keygen -t rsa -C "jianghangscu@gmail.com
@ckiszka

This comment has been minimized.

Copy link

commented Aug 30, 2017

@alexgorbatchev -- solution works for me.

@JoaoLucasGtr

This comment has been minimized.

Copy link

commented Aug 30, 2017

That works! Thanks

@chawda

This comment has been minimized.

Copy link

commented Sep 16, 2017

Thank you so much. This helped.

@vikrantsingh47

This comment has been minimized.

Copy link

commented Sep 23, 2017

thanks @george1100, worked for me

@shubham1144

This comment has been minimized.

Copy link

commented Oct 5, 2017

@renatoargh..
Solved by problem aswell..thanks a lot

@zjuzhanxf

This comment has been minimized.

Copy link

commented Oct 11, 2017

Thank george1100!

@FerreiraHelio

This comment has been minimized.

Copy link

commented Oct 16, 2017

Thankssssssssssssssssss adamjohnson..... God bless you... God Bless the America...

@kdj842969

This comment has been minimized.

Copy link

commented Oct 16, 2017

That works for me! Thanks!

@MatheusPimentel

This comment has been minimized.

Copy link

commented Oct 17, 2017

thanks, that work for me!!!!!!!!!!!!

@anshultiwari1993

This comment has been minimized.

Copy link

commented Oct 20, 2017

Thank You

@mamuf

This comment has been minimized.

Copy link

commented Nov 1, 2017

If you are on a Mac and you created the key with a passphrase, then use ssh-add -K <path to private key>. The -K options allows your system KeyChain to provide the passphrase so you don't need to enter it manually. At least this works for me, I added this command for all my keys to ~/.profile so they are loaded automatically.

@Pooji6

This comment has been minimized.

Copy link

commented Nov 9, 2017

God bless you!!!!!

@dilhan2013

This comment has been minimized.

Copy link

commented Nov 10, 2017

Thanks @renatoargh
adding .ssh\config file solved the issue.

Host git.ionicjs.com
IdentityFile C:\temp\src\ionic\ionic

@nc-github01

This comment has been minimized.

Copy link

commented Nov 10, 2017

It worked, thanks!

@peteryuanpan

This comment has been minimized.

Copy link

commented Nov 16, 2017

Thank you very much. It works. I've stored the page for use in the future.

@anyouuu

This comment has been minimized.

Copy link

commented Nov 16, 2017

ssh -T git@github.com retrun .Hi ! You've successfully authenticated, but GitHub does not provide shell access.
but
git clone git@github.com return

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

Please make sure you have the correct access rights
and the repository exists.

@tserenbaatar

This comment has been minimized.

Copy link

commented Nov 21, 2017

it is working thanks!

@praideal

This comment has been minimized.

Copy link

commented Nov 22, 2017

Worked for me, many thanks

@demonus521

This comment has been minimized.

Copy link

commented Nov 24, 2017

The note in step 5 has saved me...
Working!!!

@giovanicani

This comment has been minimized.

Copy link

commented Nov 29, 2017

thanks!!! WORKING!

@DasunKanishka

This comment has been minimized.

Copy link

commented Dec 16, 2017

Thanks a lot...!

@AtoMaso

This comment has been minimized.

Copy link

commented Dec 24, 2017

Very simple and it works

@thiagofrancisquete

This comment has been minimized.

Copy link

commented Dec 30, 2017

"Key is invalid. Ensure you've copied the file correct" but I did it correctly -.-

@almo2001

This comment has been minimized.

Copy link

commented Jan 2, 2018

Windows 7, Cygwin. It was not asking me for the passphrase on the key when trying to clone and it was failing because of that. I created a new key without a passphrase, and it started working.

@kenmaready

This comment has been minimized.

Copy link

commented Jan 24, 2018

Heavens open and 1,000 angels sign "HALLELUJAH!" Thank you.

@piyali1988

This comment has been minimized.

Copy link

commented Jan 25, 2018

This solution works perfectly. Thanks.

@schroef

This comment has been minimized.

Copy link

commented Feb 11, 2018

YESSS this worked, i had it working earlier but somehow stoped. Im using a beta app now on OSX, the official latest release wont work.

PS one step in this you need to check when 'id_rsa.pub' is created it adds 1 extra return at the end of the code. Dont copy that, you need to copy the part from ssh-rsa till the end of youremail@email.com

@TheStoneWolf

This comment has been minimized.

Copy link

commented Feb 14, 2018

Finally it works! My deepest thanks to you!

@RandyBooth

This comment has been minimized.

Copy link

commented Feb 14, 2018

Thanks @george1100 ! ssh-add ~/.ssh/id_rsa fixed this issue.

@stevenpi

This comment has been minimized.

Copy link

commented Feb 19, 2018

I got this error using SourceTree and MacOS. This solution worked for me. Thank you very much!

@nachayj

This comment has been minimized.

Copy link

commented Feb 23, 2018

Wow! Thanks. It works!!!!!

@lnkhanh

This comment has been minimized.

Copy link

commented Mar 9, 2018

Thanks. You saved my day (y)

@ClareKang

This comment has been minimized.

Copy link

commented Mar 14, 2018

It works for me!! Thanks!

@Xfaramir

This comment has been minimized.

Copy link

commented Mar 20, 2018

Compare the list of SSH keys with the output from the ssh-add command.

@inferiore

This comment has been minimized.

Copy link

commented Mar 26, 2018

Set your global config
check if the variable (git config --lis) remote.origin.url is correct (not .git, this should be a url format)
set correct url git config --global remote.origin.url https://github.com/user/repository
this work for me, i hope so that work for you

@BrunoXL

This comment has been minimized.

Copy link

commented Mar 26, 2018

Thanks! That was what i needed.

@dhousto

This comment has been minimized.

Copy link

commented Apr 11, 2018

still works thanks!

@rolandvarga

This comment has been minimized.

Copy link

commented Apr 19, 2018

you don't necessarily have to use the default keys in your home folder. In fact you can store multiple keys in any location you desire, just have to make sure that they are added to your ssh agent:

# start agent
eval $(ssh-agent)

# add your keys
ssh-add /path/to/key-A
ssh-add /tmp/key-B

# list keys added to agent
ssh-add -l
2048 SHA256:/ASDFLKAJSDFLKDalsjdkjflaskdfjasdASDLKFJSDFKJSA /path/to/key-A (RSA)
4096 SHA256:OIUASDFOASIUFSasdofiuasodifuaosdifuASDOFIUASOD  /tmp/key-B     (RSA)
@Ashish-Panwar

This comment has been minimized.

Copy link

commented May 12, 2018

Worked fine for me ..thanks :)

@chen7410

This comment has been minimized.

Copy link

commented May 13, 2018

Thank you so much

@monizza

This comment has been minimized.

Copy link

commented May 18, 2018

Solution worked for me thank you ...:)

@stanylivein

This comment has been minimized.

Copy link

commented May 28, 2018

Thanks for your help

@dupls

This comment has been minimized.

Copy link

commented Jun 5, 2018

Excellent worked for me. Good idea to name it as I change workstation and lost track.

@zmihailo

This comment has been minimized.

Copy link

commented Jun 12, 2018

Thank you. It works for me.

@AdamAndSubtractThem

This comment has been minimized.

Copy link

commented Jun 14, 2018

For those still having issues with this because they renamed their ssh key something other than the default name. If you rename the SSH key, even if it's still in the default directory, you have to create a config file as @renatoargh and a few others have mentioned above:

Host bitbucket.org
IdentityFile ~/.ssh/id_rsa_your_bitbucket_private_key

Once you do this, your custom-named ssh key should now get recognized. Hope this helps!

@anRoswell

This comment has been minimized.

Copy link

commented Jun 28, 2018

BUenas,

Para mi esto funciono perfectamente, estoy intentando montar un programa IONIC a GITHUB y luego a IONIC PRO para probarlo directamente en dispositivos MAC

primera parte listo, muchas gracias.

@strongSoda

This comment has been minimized.

Copy link

commented Jul 1, 2018

If it doesn't work by leaving the ssh key filename to default. Then try doing all commands in the git bash located at the git files location.

In my case, I was getting this error in VSCode Although I had generated the key and also left it to default name. But when i navigated to the git.exe location and voila it worked from there.

@osfunapps

This comment has been minimized.

Copy link

commented Jul 8, 2018

saved my life bra!

@PierreARM

This comment has been minimized.

Copy link

commented Jul 31, 2018

After generating the keys in /c/Users/{MyUserId}/.ssh, I had to change the HOME variable from h: to /c/Users/{MyUserId}/ and this worked.

@shobekhan

This comment has been minimized.

Copy link

commented Aug 6, 2018

For me, it was adding a SSH identity in ~/.ssh/config that solved it:

Host yourhostname.com
User yourusername
IdentityFile ~/.ssh/OpenSSH-private-key

@prnysarker

This comment has been minimized.

Copy link

commented Aug 7, 2018

After deleting .ssh directory and after that restarting again it worked.

@romeroleonardoalexandre

This comment has been minimized.

Copy link

commented Aug 9, 2018

Thank u man.
Works for me.

@stuckiest

This comment has been minimized.

Copy link

commented Aug 10, 2018

This worked great. Thank you for the clear instructions and the special note about where to find your .ssh file on a windows computer! Really appreciate it!

@eqon133

This comment has been minimized.

Copy link

commented Aug 19, 2018

thanks!

@kanzash

This comment has been minimized.

Copy link

commented Sep 2, 2018

I already have id_rsa and id_rsa.pub files that I generated when creating ssh keys for connecting to my school's Linux server. Can I uses the same key that I already have generated or is that not safe? I don't want to overwrite my credentials for the ssh keys that I have for my school's linux server, but I also want to push to my personal repositories without having to enter my credentials every time. I'm confused on how to achieve this

@Reptar-IT

This comment has been minimized.

Copy link

commented Sep 7, 2018

This solved my issue.

@CarsonRey

This comment has been minimized.

Copy link

commented Sep 7, 2018

This worked for me!! Thank you 👍

@TomasVotruba

This comment has been minimized.

Copy link

commented Sep 17, 2018

Thanks, works like a charm :) 👍

@SantiagoCYB

This comment has been minimized.

Copy link

commented Sep 28, 2018

Thanks for the help, this wroked for me!

@KannikaKlinthoop

This comment has been minimized.

Copy link

commented Oct 2, 2018

Thank you.

@mrcat323

This comment has been minimized.

Copy link

commented Oct 8, 2018

Host github.com HostName github.com User git IdentityFile /Users/myusername/.ssh/my_github_ssh_private_key_registered_on_github

Thanks buddy! 👍

@mrcat323

This comment has been minimized.

Copy link

commented Oct 8, 2018

ssh -T git@github.com retrun .Hi ! You've successfully authenticated, but GitHub does not provide shell access.
but
git clone git@github.com return

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

Please make sure you have the correct access rights
and the repository exists.

@anyouuu you should clone repos like this: git@github.com:USERNAME/REPOSITORY_NAME

@elron

This comment has been minimized.

Copy link

commented Oct 10, 2018

Thanks this worked perfectly!

@ciprian-cimpan

This comment has been minimized.

Copy link

commented Oct 30, 2018

SOLUTION FOR DIGITAL OCEAN USERS WHO MIGHT ENCOUNTER THIS PROBLEM

How to avoid this

Another solution would be to NOT create a directory with sudo priveleges (i.e. DO NOT create a direcotry with sudo mkdir test_dir). So when you run git clone git@github.com:ChannelJuanNews/myrepo.git it will look for YOUR ssh keys and not the root ssh keys.

Thank you Juan, this was really helpful!

@Divya0319

This comment has been minimized.

Copy link

commented Nov 4, 2018

It just showed a warning "Permanently added RSA key to known host" and if you would leave passphrase as empty during RSA key creation, it will push the file without any hassle.It did work for me!!!

@darkshinobiN7

This comment has been minimized.

Copy link

commented Nov 5, 2018

Worked as a charm, thank you so much!

@Tom-a-Hawk

This comment has been minimized.

Copy link

commented Nov 12, 2018

Worked like a champ .... thank you for taking the time to write that up!

@mauriciojaramillo

This comment has been minimized.

Copy link

commented Nov 14, 2018

This worked fine for me on Mac OSX High Sierra.

For Windows 7 i followed this page instructions "https://docs.joyent.com/public-cloud/getting-started/ssh-keys/generating-an-ssh-key-manually/manually-generating-your-ssh-key-in-windows" using Git Bash solution.

I hope this help.

@kratzercanby

This comment has been minimized.

Copy link

commented Nov 20, 2018

I already have id_rsa and id_rsa.pub files that I generated when creating ssh keys for connecting to my school's Linux server. Can I uses the same key that I already have generated or is that not safe? I don't want to overwrite my credentials for the ssh keys that I have for my school's linux server, but I also want to push to my personal repositories without having to enter my credentials every time. I'm confused on how to achieve this

If you already have an RSA public and private key pair, then you just need to add the contents of your public key (id_rsa.pub) to your GitHub SSH keys list. Follow steps 5-7 of the original post. You should be able to use the same key for multiple things, since the programs generally just need read access to the key files when they are authenticating data transfer (which is unlikely to happen simultaneously, and might not even matter if it does).

@ambrwlsn

This comment has been minimized.

Copy link

commented Nov 26, 2018

Thank you this worked for me after much searching for a solution 🙏

@dnaphade1

This comment has been minimized.

Copy link

commented Dec 14, 2018

Thank you! had forgotten this step when I setup Pycharm and after having deleted the key from github the push kept failing. This solution was spot on!

@abdounasser202

This comment has been minimized.

Copy link

commented Dec 16, 2018

It's worked for me, thank you!

@ranjankrchaubey

This comment has been minimized.

Copy link

commented Dec 24, 2018

rtt min/avg/max/mdev = 8.757/8.944/9.054/0.132 ms
[koko@localhost koko-repo]$ git remote add origin https://github.com/ranjankrchaubey/firstrepo.git
fatal: remote origin already exists.
[koko@localhost koko-repo]$ git push -u origin master
fatal: unable to access 'https://github.com/ranjankrchaubey/firstrepo.git/': Peer's Certificate issuer is not recognized.
[koko@localhost koko-repo]$

getting the same error

image

@davisokoth

This comment has been minimized.

Copy link

commented Dec 26, 2018

Set your global config
check if the variable (git config --lis) remote.origin.url is correct (not .git, this should be a url format)
set correct url git config --global remote.origin.url https://github.com/user/repository
this work for me, i hope so that work for you

This helped me. I as using a url with git+ssh (which is what works on Bitbucket) but apparently github accepts https instead.

@QaiserAli

This comment has been minimized.

Copy link

commented Dec 29, 2018

ssh-add ~/.ssh/id_rsa helped me and it starts working now!

@andrewufrank

This comment has been minimized.

Copy link

commented Jan 3, 2019

The problem can be in the remote host value. check with git remote -v. it should be something like

git@github.com:GITHUB_USERNAME/REPO.git

without your username, you get the Permssion denied(publickey) message

@luisphp

This comment has been minimized.

Copy link

commented Jan 5, 2019

Thanks this one work for me.

@jdglaser

This comment has been minimized.

Copy link

commented Jan 9, 2019

Thanks! Works perfect!

@nickbonham

This comment has been minimized.

Copy link

commented Jan 10, 2019

THANK YOU! :)

@AlexisThink

This comment has been minimized.

Copy link

commented Jan 15, 2019

Thanks a lot Bro!

@yackoa

This comment has been minimized.

Copy link

commented Jan 15, 2019

none of the above worked for me. but then I tried the following:
$ eval ssh-agent -s
$ ssh-add ~/.ssh/id_rsa
and it worked!

Yup this what worked for me. so I guess the local ssh server was just down. simply regenerated the key for no reason ! :)
Thanks!
👍

@jl88s

This comment has been minimized.

Copy link

commented Jan 17, 2019

01-17-2019 (Mac)

This tutorial worked for me! Remember after you open your 'id_rsa.pub', first pick a text editor of your choice that's preinstalled in your pc or off of the app store. After opening the file, you need to copy the entire text and paste that inside the SSH key text box. I had to create a new SSH apart from my first one (The Odin Project - First Rails Application).

To test it, I simply went to my new Github repository and copied the SSH key, then went back to the terminal and typed 'git clone git@github.com:your-username/git_test.git', right after it will ask you for your passphrase and if inputted correctly it should work!

Thanks for this guide! Cheers!

@chengrunyang

This comment has been minimized.

Copy link

commented Jan 26, 2019

Works for me! Thanks for the guide.

@wisestme

This comment has been minimized.

Copy link

commented Jan 26, 2019

worked for me. Thank you so so so so much

@zaytal

This comment has been minimized.

Copy link

commented Jan 27, 2019

You might have to create a config file (yeap, extension-less) under ~/.ssh/config with the following contents

Host bitbucket.org
    IdentityFile ~/.ssh/id_rsa_your_bitbucket_private_key

That immediately solved my problem!

Worked for me. Thank you!

@prashantsharmain

This comment has been minimized.

Copy link

commented Jan 29, 2019

Worked for me. Thanks.

@mrroot5

This comment has been minimized.

Copy link

commented Feb 3, 2019

PROBLEM SOLVE IF YOU USE ROOT

Just copy your id_rsa and id_rsa.pub from your user ssh folder to your root ssh folder. Example on Linux:

$ sudo cp ~/.ssh/id_rsa /root/.ssh/
$ sudo cp ~/.ssh/id_rsa.pub /root/.ssh/
@fkozaslan

This comment has been minimized.

Copy link

commented Feb 19, 2019

You are amazing ! you saved my life 💃 ChannelJuanNews

@zafark1695

This comment has been minimized.

Copy link

commented Feb 21, 2019

Thanks saviour, <3

@HikariSakura

This comment has been minimized.

Copy link

commented Mar 1, 2019

Worked for me. Ta. (o˘◡˘o)

@rama7674

This comment has been minimized.

Copy link

commented Mar 3, 2019

remote: Permission to ramya7674/rama.git denied to Ramyakrishna7674. fatal: unable to access 'https://ramya7674@github.com/ramya7674/ramya/': The requested URL returned error: 403

i need answer for this one

@Nirvana16

This comment has been minimized.

Copy link

commented Mar 5, 2019

This post should be pinned in github main page forever!

@richguo0615

This comment has been minimized.

Copy link

commented Mar 12, 2019

Thanks!

@OneCent01

This comment has been minimized.

Copy link

commented Mar 17, 2019

Thank you!

@Katepompom

This comment has been minimized.

Copy link

commented Mar 28, 2019

I did all steps exactly as you described and it worked. Thank you a lot!

@raphaelkrauss

This comment has been minimized.

Copy link

commented Mar 29, 2019

works for me. Thanks!!

@kushalarora92

This comment has been minimized.

Copy link

commented Apr 1, 2019

solved the issue. Thanks!!

@EngineerAhmedMo

This comment has been minimized.

Copy link

commented Apr 5, 2019

You saved my whole life !

thanks a lot, thank you very very much :)

@Carpe-Omnia

This comment has been minimized.

Copy link

commented Apr 9, 2019

People like you are what makes the github community so great. (worked for me btw)

@voa2000

This comment has been minimized.

Copy link

commented Apr 15, 2019

It worked for me. Thanks for providing.

@marco927

This comment has been minimized.

Copy link

commented Apr 17, 2019

NOTE: Your SSH keys must be named id_rsa and id_rsa.pub in order for Git, GitHub, and BitBucket to recognize them by default.
Works also for raspberry pi.
Thanks.

@techhomesmith

This comment has been minimized.

Copy link

commented Apr 27, 2019

great!

@techhomesmith

This comment has been minimized.

Copy link

commented Apr 27, 2019

great!

@Ali-F92

This comment has been minimized.

Copy link

commented May 5, 2019

It worked for me, thanks a lot

@jesusquev3311

This comment has been minimized.

Copy link

commented May 15, 2019

SOLUTION FOR DIGITAL OCEAN USERS WHO MIGHT ENCOUNTER THIS PROBLEM

I had the same problem even AFTER I had followed the instructions on the gitub page over 10 times and I finally figured out what was wrong.

Background: I have a digitalocean droplet running that I am SSH'd into. I have two users (root and admin)

My Problem

  • I ssh as admin@domain.com
  • I create ssh keys for /home/admin/.ssh/id_rsa.pub
  • I add this key to my github account
  • I verify my key by SSHing into ssh -T git@github.com and everything goes down just great
  • I cd into / and run $ sudo mkdir node_sites since I cannot create a top level directory without sudo priveleges
  • I cd into node_sites and run $ git clone git@github.com:ChannelJuanNews/myrepo.git and I get
permission denied (need sudo privileges)
  • I then run $ sudo git clone git@github.com:ChannelJuanNews/myrepo.git
Cloning into 'myrepo'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  • I repeat steps 1-7 for about 3 hours
  • I then realize my simple mistake

My Solution

  • I realized that when you create a directory with the sudo command it requires that you have sudo privileges when you do operations on that directory. (i.e. you cannot run rm -rf test_directory if you created it with sudo privleges. You would need to run sudo rm -rf test_directory ). This means that you must be sudoed if you want to clone a git repo. This lead to my second discovery
  • when you run the sudo command and use the ssh program it invokes the ssh keys from your root user.

My workaround was to just copy my ssh keys from /home/admin/.ssh/ into /root/.ssh. Whenever I run sudo, I now use my admin ssh keys instead of my root ssh keys. I am sure there is a better work around but if you needed to get your code up onto your server this one solution.

How to avoid this

Another solution would be to NOT create a directory with sudo priveleges (i.e. DO NOT create a direcotry with sudo mkdir test_dir). So when you run git clone git@github.com:ChannelJuanNews/myrepo.git it will look for YOUR ssh keys and not the root ssh keys.

This worked for me thanks a lot

@alirezagolrokh

This comment has been minimized.

Copy link

commented May 22, 2019

fixed. thankful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.