Skip to content
Permission denied (publickey)

Permission denied (publickey)

DodaTech 3 min read

Git’s Permission denied (publickey) error means your SSH key wasn’t accepted by the remote server (GitHub, GitLab, Bitbucket, etc.), so the connection was refused before Git could authenticate you.

What It Means

When you use an SSH remote URL like git@github.com:user/repo.git, Git connects via SSH. SSH uses public-key cryptography: your machine holds a private key, and the server holds a matching public key. If the server doesn’t recognize your public key, or if SSH can’t find your private key, you get Permission denied (publickey).

Why It Happens

  • You haven’t generated an SSH key pair yet.
  • Your public key isn’t added to your GitHub/GitLab/Bitbucket account.
  • The ssh-agent isn’t running or doesn’t have your key loaded.
  • You’re using the wrong SSH key (e.g., a deploy key when you need a user key).
  • Your SSH config is pointing to the wrong key file.

How to Fix It

1. Generate a new SSH key pair

ssh-keygen -t ed25519 -C "your-email@example.com"

Press Enter to accept the default file location (~/.ssh/id_ed25519). Optionally add a passphrase for extra security.

2. Start the ssh-agent and add your key

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

3. Copy your public key

cat ~/.ssh/id_ed25519.pub

Select and copy the entire output — it starts with ssh-ed25519 (or ssh-rsa) and ends with your email.

4. Add the key to your Git provider

  • GitHub: Settings → SSH and GPG keys → New SSH key → paste and save.
  • GitLab: Preferences → SSH Keys → Add key → paste and save.
  • Bitbucket: Personal settings → SSH keys → Add key → paste and save.

5. Test the connection

ssh -T git@github.com

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

6. Verify your remote URL uses SSH

git remote -v

Make sure the URL starts with git@ (SSH), not https://. If it’s HTTPS, switch to SSH:

git remote set-url origin git@github.com:user/repo.git
What's the difference between SSH and HTTPS for Git?
SSH uses key-based authentication (no password prompt after setup), while HTTPS requires a personal access token or password. SSH is preferred for frequent command-line use. HTTPS works better behind corporate firewalls that block port 22. You can switch between them with git remote set-url.
What if I have multiple SSH keys?
Create or edit ~/.ssh/config and specify which key to use per host. For example: Host github.com HostName github.com IdentityFile ~/.ssh/github-personal and a separate entry for Host github-work HostName github.com IdentityFile ~/.ssh/github-work. Then use the appropriate host alias in your remote URL.
Does re-installing the OS delete my SSH keys?
Yes — SSH keys are stored in ~/.ssh/ and are lost when the home directory is wiped. Always back up your keys (if needed) or regenerate them after a fresh install. Your public key can be safely stored anywhere; your private key must be kept secret and never shared.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro