How to Solve Permission Denied (publickey) Error when Using Git
This tutorial provides the answers of solving the permission denied error when using Git. Also, get useful information about the public key autentication.
The "Permission denied" error means that the server rejected the connection. In this tutorial, you will explore how to solve this problem.
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 <kbd class="highlighted">.ssh</kbd> directory by running the following into the terminal:
Generate a new SSH key pair
cd ~/.ssh && ssh-keygenCopying to clipboard
Secondly, copy the following to your clipboard:
On Linux execute:
Copy public key to clipboard (Linux)
cat ~/.ssh/id_rsa.pub | xclip -selection clipboardOn OS X execute:
Copy public key to clipboard (OS X)
cat ~/.ssh/id_rsa.pub | pbcopyOn Windows (via Cygwin/Git Bash) execute:
Copy public key to clipboard (Windows)
cat ~/.ssh/id_rsa.pub | clipAdding key to account
The next step is adding your key to your account via the website.
Navigate to your Git provider's (e.g., GitHub, GitLab) SSH key settings page and paste the copied public key.
Restart the command line to ensure the changes take effect.
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 keys have been authorized on your GitHub account settings.
Discovering private key
Determine which private key must be associated from your local computer:
Start the SSH agent
eval $(ssh-agent -s)Specifying the location
Define the location of keys:
Add the SSH private key to the ssh-agent
ssh-add ~/.ssh/id_rsaThen, 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 frees users from remembering long passwords or writing 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.