git clone from GitLab fails with Permission denied (publickey)
git@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.
Fix it fast
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.com2Match what you see, then jump to its fix
- 1
ssh -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
You 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
It worked before a reboot or in an older terminal, and fails in a new session.The key is not loaded into ssh-agentFix - 4
Keys and folders under ~/.ssh show modes wider than 600 and 700, and the key is not accepted.The key files have loose permissionsFix
- 1
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.pub2Fix
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.pub3Verify
GitLab answers with a welcome message that contains your username.
ssh -T git@gitlab.comConfirmed by docs.gitlab.com
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 identity2Fix
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_gitlab3Verify
The test now offers that file and succeeds.
ssh -T git@gitlab.comConfirmed by docs.gitlab.com
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 -l2Fix
Add the private key to the agent.
ssh-add ~/.ssh/id_ed255193Verify
The key is listed and the test succeeds.
ssh-add -l
ssh -T git@gitlab.comConfirmed by docs.gitlab.com
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_ed255192Fix
Set the modes the guide names.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed255193Verify
The test connects.
ssh -T git@gitlab.comConfirmed by docs.gitlab.com
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
Security
SSH refuses your key and ends with Permission denied (publickey)
user@203.0.113.10: Permission denied (publickey,password).
The server accepted the connection but found no key it would take.
5 causes, each with a fixServers
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix