How to Solve Permission Denied (Publickey) Error in Git

John Wachira Mar 11, 2025 Git Git Error
  1. Understanding the Permission Denied (Publickey) Error
  2. Method 1: Check for Existing SSH Keys
  3. Method 2: Adding Your SSH Key to the SSH Agent
  4. Method 3: Adding Your SSH Key to Your Git Hosting Service
  5. Conclusion
  6. FAQ
How to Solve Permission Denied (Publickey) Error in Git

When working with Git, encountering the “Permission denied (publickey)” error can be frustrating. This error typically occurs when Git is unable to authenticate your identity with the remote repository, usually hosted on platforms like GitHub, GitLab, or Bitbucket. This issue often arises due to misconfigured SSH keys or the absence of the correct key in your SSH agent.

In this article, we will explore effective methods to resolve this error, ensuring you can continue your development work without interruptions. Whether you are a beginner or an experienced developer, understanding how to fix this error will streamline your Git experience and enhance your productivity.

Understanding the Permission Denied (Publickey) Error

Before jumping into solutions, it’s essential to understand what causes the “Permission denied (publickey)” error. This error indicates that your SSH client is unable to authenticate your connection to the remote Git repository. SSH keys are used to establish a secure connection, and if the server does not recognize your key, it will deny access. Common reasons for this error include:

  • Missing SSH key
  • Incorrectly configured SSH key
  • SSH agent not running
  • SSH key not added to your Git hosting service account

By addressing these issues, you can resolve the permission denied error and get back to coding.

Method 1: Check for Existing SSH Keys

The first step in solving the permission denied error is to check if you already have SSH keys generated on your machine. SSH keys are typically stored in the ~/.ssh directory. To check for existing keys, you can use the following command:

ls -al ~/.ssh

Output:

total 16
drwx------  2 user user 4096 Oct 10 10:00 .
drwxr-xr-x  4 user user 4096 Oct 10 10:00 ..
-rw-------  1 user user  400 Oct 10 10:00 id_rsa
-rw-r--r--  1 user user  100 Oct 10 10:00 id_rsa.pub

If you see files named id_rsa and id_rsa.pub, you already have an SSH key pair. The id_rsa file is your private key, while id_rsa.pub is your public key. If these files do not exist, you will need to generate a new SSH key pair.

To generate a new SSH key, use the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Output:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):

After running this command, follow the prompts to create your SSH key pair. Once you have your keys, you can proceed to add your public key to your Git hosting service.

Method 2: Adding Your SSH Key to the SSH Agent

If you already have an SSH key but are still encountering the permission denied error, it may be because your SSH agent is not running or your key is not added to the agent. To check if the SSH agent is running, use the following command:

eval "$(ssh-agent -s)"

Output:

Agent pid 1234

If the agent is not running, this command will start it. Next, you need to add your SSH key to the agent. Use the following command:

ssh-add ~/.ssh/id_rsa

Output:

Identity added: /home/user/.ssh/id_rsa

By adding your SSH key to the agent, you ensure that it is available for authentication when you attempt to connect to your Git repository. This step is crucial, especially if you have multiple keys or if you are using a different key than the default one.

Method 3: Adding Your SSH Key to Your Git Hosting Service

After confirming that your SSH key is generated and added to the SSH agent, the next step is to add your public key to your Git hosting service. This process varies slightly depending on the service you are using, but the general steps are similar.

For example, if you are using GitHub, follow these steps:

  1. Copy your public key to your clipboard using the following command:
cat ~/.ssh/id_rsa.pub

Output:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC...
  1. Log in to your GitHub account.
  2. Navigate to Settings > SSH and GPG keys.
  3. Click on New SSH key.
  4. Paste your public key into the “Key” field and give it a title.
  5. Click Add SSH key.

Once your key is added, try to clone or push to your repository again. If you followed these steps correctly, the permission denied error should be resolved.

Conclusion

The “Permission denied (publickey)” error in Git can be a significant hurdle, but with the right steps, you can quickly resolve it and continue your work. By checking for existing SSH keys, ensuring your SSH agent is running, and adding your public key to your Git hosting service, you can eliminate this issue. Remember that proper configuration of SSH keys is essential for a smooth Git experience, so take the time to ensure everything is set up correctly. With these solutions at your disposal, you can confidently navigate your Git projects without the frustration of authentication errors.

FAQ

  1. What is the “Permission denied (publickey)” error in Git?
    This error indicates that Git cannot authenticate your connection to a remote repository due to an issue with your SSH keys.

  2. How do I check if I have SSH keys on my machine?
    You can check for existing SSH keys by running the command ls -al ~/.ssh in your terminal.

  3. What should I do if I don’t have an SSH key?
    You can generate a new SSH key pair using the command ssh-keygen -t rsa -b 4096 -C "your_email@example.com".

  4. How do I add my SSH key to the SSH agent?
    Start the SSH agent with eval "$(ssh-agent -s)", then add your key using ssh-add ~/.ssh/id_rsa.

  5. How do I add my SSH key to GitHub?
    Copy your public key with cat ~/.ssh/id_rsa.pub, then paste it into the SSH keys section of your GitHub settings.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
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