Jun 30, 2020

How to solve "Permission denied (publickey)" error in Git

The Permission denied (publickey) error means your local SSH key either doesn’t exist, isn’t loaded, or hasn’t been added to your Git host (GitHub, GitLab, etc.).

Steps

Step 1: Check if you already have an SSH key

1
ls -al ~/.ssh

Look for files named id_rsa / id_rsa.pub or id_ed25519 / id_ed25519.pub. If they exist, skip to Step 3.

Step 2: Generate a new SSH key

1
ssh-keygen -t ed25519 -C "your_email@example.com"

Press Enter to accept the default file location and optionally set a passphrase.

Step 3: Add the key to the SSH agent

1
2
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Step 4: Copy your public key

1
cat ~/.ssh/id_ed25519.pub

Copy the entire output.

Step 5: Add the key to GitHub / GitLab

  • GitHub: Settings → SSH and GPG keys → New SSH key → paste and save
  • GitLab: Preferences → SSH Keys → paste and save

Step 6: Test the connection

1
ssh -T git@github.com

You should see: Hi username! You've successfully authenticated...

Reference

Stack Overflow: How to solve Permission denied (publickey)