Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Last active July 24, 2024 22:08
Show Gist options
  • Save adamjohnson/5682757 to your computer and use it in GitHub Desktop.
Save adamjohnson/5682757 to your computer and use it in GitHub Desktop.
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, typing 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.

@ProgrammerRomero
Copy link

Saved my life!!!

@cloud-henrique
Copy link

Thanks man, saved my life <3

@VenturaFranklin
Copy link

For me Windows did not know what ~ was even after setting HOME environment variable did not help.

Only after setting ~ environment variable to the right path (C:\Users{username}) did it work.

@shravanchk
Copy link

Thanks for the solution, also we need to check the "allow write access" check box after adding the ssh key

@Almorisson
Copy link

Almorisson commented Sep 12, 2020

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

This solution works fine on MacOSX. Just put the content below in the /etc/ssh/ssh_config and run this : source ~/.zshrc or source ~/.bashrc.

Thanks to @shravanchk

@DamirPecnik
Copy link

THX

@ZOALSD
Copy link

ZOALSD commented Sep 18, 2020

thanks

@MahdiGharooni
Copy link

you may have test by ssh key, try Https

@darcy-vitacca
Copy link

thanks!

@DrIanGregory
Copy link

Complete rubbish.
Waste of time.

@adrian-miasik
Copy link

Worked for me. Many thanks!

@thomasvilches
Copy link

Thanks!!!! It worked!

@soumya1303
Copy link

worked for me at first attempt...thank you !

@AnilkumarAEC099
Copy link

Thanks! Worked for me

@Makav3li94
Copy link

thanks alot, we lazy programmers had problem with this shit and never actualy learnt how to solve it. i'pin this shit to browser :)

@melissahuertadev
Copy link

Omg! thank you!! I forked a repo and needed to fetch from the original repo to mine, and I got this message

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

This post helped me! 🙏🏽

@OverlordsIII
Copy link

Thank you sm this helped me out a lot!

@mari967
Copy link

mari967 commented Oct 16, 2020

Thanks! it worked perfectly for me 👍

@NoailletasJordan
Copy link

Thank you :)

@KevinvFeng
Copy link

Many thanks!!!!

@Mahmud-Alam
Copy link

finally!!! you saved me hours. thanks man 😃😃😃

@TusharManohar
Copy link

In my case I have created ssh key in root user mode.
I deleted all keys and created new without sudo and worked for me

@FutureTechZW
Copy link

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!

Thanks this worked for me

@Omarjabaly
Copy link

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!

The ssh was working fine with github but when i used it over gitbash without ssh-agent it stopped working completely even with ssh-agent.
when I added the config file above it worked again. Thanks

@b01
Copy link

b01 commented Dec 3, 2020

Thanks @renatoargh

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

Of course I modified it for Github:

Host github.com
    IdentityFile ~/.ssh/id_ed25519_github.com

This is what worked for me after I moved to a new laptop with Windows 10 (202H). I assume its because ~/.ssh/id_ed25519 is used for another service and I did not have the "id_ed25519.pub" key on Github.com. So adding the config tells Git what SSH private key to use for verification when connecting to Github.com.

@valeesi
Copy link

valeesi commented Dec 8, 2020

This command got it working for me (Bitbucket) "ssh-keygen -t rsa -C "your_email@example.com".

As well as having the file name defaulted as id_rsa.

@irib102030
Copy link

Thank you so much...My problem solved

@mehmetyildiz7
Copy link

Thanks, it solved my problem

@koladee
Copy link

koladee commented Dec 27, 2020

Worked for me

@AlexFrost96
Copy link

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.

I spent a few hours solving the problem until I saw your solution. Thx a lot!!

@avinashgaur1998
Copy link

Thanks a lot. The instructions were clear with each line of code followed by the explanation.

@Demner21
Copy link

Demner21 commented Jan 14, 2021

Work for me to create config file: ~/.ssh/config
and add this configuration:

Host github.com
       Hostname github.com
       User git
       IdentityFile ~/.ssh/my_private_key

after try a test with
ssh -vT git@github.com

@dorklord23
Copy link

Works in Ubuntu 20.04 inside WSL

@AmirHossinZabbah
Copy link

Thanks, Thanks, Thanks ,....

@felipeAndrade0918
Copy link

felipeAndrade0918 commented Jan 20, 2021

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!

Holy crap, it actually solved my problem on Windows!!

@nishant1596
Copy link

thank you so much it worked

@mussymmw
Copy link

Cheers, i ran into issue when trying to use id_rsa.meaningfulname. Leaving it as id_rsa solve the problem.

@trasumanar
Copy link

Nothing helped me, only this post here helped, thank you a lotttt:

https://gist.github.com/adamjohnson/5682757#gistcomment-1978451

@devmike93
Copy link

Thanks! Helped a lot

@blexc
Copy link

blexc commented Feb 2, 2021

very cool bro.

@belloniz
Copy link

belloniz commented Feb 4, 2021

2021, still works! Thank you so much :)

@deepali-2018
Copy link

Thank you this saved my day

@erycamel
Copy link

erycamel commented Feb 5, 2021

Thank you.

@Alpha-Rome0
Copy link

adding the config was what fixed it for me

@GuilhermeHen
Copy link

Works for me, Thank you! 😁

@Emil-Valentin-Toma
Copy link

Thanks Adam

@devbogor
Copy link

sudo bash

IT WORKSSS...!!!! THANK YOU......!!!!!

@migeriva
Copy link

Thanks!!

@eduardosene
Copy link

This works with me!!! Thank you!!

@shooter1989
Copy link

great, it works! Thank you, man!

@seerbit-developers
Copy link

seerbit-developers commented Mar 26, 2021

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.

Thanks. This should work. Generate and add the key to keygen in Super User mode.

@AgboVincent
Copy link

this works, like charm

@pradeep-raj
Copy link

Its worked.
Thank you!!

@Zedade1983
Copy link

Thank you Mr. Johnson. Your tutorial breakdown was very helpful and I was able to troubleshoot the issue.

@stefandesu
Copy link

For anyone using a public GitHub dependency via https stumbling upon this when npm ci fails on GitHub Actions: This is related to the following issues:

Essentially, a local npm install will write git+ssh URLs to package-lock.json even if you have specified a git+https URL in package.json. There's a workaround that worked for me:

For the benefit of anyone looking for a workaround who struggled to understand the conversation above, if you are installing something from a public github repo and want to force https, this should work:

npm install git+https://<githubUsername>@github.com/<project>/<repo>.git

It potentially doesn't matter what github username you use, because everyone can access public repos. The use of the username qualifies as 'auth' and therefore forces the protocol to be kept, as explained in point 2 in #2610 (comment).

@zunpitchk
Copy link

Checklist TL;DR for Windows Powershell

  1. Create new ssh key ssh-keygen -t -rsa -b 4096 -C "your@email.com"
  2. Verify service-ssh is running get-service ssh-agent if result is "Stopped", run start-service ssh-agent
  3. Add private key ssh-add ~/.ssh/id_rsa

@Dindy86
Copy link

Dindy86 commented Apr 21, 2021

Thx a lot!

@yjkellyjoo
Copy link

Thanks, worked for me!

@michell-dev
Copy link

Cheers, worked like a charm.

@FlatCodeIq
Copy link

The solution in the original post works great. but if it didn't work for you, make sure you restart your Android Studio if it was open during the SSH KEY Generation process, afterwards it will work normally.

Thank you.

@SysSn13
Copy link

SysSn13 commented May 6, 2021

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!

This worked for me.

@RicardoMoFig
Copy link

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](mailto: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.

Thanks. His comment gave me an idea of what my mistake might be. I was creating the project folders outside of my user's folder and couldn't connect to GitHub when using sudo outside of the root directory.

@Mitrajit
Copy link

Thanks saved me a day.

@NERDGHOST
Copy link

Run git with the same user you used to create the SSH key. So if you did:
sudo ssh-keygen....
Use:
sudo git......

@yousra-ht
Copy link

worked for me, thanks

@baldomp
Copy link

baldomp commented Jun 24, 2021

Worked for me. Thanks

@Codeize
Copy link

Codeize commented Jun 29, 2021

Godsend! Thanks SO much!

@ahmedsliman
Copy link

Thanks so much it works with me

@upendrakumars
Copy link

Thank you , this has solved the problem !! Guys just be sure that you have to GO to github.com/setting/keys or Account setting -> SSH and GPG options to execute setp -6

@niiamarh
Copy link

niiamarh commented Jul 3, 2021

Thanks so much :)

@kestory
Copy link

kestory commented Jul 18, 2021

Work for me, thanks!

@artsycoder533
Copy link

Worked for me, thanks a lot!

@sheba-am
Copy link

worked for me thank you so much

@ionara
Copy link

ionara commented Aug 2, 2021

Worked for me! Thank you, you are the bomb

@tgaraptor
Copy link

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!

That was it! Thank you so much!!

@EricZielinski
Copy link

Deleting the two id_rsa and then ssh-keygen'd in Git Bash to regenerate those two files seemed to do the trick. Really annoying.

@bertdelaspeed
Copy link

WHERE IS THAT ANSWER, everybody saying thanks but I can't see the answer

@nyxongod
Copy link

In my case this is not a solution. If someone is interested please go to your local repository --> .git folder --> config file --> inside delete user credentials and that is all I have done.

And works ;)

@SimarPalSingh
Copy link

This Works!! Thanks a lot!!

@squidproquoio
Copy link

@ChannelJuanNews, thank you!

@FergusJJ
Copy link

FergusJJ commented Sep 10, 2021

Thank you!

@Riya-Singhal15
Copy link

thanks

@samseen
Copy link

samseen commented Sep 18, 2021

Thank you.

@Astrogeorgeonethree
Copy link

Thank you for that, my friend. Your solution was spot-on.

@anindya-bhowmik
Copy link

Thanks a lot...

@masfikalam
Copy link

worked! Thanks a ton <3

@HenkPoley
Copy link

HenkPoley commented Oct 21, 2021

Why would git version 2.31.1.windows.1 work, but git version 2.33.1.windows.1 not (gives the aforementioned publickey permission denied error) ?

Can git tell which ssh tries to use?

🤔 https://community.chocolatey.org/packages/git#comment-5573586700

Edit: The breakage I was looking at had to do with SHA-1 deprecation.

@ikiraschielke
Copy link

Thanks worked for me too!

@nathansnow1981
Copy link

Thanks so much @adamjohnson 🍻

@rahulbanerjee26
Copy link

Thank You!!

@scizmeli
Copy link

Thank you. My keys were not named id_rsa*
I renamed them, it works now.

@hellinger04
Copy link

This worked for me, thank you!

@shishuwu
Copy link

shishuwu commented Jan 5, 2022

Works for me, thanks

@joelsonderegger
Copy link

It worked - thank you! 🍻

@Highbee4u
Copy link

I don't like writing coments, and I have been on this for a while and this works!. Thanks

@saswatinandan
Copy link

Hi, I followed all options, but still getting the error
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
whiole running git pull, any kind of help will be highly appreciated.

@saswatinandan
Copy link

can anybody please help me why I am getting the same error following all options? Please suggest something

@SukaTheDev
Copy link

Thank you very much. Great Help!

@mustafamoe
Copy link

What a time saver, thanks!!!

@Shruti700
Copy link

Thanks a lot.

@zelucena
Copy link

zelucena commented Feb 1, 2022

thanks, it really did it

@derkmal
Copy link

derkmal commented Feb 6, 2022

Thank you very much! It worked out perfect!

@ecg-sniper
Copy link

This time I have to spend a minute to thank you for the good explanation.

@mariaclarahl
Copy link

Works for me, thank you very much!

@nielvid
Copy link

nielvid commented Feb 19, 2022

@rollanda21
Copy link

Thanks a lot! This worked for me.

@ekjamu
Copy link

ekjamu commented Mar 3, 2022

Thank you

@johannesrave
Copy link

Didn't work for me unfortunately. I'm still being asked to enter my ssh passphrase on WSL2.

@thirrukumar
Copy link

thirrukumar commented Mar 7, 2022

No solution for me. Finally it works after deleting contents in config file in .ssh.

@NickDulvy
Copy link

Adam, This solved a lot of github woes for me. Thanks.

@malikmukhtar
Copy link

It works, Thanks

@centratelemedia
Copy link

my problem solve here, thanks

@rocketxl5
Copy link

It worked! Thank you

@0xhafa
Copy link

0xhafa commented Mar 28, 2022

thanks!!

@Naboum
Copy link

Naboum commented Apr 5, 2022

You're a life savior, thank you

@Gio-Ayiman
Copy link

Thanks guy!

@nhut3110
Copy link

thank you, it worked for me!

@berkayalan
Copy link

Thanks you, it also worked to me!

@jharris-uncomn
Copy link

VERY helpful for this noob. Thanks!

@gconnect
Copy link

gconnect commented May 8, 2022

Thanks. This worked for me

@9h057
Copy link

9h057 commented May 11, 2022

Face palm. Thanks worked like a charm.

@OscarGabriel07
Copy link

¡Funcionó, muchas gracias!

@nelsonpoon
Copy link

Thanks. This worked for me.

@vaniswamy
Copy link

Thanks. great help.

@mavsin
Copy link

mavsin commented May 25, 2022

Awesome! I appreciate you.

@jemilehin
Copy link

jemilehin commented Jun 30, 2022

Thanks! worked for me.

@Xylopyrographer
Copy link

FWIW, on macOS, also need to add the private key to the SSH Agent as per this stack overflow link

By doing this in terminal:
ssh-add --apple-use-keychain ~/.ssh/id_rsa

(Note the command above is different from that provided in the link. It jhas been updated to reflect current version of ssh-add.)

@nbomasi
Copy link

nbomasi commented Jul 5, 2022

Thanks, it solve my issue

@pauGuarch
Copy link

After trying many different solutions this one finally worked. Thanks a lot!

@knkailash
Copy link

Works like a charm, many thanks for this solution suggestion.

@shazib2022skipq
Copy link

Bro, youre a lifesaver. I wasted so much time on setting up the credential manager, but finally your post helped me figure this out. Thanks a lot and lots of love.

@pbatsaihan
Copy link

Thanks

@EmilyYChan
Copy link

Thank you for the great explanation!

@TonniPaul
Copy link

it worked for me but now I have to input my phrase before i could push anything

@Murillo-Henrique-Monteiro-Nacfur

Thanks, it worked.

@mhd-hi
Copy link

mhd-hi commented Aug 31, 2022

It didn't work for me. I had to rename my .pub public file and the private file to id_rsa and it works fine now 🎉🎉 Have a great code!

@apprajapati9
Copy link

what if I don't wanna have the default name like id_rsa. pub? what would be the setup or changes that I have to do if I change key names?

@2bam
Copy link

2bam commented Sep 11, 2022

If you:

  • are using PowerShell or CMD directly
  • don't want to be forced to use id_rsa
  • don't want to input a passphrase on each call to git
  • don't want to use Git Bash because you want to use it from CMD/PowerShell directly
    you need some troubleshooting to use git installation executables and a prior setup:

First of all thanks for this gist, it helped me out when I was at a total loss!!!

1. Start or get currently running agent into env vars (PID and socket/pipe file).
start-ssh-agent.cmd
It should be in PATH, otherwise find it at C:\Program Files\Git\cmd
This command undocumented in the FAQ will spawn a child CMD with the dynamic env vars set. Use it, if you close it will leave the agent on but git calls won't work as wanted.

2. Make sure you're using the ssh executables from the git installation
In PowerShell: Get-Command ssh (this is like which ssh in Linux)
You're probably not using that, so point there before anything else.
In CMD: set PATH=C:\Program Files\Git\usr\bin;%PATH%
In PowerShell: $Env:PATH = "C:\Program Files\Git\usr\bin;$Env:PATH"

3. Only after that load keys using git installation's ssh-add:
ssh-add <PATH_TO_KEY>
And enter the passphrase.

Then you can use git directly from CWD/PowerShell without (so much) troubles. Not only git push but also git clone, etc.
I needed this for some automation and didn't want to spawn a Git Bash per git call, neither wanted to run inside Git Bash.

https://gist.github.com/2bam/33347ff54be8088943450f20323fde53

@andresernajoto
Copy link

Thanks, now it's working correctly!

@TadesseDev
Copy link

Thank you brother, It's working. And for someone curious like me. If you don't like to name your ssh id_pub. You need to re-install git since most of the time the problem is related to the git you have installed or how you install it. For me the problem was, I install git on a different user account and try to use it in another one. 😎

@joel-dev22
Copy link

I added public key to Github...But still i am having the error..Can you explain why brother?

@yusufkarakaya
Copy link

it works! thank you.

@Ekta17
Copy link

Ekta17 commented Sep 29, 2022

this works!

one extra step is to add the rsa key in your path:

touch ~/.ssh/config
vi ~/.ssh/config

paste following in this file and change location of the rsa file:

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

ssh-add -K ~/.ssh/id_rsa

@Volodymyr-Shabala
Copy link

Thanks!

@pfabiszewski
Copy link

Thank you!

@kalinchkma
Copy link

Thank You!

@vaniswamy
Copy link

vaniswamy commented Oct 11, 2022 via email

@Volodymyr-Shabala
Copy link

Hello, I think I’m not the correct audience for this mail chain. If yes , please exclude me from this call. Best Regards, Vani From: Ekta Arora @.> Date: Thursday, 29 September 2022 at 07:30 To: adamjohnson @.> Cc: Vani Swamy @.>, Comment @.> Subject: Re: adamjohnson/publickey-git-error.markdown @Ekta17 commented on this gist.

________________________________ this works! one extra step is to add the rsa key in your path: touch ~/.ssh/config vi ~/.ssh/config paste following in this file and change location of the rsa file: Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa ssh-add -K ~/.ssh/id_rsa — Reply to this email directly, view it on GitHubhttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2F5682757%23gistcomment-4319125&data=05%7C01%7Cvani.swamy%40ingka.ikea.com%7Cd6fe2548628e4f8db1bc08daa1dbc5a9%7C720b637a655a40cf816af22f40755c2c%7C0%7C0%7C638000262552187699%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=rwkWT3KspXxH6erjIjL9gwD3kDudmWZU0iw4KVKwpGc%3D&reserved=0, or unsubscribehttps://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAYDSDCEK2WYHRR7PWI4B62TWAUSQVANCNFSM4HI4HAHA&data=05%7C01%7Cvani.swamy%40ingka.ikea.com%7Cd6fe2548628e4f8db1bc08daa1dbc5a9%7C720b637a655a40cf816af22f40755c2c%7C0%7C0%7C638000262552187699%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=SqRZnNCXjvPN0YuNvdd1WWp23nQELzxKeB5Hjdphjis%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

Hi. There is an unsubscribe button in the email you get that you can press to stop receiving updates from this thread.

@arifbangashse
Copy link

How did you fix it with another user?

@a-malacarne
Copy link

I got the same error when following the key generation guid for Mac on Github docs: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key

The problem for me was the entry in .ssh/config which has to be for host *github.com and not *.github.com as the guide says since *.github.com does not match the ssh domain git@github.com:your-user.... So Git didn't even use the key when trying to push to Github.

@BotanicalAmy
Copy link

THANK YOU! Finally connected PyCharm because of this.

@GeorgeLukoff
Copy link

thanks, you are awesome!!!

@eliascreates
Copy link

This helped. Thank you so much 🙏

@appaloosa-marketing
Copy link

Thank you!

@kdaysal
Copy link

kdaysal commented Nov 24, 2022

Bravo!! I was getting the Permission Denied (publickey) error when trying to execute npm run deploy to publish an app to gh-pages for the first time. Kept going in circles on different forums without much luck. One suggestion was to change the remote's URL from SSH to HTTPS - which I tried, and it worked ONCE (published to gh-pages successfully) - and then some time later I tried to publish new changes and it failed with the same error as before. Such a headache, but the solution described here worked like a charm after I switched my remote URL back to SSH and followed the steps. Thank you so much! :)

@nyamwaya
Copy link

nyamwaya commented Dec 1, 2022

worked for me. had to rename files to id_rsa respectfully

@theo-carr
Copy link

Thank you so much! fixed a problem I was having for a few days...

@Manimit10
Copy link

Very helpful, thanks

@Zyanvel
Copy link

Zyanvel commented Dec 15, 2022

Thank you SO much!

@johannesrave
Copy link

using WSL, this DID solve my problem for windows-based git. however, using WebStorm and a project based in the linux file system, it still wasn't enough and I couldn't get it to work for several months. the solution finally came by wrapping the git that WebStorm (or IntelliJ etc) uses for WSL projects in a bash script that starts the respective ssh-agent, see this thread:
https://youtrack.jetbrains.com/issue/IDEA-275823/Git-from-WSL-Support-SSH-agent-from-WSL

@bassigbc
Copy link

Thanks, it works with Ubuntu on WSL2

@harsh-kp
Copy link

Thanks man, i named my keys id_"some_custom_name" but i renamed it to rsa and it started working fine.
Still i didnt get why it didnt work for custom name. I added the identity in the config file

@santosh2818
Copy link

This is very helpful. Easy steps saved lots of trouble.

@steveninety
Copy link

steveninety commented Jan 3, 2023

WARNING!
Powershell terminal users!

You'll keep getting this error even after following the outlined instructions. It only works in the Bash/Cygwin terminal. In Cygwin, I ran:

ssh -T git@github.com

Result:

Hi steveninety! You've successfully authenticated, but GitHub does not provide shell access.

Find the comment by @2bam where he explains how to get rid of the error in Powershell.

@eprasetyaw
Copy link

thanks. it's work master.

@ranjankhanna
Copy link

I am also running into same issue on my mac. I tried above solution but didn't work. Following is my ~/.ssh/config file:

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

Can anyone please help!

@TiffLSmith
Copy link

TiffLSmith commented Jan 10, 2023

I have this issue and need help! My config file is set up correctly, I can connect to GitHub using the command ssh -T@github.com. I ran the command ssh -vT git@github.com and was able to see my public key connect successfully. However when I run the command ssh-add -l -E sha256 I receive this error message "The agent has no identities." I can not understand how this is possible and any help would be greatly appreciated as I have been working on this issue for 2 weeks. Using a Mac.

@delacrucen
Copy link

Thanks!

@mbgoodguy
Copy link

Thanks! It's really works! <3

@soscarlos
Copy link

It worked! Thanks!

@bitbyalex
Copy link

Thank you! Finally it worked!

@starthecode
Copy link

Not working at all

@LiuXH98
Copy link

LiuXH98 commented Feb 14, 2023

Thank you so much!!!!!

@ArielRobson
Copy link

Thanks!

@lanbufan
Copy link

Thank You! Works like a charm.

@Coding-Noob30
Copy link

you saved me

@FREEDOCTOR1986
Copy link

Thanks man, i named my keys id_"some_custom_name" but i renamed it to rsa and it started working fine.
Still i didnt get why it didnt work for custom name. I added the identity in the config file

hello sir, I also have the same issue how can I rename ? please help

@gowthamkumar12
Copy link

I have an issue I have created a repo and I am trying to clone the repo using ssh, for that I have setup everything up, but when I am trying to clone the repo I am facing this.

`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 don't know where I am going wrong. someone please help me with this

@Divya0319
Copy link

Divya0319 commented Jun 12, 2023

I have an issue I have created a repo and I am trying to clone the repo using ssh, for that I have setup everything up, but when I am trying to clone the repo I am facing this.

`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 don't know where I am going wrong. someone please help me with this

Make sure you have created public and private key pairs properly, by following the steps described in SSH key generation official docs, and also make sure you uploaded your public key in "SSH Keys" section of your GitHub account. I have attached two screenshots from my GitHub account for your reference.
Screenshot 2023-06-12 163242
Screenshot 2023-06-12 163451

@gowthamkumar12
Copy link

gowthamkumar12 commented Jun 12, 2023

Yeah, I have done all of that and I am also able to get this message "Hi gowthamkumar12! You've successfully authenticated, but GitHub does not provide shell access." while doing "ssh -T git@github.com"

@Divya0319
Copy link

Yeah, I have done all of that and I am also able to get this message "Hi gowthamkumar12! You've successfully authenticated, but GitHub does not provide shell access." while doing "ssh -T git@github.com"

For registering newly created SSH keys, you must register your keys (private keys) to SSH agent, instead of requesting shell access of GitHub.

@josehricardo
Copy link

This procedure solved my problem! thanks!!!

@tharaka16
Copy link

Worked like a charm. thank you!!

@bvercelli99
Copy link

Thanks!!

@Vil94
Copy link

Vil94 commented Aug 22, 2023

What a lifesaver, thank you!!

@iamkayshow
Copy link

Thanks it worked!

@pjkitsune
Copy link

Thank you so much! I was stumped!

@Yunishello
Copy link

Thanks so much it worked

@emil-raubach
Copy link

Thank you!

@ScorpQ
Copy link

ScorpQ commented Nov 1, 2023

Thank youuu

@roziqjon1
Copy link

l;,
l

@chronkituss
Copy link

Thank you, it worked!

@sagar03d
Copy link

sagar03d commented Jan 2, 2024

I was having this error on ubuntu, do not forget to add 'sudo' while running ssh-keygen command

@devonkinghorn
Copy link

I had this problem but only with docker-compose build with a git context. Git clone and push still worked though

For some reason when I ran ssh-add, it magically worked. No clue why but hopefully it helps someone.

$ ssh-add ~/.ssh/id_rsa

@0xkarambit
Copy link

Thank you bro, needed this

@Cegt25
Copy link

Cegt25 commented Apr 18, 2024

Gracias, esta solución me funciono perfectamente

@pxpana
Copy link

pxpana commented May 9, 2024

thank you mahn

@purplena
Copy link

extremely appreciated!

@hbolajraf
Copy link

For those who still have the issue, I created a step-by-step tutorial on how to fix the "Permission Denied (publickey)" error. Post : https://hbolajraf.net/posts/Connecting-to-GitHub-and-Pushing-Changes-Using-SSH-on-Windows/

@zcecchetti
Copy link

THIS WORKED!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment