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
- No SSH Key: You might not have an SSH key set up in your system.
- Wrong SSH Key: The SSH key on your system doesn’t match any of the keys stored in the Git repository’s settings.
- SSH Key Not Added to SSH Agent: Sometimes, even if you have an SSH key, it might not be added to the SSH agent.
- 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:
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:
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:
If you receive an error saying "Could not open a connection to your authentication agent," you may need to start the agent manually:
4. Set Correct File Permissions
SSH keys need to have strict permissions set. Correct these permissions using:
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:
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 Item | Command/Step |
| Check for existence of SSH keys | ls -al ~/.ssh |
| Generate SSH keys if absent | ssh-keygen -t rsa -b 4096 |
| Add SSH public key to Git server | Copy content of id_rsa.pub to Git server |
| Add SSH key to SSH agent | ssh-add ~/.ssh/id_rsa |
| Set correct file permissions | chmod 400 ~/.ssh/id_rsa |
| Check/modify SSH configuration | Modify ~/.ssh/config as needed |
| Verify SSH connection | ssh -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.

