Skip to content

Instantly share code, notes, and snippets.

@adamjohnson
Last active December 15, 2024 08:41
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.

@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.

@alwa97
Copy link

alwa97 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!!

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