Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

git clone from GitLab fails with Permission denied (publickey)

Errorgit@gitlab.com: Permission denied (publickey).

Short answer

GitLab did not accept any key your computer offered. Test with ssh -T against your GitLab host, then check four things: the public key is in your profile and has not expired, the right private key is chosen, it is loaded into ssh-agent, and the key files have strict permissions.

  • Applies to GitLab.com and GitLab Self-Managed (current), OpenSSH 9, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the SSH test against your GitLab host with verbose output. It shows which key files are tried and whether the server accepts any of them.

    ssh -Tvvv git@gitlab.com
  2. 2Match what you see, then jump to its fix

    1. 1ssh -T prints Permission denied (publickey) and the verbose log offers your key without the server accepting it.The public key is not in your GitLab profile, or it has expiredFix
    2. 2You keep several keys, and the verbose log lists a key file that is not the one you added to GitLab.ssh uses a different private key than the one GitLab knowsFix
    3. 3It worked before a reboot or in an older terminal, and fails in a new session.The key is not loaded into ssh-agentFix
    4. 4Keys and folders under ~/.ssh show modes wider than 600 and 700, and the key is not accepted.The key files have loose permissionsFix

What does the error look like?

  • git@gitlab.com: Permission denied (publickey).

Cause 1 of 4

The public key is not in your GitLab profile, or it has expired

1Check

Compare the fingerprint of the local key with the keys listed under your profile, in the SSH keys page. GitLab lets you set an expiry date on a key, and an expired key can no longer sign in.

ssh-keygen -lf ~/.ssh/id_ed25519.pub

2Fix

Create a key if you have none, then add its public half (the .pub file, never the private one) in GitLab under Edit profile, Access, SSH keys. If the old key expired, generate a new pair and add that.

ssh-keygen -t ed25519 -C "my-laptop"
cat ~/.ssh/id_ed25519.pub

3Verify

GitLab answers with a welcome message that contains your username.

ssh -T git@gitlab.com

Confirmed by docs.gitlab.com

Did this fix it?

Cause 2 of 4

ssh uses a different private key than the one GitLab knows

1Check

By default ssh tries the standard files such as id_ed25519 and id_rsa. A key with another name is not tried unless a config entry points to it.

ssh -Tvvv git@gitlab.com 2>&1 | grep -i identity

2Fix

Add a Host block that names the key file for GitLab.

# ~/.ssh/config
Host gitlab.com
    Hostname gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_gitlab

3Verify

The test now offers that file and succeeds.

ssh -T git@gitlab.com

Confirmed by docs.gitlab.com

Did this fix it?

Cause 3 of 4

The key is not loaded into ssh-agent

1Check

GitLab's troubleshooting page lists a key missing from ssh-agent as a cause that shows up after a reboot or in a new terminal session. Load the key and test again.

ssh-add -l

2Fix

Add the private key to the agent.

ssh-add ~/.ssh/id_ed25519

3Verify

The key is listed and the test succeeds.

ssh-add -l
ssh -T git@gitlab.com

Confirmed by docs.gitlab.com

Did this fix it?

Cause 4 of 4

The key files have loose permissions

1Check

GitLab's guide says the private key should use mode 600 and the .ssh directory mode 700.

ls -ld ~/.ssh && ls -l ~/.ssh/id_ed25519

2Fix

Set the modes the guide names.

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519

3Verify

The test connects.

ssh -T git@gitlab.com

Confirmed by docs.gitlab.com

Did this fix it?

How do I stop it coming back?

  • Give each machine its own key and write the machine name in the key comment.
  • Set an expiry reminder when you add a key, and rotate it before the date.
  • Keep one Host block per Git server in ~/.ssh/config so the key choice is written down.
  • Never copy a private key between machines; add a second public key instead.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.