Fixing Git Error: Permission denied (publickey) – 4 solutions

Updated: January 27, 2024 By: Guest Contributor Post a comment

Understanding the Git Publickey Error

The ‘Permission denied (publickey)’ error in Git stems from an authentication issue between your local machine and the Git server, specifically related to SSH public key authentication failures. This error can occur when trying to push to or pull from a Git repository without a proper SSH key setup or with incorrect SSH configurations.

Solution 1: Verify SSH Keys Exist

Before proceeding with any advanced troubleshooting, ensure that an SSH key exists on your machine and is added to the ssh-agent.

  1. Open your terminal.
  2. Enter ls -al ~/.ssh to list all files in your .ssh directory.
  3. Look for files named id_rsa.pub or id_ecdsa.pub.

If the key exists, move on to adding it to the ssh-agent. Otherwise, you must generate a new SSH key.

Example:

$ ls -al ~/.ssh
# Lists the files in your .ssh directory

total 16
drwx------   5 username  staff  160 Mar 29 10:00 .
drwxr-xr-x+ 54 username  staff 1728 Mar 29 09:47 ..
-rw-------   1 username  staff 1679 Mar 29 09:57 id_rsa
-rw-r--r--   1 username  staff  403 Mar 29 09:57 id_rsa.pub

If you see id_rsa.pub or another public key file, your SSH key exists.

Notes: Having SSH keys doesn’t guarantee they are added to the Git server or are correctly configured. You must also ensure the public key is correctly set up on the Git server.

Solution 2: Add SSH Key to ssh-agent

If an SSH key exists but still faces the error, you may need to add the key to the ssh-agent, a program that holds private keys used for public key authentication.

  1. Start the ssh-agent in the background with the command eval "$(ssh-agent -s)".
  2. Add your SSH private key to the ssh-agent using ssh-add ~/.ssh/id_rsa.

Example:

$ eval "$(ssh-agent -s)"
Agent pid 59566

$ ssh-add ~/.ssh/id_rsa
Identity added: /Users/username/.ssh/id_rsa ([email protected])

If successful, you should see a message stating your identity was added.

Notes: You can automate this process by adding your SSH key to the ssh-agent and storing your passphrase in the keychain.

Solution 3: Generate a New SSH Key and Add It to the Git Server

If no SSH key is present or you want to create a new one, follow these steps to generate and add it to the Git server.

  1. Generate a new SSH key using ssh-keygen -t rsa -b 4096 -C "[email protected]".
  2. When prompted, press Enter to accept the default file location and file name. Optionally, provide a passphrase for extra security.
  3. Start the ssh-agent using eval "$(ssh-agent -s)".
  4. Add the new SSH key to your agent with ssh-add ~/.ssh/id_rsa.
  5. Log in to your Git server and navigate to SSH key settings.
  6. Copy the contents of your public key file with pbcopy < ~/.ssh/id_rsa.pub (this command may vary depending on your OS).
  7. Paste your key into the appropriate field on the Git server and save it.

Notes: Consider using a passphrase for additional security on your SSH keys. However, you will need to enter this passphrase whenever you use the key unless you use an ssh-agent.

Solution 4: Check SSH Configuration

Your SSH configuration might not be correctly pointing to your SSH key or may have incorrect settings.

  1. Open or create the SSH config file with nano ~/.ssh/config.
  2. Ensure it includes the following:
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa

Replace github.com with your Git server and id_rsa with the name of your private key if different.

Notes: Make sure the SSH configuration matches the host you are trying to connect to, and the User typically should be ‘git’.

Conclusion

Authentication errors with Git via SSH can be frustrating, but troubleshooting them usually involves checking your SSH key setup. By verifying existing keys, ensuring the ssh-agent has them, generating new keys when necessary, and checking your SSH configurations, you can usually resolve ‘Permission denied (publickey)’ errors and get back to work quickly.