Git: Permission denied (publickey)

If you encountered an error in Git that says “Permission denied (publickey)…” then you need to generate your SSH keys and add it to your Github account(

[sourcecode language=”bash”]
### STEP 1 ###
$ ls -al ~/.ssh # List all your local ssh keys
$ ssh-keygen -t rsa -b 4096 -C “”

Visit for more info.

Enter file in which to save the key (/c/Users/ADMIN/.ssh/id_rsa): /c/Users/ADMIN/.ssh/id_rsa_github
Enter passphrase (empty for no passphrase):
press enter
Enter same passphrase again:
press enter
Your identification has been saved in /c/Users/ADMIN/.ssh/id_rsa_github
Your public key has been saved in /c/Users/ADMIN/.ssh/
The key fingerprint is: SHA256:XXX

[sourcecode language=”bash”]
### STEP 2 ###
$ eval $(ssh-agent) or exec ssh-agent bash (Linux)
$ ssh-add /c/Users/ADMIN/.ssh/id_rsa_github

NEXT.. We need to copy the contents of the id_rsa_github) and paste into your Github account SSH Keys.

NOTE: On most systems the default private keys (~/.ssh/id_rsa, ~/.ssh/id_dsa and ~/.ssh/identity) are automatically added to the SSH authentication agent. You shouldn’t need to run ssh-add path/to/key unless you override the file name when you generate a key.
When you set up an ssh key for github, if it’s not your default key, you will need to add a section to your ~/.ssh/config(create a file if doesn’t exist yet) something like this:

[sourcecode language=”bash”]
### STEP 3 ###
$ vi ~/.ssh/config
# Then add this line in your config file
Host *
 User git
 IdentityFile ~/.ssh/id_rsa_github

Host * # If using the Github Enterprise
 User git
 IdentityFile ~/.ssh/id_rsa_mycompany

Next, Let’s test the connection

[sourcecode language=”bash”]
### STEP 4 ###
$ ssh -vT # OR …
$ ssh -vT # If youre using Github Enterprise

Warning: Permanently added the RSA host key for IP address ‘’ to the list of known hosts.
Hi sudogem! You’ve successfully authenticated, but GitHub does not provide shell access.
If the username in the message is yours, you’ve successfully set up your SSH key!

Finally it works!! You can start cloning any git repositories.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.