TechEarl

Git: 'Permission denied (publickey)' - How to Fix It

The 'Permission denied (publickey)' error means SSH could not prove who you are to GitHub. Here is the ordered checklist that fixes it: key, agent, GitHub, test.

Ishan KarunaratnePublished ⏱️ 10 min readUpdated
Share thisCopied
Fixing the Git Permission denied publickey SSH authentication error with GitHub

Permission denied (publickey) means SSH could not prove to GitHub that you are who you say you are. GitHub asked for a key, your machine either had none to offer or offered one GitHub does not recognize, so the connection was refused. The fix is to make sure you have an SSH key, that it is loaded into your SSH agent, and that its public half is attached to your GitHub account.

Here is the full error you are probably staring at:

text
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Despite the second paragraph, this is almost never about access rights or a missing repository. It is an SSH authentication failure, full stop. Work the checklist below in order and you will land on the broken step.

Why this happens

When your remote URL starts with git@github.com:, Git talks to GitHub over SSH. SSH authenticates you with a key pair: a private key that stays on your machine and a public key you give to GitHub. GitHub never asks for a password on an SSH remote. It only checks: "does this person hold a private key whose public half I have on file?" If the answer is no, you get Permission denied (publickey).

So there are three usual things that can be wrong, and they map to three checks:

  1. You do not have an SSH key.
  2. You have a key, but SSH is not offering it (it is not in your SSH agent, and not at a default path or named in ~/.ssh/config).
  3. You have a key and SSH is offering it, but GitHub does not have the matching public key on your account.

GitHub's own troubleshooting page lists a few rarer causes as well: connecting as a user other than git (a remote like yourname@github.com:... is refused with this exact error), connecting to the wrong host, and running Git with sudo, which uses root's keys instead of yours. I come back to those in the wrong-remote section below.

If you are not even sure you want SSH here, the SSH vs HTTPS remotes comparison lays out the trade-off; switching to an HTTPS remote sidesteps this error entirely. But if you are committed to SSH, keep reading.

Step 1: Do you have an SSH key?

To find out whether you already have an SSH key for GitHub, list the ~/.ssh directory and look for a key pair such as id_ed25519 (private) and id_ed25519.pub (public), or the older id_rsa / id_rsa.pub. If the pair is there, you have an SSH key and can skip to Step 2. If the directory does not exist or is empty, there is no key at the default on-disk location. That is the usual cause of Permission denied (publickey) on a new machine, but SSH can also get a key from an agent, a hardware token, or a path named with ssh -i or in the SSH config, so if you expect a key to exist somewhere else, run ssh-add -l (Step 2) and ssh -vT git@github.com before you generate a new one.

bash
ls -al ~/.ssh

If there is no key pair, generate one:

bash
ssh-keygen -t ed25519 -C "your_email@example.com"

Press Enter to accept the default file location. Set a passphrase if you want one (recommended). This writes ~/.ssh/id_ed25519 and ~/.ssh/id_ed25519.pub. If you want the full cross-platform walkthrough, including the Windows and macOS specifics, I cover how to create an SSH key from scratch in its own guide.

One mistake worth naming: do not run ssh-keygen with sudo. That writes a root-owned key into /root/.ssh that your normal user cannot read, and you will keep hitting the same error wondering why a key you "just made" does nothing. Generating your own key is a user-scoped task. No sudo.

If ssh-keygen is not found, install OpenSSH (it ships by default on macOS and most Linux; on Windows use Git Bash, which bundles it).

Step 2: Is the key loaded into the agent?

The SSH agent holds your private key in memory so SSH can offer it without prompting for the passphrase every time. Start the agent and add your key:

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

Confirm the key is loaded:

bash
ssh-add -l

That lists the fingerprints the agent currently holds. If it prints The agent has no identities, the agent is holding no keys. That is not always fatal: OpenSSH also tries key files at the default paths (~/.ssh/id_ed25519, ~/.ssh/id_rsa, ~/.ssh/id_ecdsa) and any IdentityFile named in ~/.ssh/config, asking for the passphrase each time. But a key saved under a custom file name is offered only if something points SSH at it: the agent, an IdentityFile line in the config, ssh -i, or a Git setting that supplies the SSH command (core.sshCommand or the GIT_SSH_COMMAND environment variable). An empty agent plus no usable key file gives SSH nothing to offer GitHub, which produces the exact error you are seeing. Re-run the ssh-add line above, then check what SSH really offers with ssh -vT git@github.com (see Reading the verbose output).

On macOS, you usually want the agent to load the key automatically and pull the passphrase from the Keychain. Add this to ~/.ssh/config:

text
Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Then add it to the Keychain once:

bash
ssh-add --apple-use-keychain ~/.ssh/id_ed25519   # macOS Monterey (12) and later
# On older macOS (before 12) use the short form:
ssh-add -K ~/.ssh/id_ed25519

Step 3: Is the public key on your GitHub account?

This is the step people skip, and it is the most common cause. Having a key locally does nothing if GitHub has never seen the public half. Print your public key:

bash
cat ~/.ssh/id_ed25519.pub

Copy the entire line (it starts with ssh-ed25519 and ends with your email comment). Then in GitHub: Settings, SSH and GPG keys, New SSH key. Paste it, give it a title, and save. I wrote a step-by-step walkthrough of exactly this part with screenshots in how to add an SSH key to GitHub; use it if the GitHub UI bit is where you are stuck.

A subtle trap: if you have several keys, make sure the public key you paste into GitHub matches the private key the agent is offering. Mismatched pairs fail silently with the same Permission denied (publickey) message.

GitLab works the same way: the same .pub file goes into its Settings, SSH Keys panel, and the test command becomes ssh -T git@gitlab.com.

Step 4: Test the connection

Once the key exists, is loaded, and is on GitHub, verify the whole chain with GitHub's test endpoint:

bash
ssh -T git@github.com

A working setup answers like this:

text
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

That "does not provide shell access" line is the success message, not an error: GitHub deliberately does not give you a shell. Seeing your username there means SSH authentication to your GitHub account works. It does not prove you can write to a particular repository: you still need access to that repo, and an organization that uses SAML single sign-on also requires you to authorize the key for that organization (GitHub Settings, SSH and GPG keys, Configure SSO). If you instead get Permission denied (publickey) from this command too, you have isolated the problem to SSH itself (not Git), and one of Steps 1 to 3 is still wrong.

Reading the verbose output

If the test still fails after the checklist, ask SSH to narrate what it is doing:

bash
ssh -vT git@github.com

Two lines tell you most of the story. Watch for the key SSH actually offers:

text
debug1: Offering public key: /Users/you/.ssh/id_ed25519 ED25519 ...
debug1: Authentications that can continue: publickey

If you see Offering public key followed by GitHub continuing to ask for publickey, the key being offered is not the one on your account: back to Step 3. If you see no Offering public key line at all, SSH found no key to offer: the agent is empty and there is no key file at a default path or at the IdentityFile your config names. Back to Step 2.

Quick reference

SymptomWhat it meansFix
~/.ssh is emptyNo key existsssh-keygen -t ed25519 (Step 1)
ssh-add -l says no identitiesKey not loaded in agentssh-add ~/.ssh/id_ed25519 (Step 2)
Key offered, GitHub still refusesPublic key not on account, or wrong keyAdd the matching .pub to GitHub (Step 3)
ssh -T works, git push still fails with publickeyThe push uses a different user, host alias or key than the test didCheck git remote -v; the URL should start with git@github.com:

When the wrong remote is the real cause

If ssh -T git@github.com greets you by name but git push still throws Permission denied (publickey), the push is not making the same SSH connection your test did. Check where it really goes:

bash
git remote -v
git remote get-url --push origin

Compare the URL with the git@github.com you tested:

  • A user other than git. A remote like yourname@github.com:user/repo.git is refused with this exact error. GitHub only accepts SSH connections as the git user.
  • A host alias. A remote like git@github-work:user/repo.git goes through a Host github-work block in ~/.ssh/config, which can name a different IdentityFile. Test that alias directly with ssh -T git@github-work.
  • sudo git push. Under sudo, SSH looks for root's keys, not yours. Do not use sudo with Git.

An HTTPS remote (https://github.com/user/repo.git) is a different case: it never uses your SSH key, so it cannot produce this error, but it also gets no benefit from the key you just set up. To switch a remote to SSH with the standard git user:

bash
git remote set-url origin git@github.com:user/repo.git

If there is no remote at all yet, git remote -v prints nothing; in that case you have not wired up the remote, and the path to fix it is to add Git to a project and wire up the remote before any of this SSH troubleshooting applies.

This is also worth knowing because GitHub removed password authentication for HTTPS years ago, which trips people up in a different way; I cover that in the password authentication removed fix. The two errors look unrelated but both come down to "how are you proving who you are."

Where this sits in the bigger picture

Getting SSH working is part of the foundation for everything else: cloning, pushing to a branch with no upstream, and opening your first pull request. If you are early in your Git journey, the Git for beginners guide is the hub that ties these pieces together and points to the rest of the series.

Almost never. The wording suggests an access problem, but the error is an SSH authentication failure that happens before GitHub even checks repository permissions. It means SSH could not prove who you are, usually because the key is missing, not loaded into the agent, or not added to your GitHub account.

That message is the success response, not an error. GitHub intentionally does not give SSH users a shell. If the line starts with your username and confirms you have authenticated successfully, SSH authentication to your GitHub account is working. Pushing and pulling then depend only on your access to the specific repository, including authorizing the key for any organization that uses SAML single sign-on.

Use ed25519. It is shorter, faster, and is the modern recommendation. RSA still works if you already have an RSA key on your account, but for a new key there is no reason to choose RSA over ed25519.

Yes. An HTTPS remote authenticates with a personal access token rather than an SSH key, so it never produces this particular error. The trade-off is that you manage a token instead of a key pair. Either approach is valid; pick the one you find easier to keep working.

Sources

Authoritative references this article was fact-checked against.

Tagsgit permission denied publickeyGitVersion ControlSSHGitHub

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts