// troubleshooting

How to fix “Permission denied (publickey)” on a Mac

The error means the server didn't accept any key your Mac offered. Here's how to find out which key SSH actually sent, and the fix for each of the seven usual causes.

Updated · 3 min read · by Pixel Worship, LLC

Permission denied (publickey) is SSH's way of saying: I offered keys, the server rejected all of them, and password login is off. The fix is almost never "make a new key". It's finding out which key SSH actually offered and why the server didn't like it.

#Step 1: see which key SSH actually offers

Run the connection verbosely. For GitHub:

Terminal
ssh -vT git@github.com 2>&1 | grep -E "Offering|Authenticated|identity file|denied"

You'll see lines like these:

Output
debug1: identity file /Users/you/.ssh/id_rsa type -1
debug1: identity file /Users/you/.ssh/id_ed25519 type 3
debug1: Offering public key: /Users/you/.ssh/work_ed25519 ED25519 SHA256:9aB…
debug1: Offering public key: /Users/you/.ssh/id_ed25519 ED25519 SHA256:Zp7…
git@github.com: Permission denied (publickey).

type -1 means the file doesn't exist. Every Offering public key line is one attempt. If the key you expected isn't in the list, it's a config problem. If it is in the list and still fails, it's a server-side problem.

To see what your config resolves for a host without connecting, ask SSH directly:

Terminal
ssh -G github.com | grep -E "^(identityfile|identitiesonly|user|hostname) "

#Cause 1: the public key isn't on the server

The server must already trust the public half of your key.

  • GitHub / GitLab / Bitbucket: copy the .pub file and add it in your account's SSH key settings.
  • Your own server: append it to ~/.ssh/authorized_keys for the user you log in as.
Terminal
pbcopy < ~/.ssh/id_ed25519.pub          # copies the public key on macOS
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server.example.com

#Cause 2: SSH offers the wrong key first

If you have several keys, SSH tries them in order: the ones from your config, then everything in your agent. GitHub accepts the first key that matches any account, so a personal key offered first logs you in as the wrong account (ERROR: Permission to org/repo.git denied to personal-you). Too many wrong keys also trip the server's attempt limit.

Pin exactly one key per host:

~/.ssh/config
Host github.com
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

IdentitiesOnly yes stops SSH from also trying every key in your agent. See IdentitiesOnly explained.

#Cause 3: a broader rule adds an extra key

IdentityFile is the one SSH option that adds up across every matching Host block. A Host * block at the bottom of your config adds its key to every host:

~/.ssh/config
Host github.com
  IdentityFile ~/.ssh/work_ed25519

# …later in the file
Host *
  IdentityFile ~/.ssh/id_rsa        # also offered to github.com!

Exclude the specific host from the wildcard (Host * !github.com), or remove the catch-all key. The wildcards guide explains the precedence rules.

#Cause 4: the private key's permissions are too open

SSH refuses a private key other users can read, and says so in verbose output:

Output
WARNING: UNPROTECTED PRIVATE KEY FILE!
Permissions 0644 for '/Users/you/.ssh/id_ed25519' are too open.
This private key will be ignored.

Fix it:

Terminal
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

#Cause 5: your config points at a key that doesn't exist

A typo or a deleted file shows up as no such identity or type -1 in ssh -v. SSH quietly skips it and moves on. If that was the only key for the host (especially with IdentitiesOnly yes), nothing valid gets offered.

#Cause 6: wrong user name

For Git hosts the user is always git: git@github.com:org/repo.git, never your own user name. For servers, make sure you're logging in as the account whose authorized_keys holds your key (User deploy in the config, or deploy@host).

#Cause 7: server-side file permissions

With OpenSSH's default StrictModes, the server ignores authorized_keys if the home directory, ~/.ssh or the file itself is writable by other users:

Terminal
# on the server
chmod go-w ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

#Prevent it for good

Most of these causes come down to SSH not knowing which key belongs to which host. Give every host exactly one key, keep wildcards from leaking extra keys, and check what SSH will really do before you connect.

EasySSHConfig does exactly that on a Mac. It lists every key with its file names and permissions, flags missing key files and keys that are too open, lets you drag a host onto the key it should use, and shows any host that gets offered extra keys, with the line in your config responsible.

Frequently asked questions

What does Permission denied (publickey) mean?

The server only allows public key authentication, and none of the keys your SSH client offered matched a public key the server trusts for that user. Either the right key wasn't offered, it wasn't readable, or the server doesn't have the matching public key.

How do I see which SSH key is being used?

Run ssh -vT git@github.com (or your host). Lines starting with 'Offering public key' show each key SSH tries, in order. ssh -G hostname | grep identityfile shows which IdentityFile entries your config resolves for that host.

Why does it work on one Mac but not another?

The key, the ~/.ssh/config entries, or the agent contents differ. Copy the working config, make sure the private key exists with mode 600, and add the matching public key to the server or Git host from the new Mac.

Should I use git@github.com or my username?

Always git@github.com. GitHub identifies your account by the key you present, not by the SSH user name.