Permission denied (publickey)

Fix Git 'Permission Denied Publickey' Clone Error on Linux

Linux & Unix Beginner 👁 9 views 📅 Jun 24, 2026

You tried to clone a repo and got a publickey error. It's usually a missing SSH key or wrong remote URL. Here's how to fix it fast.

First Check (30 seconds) — Is Your Public Key Uploaded?

I know seeing 'Permission denied (publickey)' makes you want to throw your laptop out the window. Happened to me on a Friday afternoon once. Deep breath. Let's fix it.

The most common reason: you haven't added your SSH public key to your Git provider (GitHub, GitLab, Bitbucket). Do this first.

  1. Open a terminal.
  2. Run cat ~/.ssh/id_rsa.pub (or cat ~/.ssh/id_ed25519.pub if you use ED25519). If you see a long string starting with 'ssh-rsa' or 'ssh-ed25519', that's your public key.
  3. Copy the entire output (from ssh-rsa to the email at the end).
  4. Go to your Git provider's settings:
    • GitHub: Settings → SSH and GPG keys → New SSH key
    • GitLab: Preferences → SSH Keys
    • Bitbucket: Personal settings → SSH keys → Add key
  5. Paste the key, give it a name (like 'my laptop'), save.

Now try cloning again. If it works, you're done. If not, move to step 2.

Second Check (5 minutes) — Is Your SSH Agent Running and Key Loaded?

Sometimes your key is uploaded, but the SSH agent (a program that holds your private key in memory) isn't running, or your key isn't loaded. This tripped me up the first time too.

  1. Check if SSH agent is running: echo $SSH_AGENT_PID. If nothing shows up, start it: eval "$(ssh-agent -s)".
  2. Add your private key: ssh-add ~/.ssh/id_rsa (or ssh-add ~/.ssh/id_ed25519). Enter your passphrase if you set one.
  3. Test the connection: ssh -T git@github.com (or git@gitlab.com, git@bitbucket.org). You should see a welcome message like 'Hi username! You've successfully authenticated.'

If that works, clone again. If not, keep going.

Third Check (5 minutes) — Wrong Git Remote URL?

You might be using an HTTPS URL instead of SSH. Or your repo is on a different provider than you think. I've done this — thought I was cloning from GitHub, but the URL pointed to GitLab.

  • Check your remote URL: git remote -v (if you already cloned) or look at the clone button on the repo page.
  • The SSH URL looks like git@github.com:username/repo.git. The HTTPS URL looks like https://github.com/username/repo.git.
  • Make sure you're using an SSH URL (starts with git@). If you're using HTTPS, this error won't happen, so you're probably already using SSH.

Still stuck? Move to the advanced fix.

Advanced Fix (15+ minutes) — Regenerate Your SSH Key

If none of the above worked, your SSH key might be corrupted or you're using the wrong algorithm. Some providers now require ED25519 keys (more secure, faster).

  1. Back up your old keys (just in case): mkdir ~/old-ssh-keys && mv ~/.ssh/id_* ~/old-ssh-keys/
  2. Generate a new key: ssh-keygen -t ed25519 -C "your_email@example.com". Press Enter to accept default location, set a passphrase (or leave empty).
  3. Display your new public key: cat ~/.ssh/id_ed25519.pub
  4. Add it to your Git provider (same steps as step 1).
  5. Add the key to your agent: ssh-add ~/.ssh/id_ed25519
  6. Test the connection: ssh -T git@github.com

If you still get the error, there might be a config issue. Check your SSH config file:

cat ~/.ssh/config

Add these lines if missing:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

Save, test again. If nothing works, your firewall or proxy might block SSH on port 22. Try cloning over HTTPS instead (just use the HTTPS URL). Or talk to your IT team — they might have disabled SSH access.

One More Thing (for experts)

If you're on a corporate network, they might use a custom SSH port. Check your repo URL — some companies use ssh://git@company.com:2222/repo.git. Adjust your SSH config accordingly.

Good luck. You'll get it.

Was this solution helpful?