Git
Permission Denied
PublicKey Error
Troubleshooting
Programming Solutions

Git, How to solve Permission denied (publickey) error when using Git?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When using Git, especially when interacting with remote repositories (like those hosted on GitHub, GitLab, Bitbucket, etc.), you might encounter the Permission denied (publickey) error. This common issue often occurs during operations like git clone, git fetch, git pull, or git push, where authentication between your local system and the remote server fails due to issues related to SSH keys. Understanding and solving this problem involves several steps and checks.

Understanding SSH and Public Key Authentication

SSH (Secure Shell) is a protocol used to securely access one computer from another over an unsecured network. It relies on public key cryptography where a pair of keys is used: a public key that can be shared with others, and a private key that is kept secret on your computer.

When you attempt to connect to a Git repository, the server uses the public key to create a challenge that can only be answered with the corresponding private key. If your local machine can provide the correct response using the private key, the server allows access, thereby confirming your identity without transmitting sensitive information.

Common Causes of the Permission denied (publickey) Error

  1. No SSH Key: You might not have an SSH key set up in your system.
  2. Wrong SSH Key: The SSH key on your system doesn’t match any of the keys stored in the Git repository’s settings.
  3. SSH Key Not Added to SSH Agent: Sometimes, even if you have an SSH key, it might not be added to the SSH agent.
  4. Incorrect File Permissions: SSH keys need to have specific file permissions to be considered valid and not ignored by SSH.

Steps to Solve the Error

1. Check if SSH keys are present

First, check if you have SSH keys generated on your computer:

bash
ls -al ~/.ssh

Look for files named id_rsa and id_rsa.pub (or id_ecdsa, id_ecdsa.pub, etc., depending on the encryption algorithm). If these files do not exist you need to create them using:

bash
ssh-keygen -t rsa -b 4096

2. Ensure the public key is added to the Git server

Make sure the public key (id_rsa.pub) is added to your user profile on the Git server. This process varies by platform (GitHub, GitLab, Bitbucket) but generally involves pasting the content of id_rsa.pub into the appropriate section of your account settings.

3. Add the SSH Key to the SSH Agent

Ensure your SSH key is added to the SSH agent which manages your keys:

bash
ssh-add ~/.ssh/id_rsa

If you receive an error saying "Could not open a connection to your authentication agent," you may need to start the agent manually:

bash
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa

4. Set Correct File Permissions

SSH keys need to have strict permissions set. Correct these permissions using:

bash
chmod 400 ~/.ssh/id_rsa

5. Check the SSH Configuration

If you have multiple SSH keys or a non-standard configuration, make sure your SSH is configured to use the correct key. This can be specified in the ~/.ssh/config file:

bash
1Host github.com
2  HostName github.com
3  User git
4  IdentityFile ~/.ssh/id_rsa

Make sure to replace github.com and ~/.ssh/id_rsa with your respective server and key.

6. Verify the Connection

Finally, verify your SSH connection to the Git server:

Replace [email protected] with the appropriate SSH URL. You should receive a welcome message if everything is configured correctly.

Summary Table of Key Actions

Action ItemCommand/Step
Check for existence of SSH keysls -al ~/.ssh
Generate SSH keys if absentssh-keygen -t rsa -b 4096
Add SSH public key to Git serverCopy content of id_rsa.pub to Git server
Add SSH key to SSH agentssh-add ~/.ssh/id_rsa
Set correct file permissionschmod 400 ~/.ssh/id_rsa
Check/modify SSH configurationModify ~/.ssh/config as needed
Verify SSH connectionssh -T [email protected]

By following these steps and ensuring each component is correctly set up, you should be able to resolve the Permission denied (publickey) error and successfully authenticate with your Git repository via SSH.


Course illustration
Course illustration

All Rights Reserved.