How to Solve Permission Denied (Publickey) Error in Git
- Understanding the Permission Denied (Publickey) Error
- Method 1: Check for Existing SSH Keys
- Method 2: Adding Your SSH Key to the SSH Agent
- Method 3: Adding Your SSH Key to Your Git Hosting Service
- Conclusion
- FAQ

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:
- Copy your public key to your clipboard using the following command:
cat ~/.ssh/id_rsa.pub
Output:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC...
- Log in to your GitHub account.
- Navigate to Settings > SSH and GPG keys.
- Click on New SSH key.
- Paste your public key into the “Key” field and give it a title.
- 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
-
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. -
How do I check if I have SSH keys on my machine?
You can check for existing SSH keys by running the commandls -al ~/.ssh
in your terminal. -
What should I do if I don’t have an SSH key?
You can generate a new SSH key pair using the commandssh-keygen -t rsa -b 4096 -C "your_email@example.com"
. -
How do I add my SSH key to the SSH agent?
Start the SSH agent witheval "$(ssh-agent -s)"
, then add your key usingssh-add ~/.ssh/id_rsa
. -
How do I add my SSH key to GitHub?
Copy your public key withcat ~/.ssh/id_rsa.pub
, then paste it into the SSH keys section of your GitHub settings.
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.
LinkedInRelated Article - Git Error
- How to Fix: Git Is Not Recognized as an Internal or External Command Error
- How to Resolve Git Status Unmerged Paths
- Bower: ENOGIT Git Is Not Installed or Not in the PATH
- How to Fix Another Git Process Seems to Be Running in This Repository Error
- How to Fix Fatal: Origin Does Not Appear to Be a Git Repository Error in Git
- How to Fix Fatal: The Current Branch Master Has No Upstream Branch Error in Git