How to Solve Permission Denied (publickey) Error when Using Git

The "Permission denied" error means that the server rejected the connection. In this tutorial, you will explore how to solve this problem.

Watch a course Git & GitHub - The Practical Guide

If the ssh public/private key pair set is not generated

If the user has not generated an SSH public/private key pair set, firstly, set up your own public/private key pair set. On most systems, you can use ssh-keygen.

Setting up a public/private key pair set

First, cd into your .ssh directory by running the following into the terminal:

cd ~/.ssh && ssh-keygen

Copying to clipboard

Secondly, copy the following to your clipboard:

On Linux execute:

cat | xclip

On OS X execute:

cat | pbcopy

On Windows (via Cygwin/Git Bash) execute:

cat | pbcopycat | clip

On Windows (via Cygwin/Git Bash) execute:

cat | clip

Adding key to account

The next step is adding your key to your account via the website.

  • Set up git config:
    git config --global "w3docs"
    git config --global w3docs@...
  • Restart the command line to be sure that the config is reloaded.

If the ssh public/private key pair set is generated before

If the user has generated an ssh public/private key pair set, then you should follow the steps below.

Checking keys

Check which key have been authorized on your github account settings.

Discovering private key

Determine which private key must be associated from your local computer:

eval $(ssh-agent -s)

Specifying the location

Define the location of keys:

ssh-add ~/.ssh/id_rsa

Then, you can add the SSH key to your account.

Public Key Authentication

Using public key authentication instead of simple passwords is the question of security. The power of public key authentication lies in the provision of cryptographic strength that even long passwords can not offer. The security of public key authentication with SSH free users from remembering long passwords or write them down.

SSH keys are an access credential used in SSH protocol (Secure Shell). A secure shell network protocol helps to log in from one computer to another securely, as well as to manage networks, operating systems, and configurations. The SSH keys are created using the keygen program. The ssh-keygen is a tool for creating new authentication key pairs for SSH. The key pairs are used for automating logins, single sign-on, and for authenticating hosts.

The keys come in pairs, and each of them is composed of a public key and a private key.

  • Public keys (or authorized keys), determine who can access each system.
  • Private keys (or identity keys) identify users and give them access.

The SSH keys are nearly the same as passwords, but it is more secure to use SSH Keys to log in a server because it is almost impossible to decrypt.