How to Solve Permission Denied (Publickey) Error in Git

John Wachira Feb 02, 2024
  1. Set Up a Public/Private Key Pair Set in Git
  2. Add Key to Account
  3. Check Private Key
How to Solve Permission Denied (Publickey) Error in Git

This article demonstrates the steps you can follow to solve the Permission denied (publickey) error in Git. If you are a regular Git user, you may encounter such an error while cloning a remote repository.

In such a case, it simply means that the server has rejected your request. How do you solve it?

Set Up a Public/Private Key Pair Set in Git

Open your terminal and run the command below to access your .ssh directory.

$ cd ~/.ssh && ssh-keygen

Copy one of the following commands based on your system and execute.

On Linux:

cat id_rsa.pub | xclip

On OS X:

cat id_rsa.pub | pbcopy

On Windows:

cat id_rsa.pub | pbcopycat id_rsa.pub | clip

And

cat id_rsa.pub | clip

Add Key to Account

Now you can add your key to your account. You can do this by making a few tweaks to your .config file, as shown below.

$ git config --global user.name <Your-Name>
$ git config --global user.email <Your-Email>

The next steps are below if you have already generated a public/private key.

Check Private Key

The command below determines which private key corresponds with your local computer.

eval $(ssh-agent -s)

Run the command below to define the location of your keys.

ssh-add ~/.ssh/id_rsa

As we illustrated earlier, you can now configure your SSH key by editing the .config file.

Note
  1. Public keys, also called authorized keys, determine who can access a system.
  2. Private keys, also called identity keys, identify users and grant them access to a system.

In a nutshell, SSH keys are similar to passwords but are more secure since they are extremely hard to decrypt. If you get the Permission denied (publickey) in Git, you need to configure the keys in the .config file as discussed above.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Error