ssh: Permission denied (publickey) means the server only accepts key-based login and rejected every key your client offered, or your client offered none at all. To fix it, run ssh -v user@host and check three things in order: you are logging in as the right user, the client is offering the key you expect, and the server has the matching public key in the right authorized_keys file with permissions strict enough for sshd to trust it. The server's log (journalctl -u sshd or journalctl -u ssh) usually tells you which of those failed in one line.
Every error message and log line quoted below came from a test sshd I ran on 127.0.0.1 with OpenSSH 10.5p1, reproducing each failure on purpose. The fixes are ordered by how often I see them in practice.
What SSH "Permission denied (publickey)" Actually Means
The word in parentheses is the list of authentication methods the server still allows. (publickey) alone means password and keyboard-interactive logins are disabled, so a key is the only way in. If you see (publickey,password) the server would also accept a password, and the problem is different.
SSH public key authentication happens in two steps. The client asks "would you accept this public key for user X?", and only if the server says yes does the client prove it holds the private key by signing. The error appears when the client runs out of keys to ask about. That gives you three broad failure classes:
- The client never offers the right key (wrong file, wrong agent, wrong config).
- The server never finds that key for that user (wrong user, wrong file, bad permissions, key not in
authorized_keys). - The server finds the key but refuses its signature algorithm (old
ssh-rsaSHA-1 signatures, removed DSA keys).
Read the ssh -v Output Line by Line
Start every investigation with verbose output. One -v is usually enough; -vvv adds detail you rarely need.
ssh -v -i ~/.ssh/id_ed25519 deploy@203.0.113.10
This is the relevant part of a failed attempt from my test server, where I deliberately offered a key that was not in authorized_keys:
debug1: Authenticating to 127.0.0.1:2222 as 'tapeshkhandelwal'
debug1: Authentications that can continue: publickey
debug1: Will attempt key: wrongkey ED25519 SHA256:gykdiP8eqUVpckALYe0e33HkkLbX2I8PNVpPckTbLL4 explicit
debug1: Offering public key: wrongkey ED25519 SHA256:gykdiP8eqUVpckALYe0e33HkkLbX2I8PNVpPckTbLL4 explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
tapeshkhandelwal@127.0.0.1: Permission denied (publickey).
Read it like this:
Authenticating to ... as '...'is the username the server sees. If it is your laptop username and the server expectsubuntuorec2-user, you have found the bug already.Will attempt key:lists every key the client plans to try, with its fingerprint.explicitmeans it came from-iorIdentityFile; keys from the agent are markedagent.Offering public key:followed by anotherAuthentications that can continue: publickeymeans the server said no to that key. That is a server-side mismatch: wrong user, wrongauthorized_keys, or permissions.No more authentication methods to trymeans the client has nothing left to offer.
A successful login looks like this instead:
debug1: Offering public key: goodkey ED25519 SHA256:/8HBAcLog7bVlayymyNI4VHnuSASF28nDTZbkvy4Ctw explicit
debug1: Server accepts key: goodkey ED25519 SHA256:/8HBAcLog7bVlayymyNI4VHnuSASF28nDTZbkvy4Ctw explicit
Authenticated to 127.0.0.1 ([127.0.0.1]:2222) using "publickey".
Compare the SHA256: fingerprint the client offers with the fingerprint of the line in the server's authorized_keys. On the server, ssh-keygen -lf ~/.ssh/authorized_keys prints a fingerprint for every key in the file. If none match, the key simply is not there.
Symptom-to-cause table
What ssh -v shows |
Most likely cause | Where to fix |
|---|---|---|
Wrong name after Authenticating to ... as |
Wrong username | Client: user@host or User in ~/.ssh/config |
Offering public key then rejected |
Key not in that user's authorized_keys, or sshd refuses the file's permissions |
Server |
No Offering line for the key you expected |
Wrong -i path, IdentitiesOnly, agent missing the key |
Client |
Load key "...": bad permissions |
Private key readable by others | Client: chmod 600 |
Skipping ssh-rsa key ... algorithm not supported by server |
Client limited to SHA-1 ssh-rsa signatures |
Client config or new key |
Server accepts key but still denied |
Private key could not sign (agent or passphrase problem) | Client |
Too many authentication failures |
Agent offered too many keys before the right one | Client: IdentitiesOnly yes |
Client-Side Causes and Fixes
The wrong username
Most cloud images do not have an account matching your laptop's username, and sshd treats a nonexistent user exactly like a rejected key from the client's point of view. On the server side the log is explicit: when I connected as a user that did not exist, sshd logged Invalid user nosuchuser from 127.0.0.1. The client just saw Permission denied (publickey).
The key file is ignored because of permissions
OpenSSH refuses to use a private key that other users can read:
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
Permissions 0644 for 'looseperm' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "looseperm": bad permissions
Fix it with chmod 600 ~/.ssh/id_ed25519 (and chmod 700 ~/.ssh). This happens constantly with .pem files downloaded from a cloud console into ~/Downloads.
A typo in -i silently falls back to default keys
If the path you pass to -i does not exist, ssh prints a one-line warning and carries on with the default key files:
Warning: Identity file nosuchkey not accessible: No such file or directory.
debug1: Will attempt key: /Users/me/.ssh/id_rsa
debug1: Will attempt key: /Users/me/.ssh/id_ed25519
In a script or CI log that warning scrolls past easily, and the eventual error looks like a server problem. Always check the Will attempt key lines.
The agent offers the wrong keys, or too many
Without IdentitiesOnly yes, ssh offers every key loaded in your agent before the files you named. Servers cap attempts with MaxAuthTries (default 6), so a well-stocked agent can exhaust the limit before reaching the right key and you get Too many authentication failures instead. OpenSSH 10.6 relaxed this on the server side by counting "would you accept this key?" probes separately from real attempts via the new PubkeyAuthOptions max-pk-ok option (OpenSSH release notes), but older servers are everywhere. Pin the key per host:
Host prod-bastion
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/prod_ed25519
IdentitiesOnly yes
Check what the agent holds with ssh-add -l -E sha256. If it says it cannot connect to the agent, start one with eval "$(ssh-agent -s)" and ssh-add ~/.ssh/prod_ed25519.
To see the configuration ssh will actually use after all Host and Match blocks are applied, run:
ssh -G prod-bastion | grep -Ei '^(user|hostname|port|identityfile|identitiesonly|pubkeyacceptedalgorithms) '
This catches the classic mistake of a broad Host * block near the top of ~/.ssh/config setting User or IdentityFile before your specific block gets a chance. For each option, ssh uses the first value it finds.
Usernames with backslashes or dollar signs
OpenSSH 10.6 started rejecting usernames containing \ or $ when they are typed on the command line, for example CORP\alice@host. Usernames set with User in a config file are not subject to the restriction (release notes). If a Windows-domain login broke after a client update, move the username into ~/.ssh/config.
Server-Side Causes and Fixes
If the client offered the right key as the right user and was still refused, go to the server. You need console access, a cloud serial console, or another account that still works.
Read the sshd log first
# Fedora, RHEL, Amazon Linux, Arch
sudo journalctl -u sshd -n 50 --no-pager
# Debian and Ubuntu (the unit is called ssh)
sudo journalctl -u ssh -n 50 --no-pager
These are the lines that matter, all from my test server:
Authentication refused: bad ownership or modes for directory /private/tmp
Failed publickey for tapeshkhandelwal from 127.0.0.1 port 53838 ssh2: ED25519 SHA256:/8HBAc...
Invalid user nosuchuser from 127.0.0.1 port 54007
Accepted publickey for tapeshkhandelwal from 127.0.0.1 port 54001 ssh2: ED25519 SHA256:/8HBAc...
Failed publickey with no other explanation usually means the key is not in the file sshd read. Authentication refused: bad ownership or modes is the StrictModes check, covered next. If the default log level hides the reason, temporarily set LogLevel DEBUG1 in sshd_config, reload, reproduce once, and set it back.
StrictModes: ownership and permissions
With StrictModes yes (the default), sshd refuses an authorized_keys file if the file or any directory above it is writable by someone other than the user or root. My first test run failed exactly this way: the key file sat under /private/tmp, which is world-writable, and sshd logged bad ownership or modes for directory /private/tmp even though the key itself was correct.
The safe layout:
# run as root, replacing alice with the target user
chown -R alice:alice /home/alice/.ssh
chmod 700 /home/alice/.ssh
chmod 600 /home/alice/.ssh/authorized_keys
chmod go-w /home/alice # home must not be group- or world-writable
Google Cloud's troubleshooting guide gives the same rule: the connecting user must own $HOME, $HOME/.ssh and authorized_keys, with 0700 on .ssh, 0600 on the file and 0700, 0750 or 0755 on the home directory depending on the distribution. Do not "fix" this by setting StrictModes no; fix the permissions.
On SELinux systems (Fedora, RHEL and derivatives), a file created in an unusual way, such as copied from a backup or written by a provisioning tool into a non-standard home path, can carry the wrong security label. sudo restorecon -Rv /home/alice/.ssh resets it.
The key is in the wrong file, or broken
Check where sshd actually looks:
sudo sshd -T | grep -Ei '^(authorizedkeysfile|pubkeyauthentication|pubkeyacceptedalgorithms|strictmodes|allowusers|denyusers|maxauthtries) '
sshd -T prints the effective configuration, including drop-ins under /etc/ssh/sshd_config.d/. Look for:
authorizedkeysfilepointing somewhere other than.ssh/authorized_keys(some hardened images use/etc/ssh/authorized_keys/%u).pubkeyauthentication noset by aMatchblock for certain users or addresses. Test a specific case withsudo sshd -T -C user=alice,host=laptop,addr=198.51.100.7.AllowUsersorAllowGroupsthat do not include your user.
Then check the key line itself. It must be a single line (ssh-ed25519 AAAA... comment). Pasting from a web page or a narrow terminal can insert line breaks, and AWS's guide explicitly warns to paste the key "in one continuous line". If the line starts with options such as from="10.0.0.0/8", your source address must match them.
When you cannot see why, run a second sshd in debug mode on a spare port. It handles one connection and prints its whole decision process to your terminal:
sudo /usr/sbin/sshd -d -p 2222
# from the client
ssh -v -p 2222 alice@server
ssh-rsa, SHA-1 and old keys
OpenSSH 8.8 (September 2021) disabled RSA signatures using SHA-1, the ssh-rsa signature algorithm, by default. The release notes stress that RSA keys still work: they sign with rsa-sha2-256 or rsa-sha2-512 instead. Problems appear in two situations:
- A modern client talks to an old server that only understands
ssh-rsa. The fix the OpenSSH team gives is a per-host stopgap, never a global one:
Host old-appliance
HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
- Something forces SHA-1 against a modern server. When I restricted my client to
ssh-rsa, it would not even offer the key:Skipping ssh-rsa key rsakey - corresponding algorithm not supported by server. OldPubkeyAcceptedKeyTypeslines copied from a decade-old blog post are a common source. That option was renamedPubkeyAcceptedAlgorithmsin OpenSSH 8.5; the old name still works as an alias.
DSA keys are a harder stop. OpenSSH 10.0 removed DSA support entirely, so an id_dsa key will never work against a current server. Generate a replacement:
ssh-keygen -t ed25519 -C "alice@laptop-2026"
Ed25519 is the right default for new keys. Use RSA (3072 bits or more) only if something in the path cannot handle Ed25519.
Cloud VM Specifics: AWS, Google Cloud and Azure
Cloud images add one twist each.
| Platform | Default login user | Common trap |
|---|---|---|
| AWS EC2 | Amazon Linux, FreeBSD, Oracle: ec2-user; Ubuntu: ubuntu; Debian: admin; Fedora: fedora or ec2-user; RHEL and SUSE: ec2-user or root; CentOS: centos or ec2-user; Bitnami: bitnami |
Wrong user for the AMI; using a different key pair than the one chosen at launch |
| Google Compute Engine | The username attached to your key in metadata, or your Google identity with OS Login | OS Login enabled, so metadata keys are ignored (or the reverse) |
| Azure | Whatever you set at creation; Microsoft's quickstart uses azureuser |
Using the wrong .pem from the portal's key-pair download |
The AWS list comes from the EC2 user guide; for other AMIs, check with the AMI provider. On Google Cloud, the SSH troubleshooting page spells out the OS Login rule: "If OS Login is enabled on your project, your VM doesn't accept SSH keys that are stored in metadata." It also notes that keys added by the console or gcloud expire, after which the guest agent removes them from authorized_keys, which explains the "it worked yesterday" case. Azure's portal quickstart connects with ssh -i ~/Downloads/myKey.pem azureuser@<ip> after chmod 400 on the key.
If you recently moved EC2 workloads to new AMIs, our Amazon Linux 2 to Amazon Linux 2023 migration guide covers the other access changes that come with the new image.
GitHub: Permission Denied (publickey) on git push
For GitHub the test is:
ssh -T git@github.com
Success prints Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. Anything else, debug with ssh -vT git@github.com and check the same Will attempt and Offering lines as above.
Three GitHub-specific causes from GitHub's troubleshooting page:
- Wrong user. Always connect as
git, never your GitHub username. A remote likealice@github.com:org/repo.gitfails. - sudo.
sudo git pushruns as root and uses root's keys, not yours. Do not use sudo with Git. - Key not loaded or not uploaded.
ssh-add -l -E sha256shows what the agent holds; compare it with the fingerprints listed under your account's SSH keys settings.
If you juggle a personal and a work account, give each a Host alias with its own IdentityFile and IdentitiesOnly yes, then use the alias in the remote URL (git@github-work:org/repo.git). And if the push is failing because of what is in the commit rather than how you authenticate, see how to undo a Git commit.
Frequently Asked Questions
How do I fix "Permission denied (publickey)" on AWS EC2?
Use the default user for your AMI (ec2-user for Amazon Linux, ubuntu for Ubuntu, admin for Debian), pass the key pair you selected at launch with -i, and make sure the .pem file is chmod 400 or 600. If you lost the key, use EC2 Instance Connect or Session Manager to add a new public key to authorized_keys.
Why does SSH say permission denied when my key is in authorized_keys?
Usually permissions or the wrong user. sshd ignores authorized_keys if the file, ~/.ssh or the home directory is writable by others, and it logs Authentication refused: bad ownership or modes. Also confirm with sshd -T that AuthorizedKeysFile points where you think it does.
Does OpenSSH still support RSA keys?
Yes. OpenSSH disabled the SHA-1 ssh-rsa signature algorithm in 8.8, not RSA keys. Existing RSA keys sign with SHA-256 or SHA-512 automatically. DSA keys, however, were removed completely in OpenSSH 10.0.
What is the difference between "Permission denied (publickey)" and "(publickey,password)"?
The list shows which methods the server still accepts. (publickey) means only keys are allowed. (publickey,password) means a password would also work, so either the account has no usable password or your client did not offer one.
How do I see which key SSH is using?
Run ssh -v user@host and read the Will attempt key and Offering public key lines, which include each key's SHA256 fingerprint. ssh -G host shows the IdentityFile and IdentitiesOnly values your config produces.
Conclusion
"Permission denied (publickey)" is never random. Run ssh -v to see which user and key you are offering, read the server log to see why it was refused, and fix the specific cause: the username, a chmod 600, an IdentitiesOnly yes, a single-line key in the right authorized_keys, or a modern key instead of DSA or SHA-1 RSA. If you are hardening the same servers, the post-quantum TLS guide covers the matching key-exchange shift on the web side; current OpenSSH already warns when a connection uses key agreement that is not post-quantum safe.