Fix "Permission denied (publickey)" with Git and GitHub
[email protected]: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
- [email protected]: Permission denied (publickey,keyboard-interactive).
- [email protected]: Permission denied (publickey).
- ERROR: Repository not found. fatal: Could not read from remote repository.
- Load key "/home/user/.ssh/id_ed25519": bad permissions
- Permissions 0644 for '/home/user/.ssh/id_ed25519' are too open.
The Git server accepted none of the SSH keys your computer offered. Create a key with ssh-keygen -t ed25519, load it into the SSH agent, add the public key to your GitHub account, then check with ssh -T [email protected].
What this error means
With a URL like [email protected]:user/repo.git, Git uses SSH. The SSH client offers one or more private keys found in ~/.ssh or in the agent; the server looks for the matching public key in its accounts. Permission denied (publickey) means key authentication, the only accepted method, failed: no key was sent, the server does not know the key, or the key belongs to an account without access to the repository. The Could not read from remote repository line that follows is the Git-side consequence, not a separate error.
Common causes
- No SSH key, or key not added to the account: new machine, or a key generated but never pasted into GitHub > Settings > SSH and GPG keys.
- Key not loaded in the agent, or a non-standard file name that SSH does not try automatically.
- Wrong account: several GitHub accounts (personal / work) and SSH offers the key of the one without access to the repository.
- Command run with
sudo: root uses its own keys (/root/.ssh), not yours. - Permissions too open on
~/.sshor the private key: SSH ignores the key ("bad permissions", "are too open").
Step-by-step fix
-
Test the SSH connection
Without
sudo. If everything works, GitHub replies "Hi user! You’ve successfully authenticated, but GitHub does not provide shell access." The-voption shows which keys are tried.ssh -T [email protected] ssh -vT [email protected] 2>&1 | grep -E "Offering|Authenticated|identity file" -
Check for or create a key
Look for an
id_ed25519/id_ed25519.pubpair. If there is none, create it and protect it with a passphrase. The command is the same on macOS, Linux and Windows (PowerShell).ls -al ~/.ssh ssh-keygen -t ed25519 -C "[email protected]" -
Load the key into the agent (macOS / Linux)
On macOS,
--apple-use-keychainstores the passphrase in the Keychain; also add the~/.ssh/configblock to make it automatic. On Linux, just usessh-add ~/.ssh/id_ed25519.eval "$(ssh-agent -s)" ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # ~/.ssh/config Host github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519 -
Load the key into the agent (Windows)
In PowerShell run as administrator, enable the OpenSSH Authentication Agent service, then add the key from a normal PowerShell.
Get-Service ssh-agent | Set-Service -StartupType Automatic Start-Service ssh-agent ssh-add "$env:USERPROFILE\.ssh\id_ed25519" -
Add the public key to GitHub
Copy the
.pubfile (never the private key) and paste it into GitHub > Settings > SSH and GPG keys > New SSH key. With GitHub CLI, one command is enough. For an organisation repository with SSO, then click "Configure SSO" to authorise the key.# macOS pbcopy < ~/.ssh/id_ed25519.pub # Linux cat ~/.ssh/id_ed25519.pub # Windows (PowerShell) Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard # or with GitHub CLI gh ssh-key add ~/.ssh/id_ed25519.pub --title "my-laptop" -
Fix permissions (macOS / Linux)
If SSH reports "bad permissions" or "are too open", restrict access to the folder and the private key. Never make a private key world-readable.
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_ed25519 chmod 644 ~/.ssh/id_ed25519.pub -
Alternative: switch to HTTPS
If you cannot use SSH (corporate network, shared machine), switch the repository to HTTPS and sign in with
gh auth loginor Git Credential Manager. GitHub no longer accepts account passwords over HTTPS: you need a token or the credential manager.git remote set-url origin https://github.com/user/repo.git gh auth login
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- With several accounts, declare one alias per account in
~/.ssh/config(e.g.Host github-workwith its ownIdentityFileandIdentitiesOnly yes) and use it in the repository URL. - Never run
gitwithsudoin your working folder; fix the folder ownership instead. - Protect each key with a passphrase and remove from GitHub the keys of machines you no longer use.
Frequently asked questions
Port 22 is blocked on my network, what can I do?
GitHub accepts SSH on port 443 through ssh.github.com. Add to ~/.ssh/config: Host github.com, Hostname ssh.github.com, Port 443, User git, then test again with ssh -T [email protected].
ssh -T works but git push still fails, why?
Either the repository uses an HTTPS URL (check git remote -v), or the key belongs to an account without write access to that repository, or the organisation requires SSO authorisation of the key.
RSA or Ed25519?
Ed25519 is recommended: shorter, fast and secure. If an old server does not support it, use ssh-keygen -t rsa -b 4096.
Useful tools for this error
More Git & GitHub errors
- Fix "failed to push some refs" (! [rejected] fetch first) in Git
- Fix "fatal: refusing to merge unrelated histories" in Git
- Get out of "detached HEAD" state in Git without losing commits
- Fix "Your local changes to the following files would be overwritten by merge" in Git
- Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git
- Fix "Support for password authentication was removed" on GitHub
- Fix "GH001: Large files detected" (file over 100 MB) on GitHub