Linux4 min read· 3 practice problems

SSH Permission denied (publickey) — causes and fixes (user name, key permissions, sshd config)

When SSH fails with Permission denied (publickey): use ssh -v to see which key was offered, then work through the image's default user, the key pair, private key and .ssh permissions, sshd settings and ssh-rsa algorithm refusals.

On this page
  1. Short answer: the client offered no key the server would accept
  2. First check: which key did ssh -v offer?
  3. Cause 1: user name and key pair
  4. Cause 2: permissions, on both ends
  5. Cause 3: sshd configuration — the server log has the answer
  6. Cause 4: ssh-rsa (SHA-1) signatures disabled
  7. What not to do
  8. The order to work in
  9. Quick diagnostic checklist

Short answer: the client offered no key the server would accept

Permission denied (publickey) means the server allows only public-key authentication and none of the keys the client offered matches that user's authorized_keys. The cause is usually one of four:

  1. Wrong user name or wrong key — the image's default user, a missing -i, a different key pair
  2. The key was dropped because of permissions — the client's private key or the server's ~/.ssh
  3. sshd configuration blocks it — PubkeyAuthentication, AllowUsers
  4. The key algorithm was refused — ssh-rsa (SHA-1) with an old server or client

The same failure also appears as Permission denied (publickey,gssapi-keyex,gssapi-with-mic). or, in git, [email protected]: Permission denied (publickey).

First check: which key did ssh -v offer?

ssh -v -i ~/.ssh/prod.pem [email protected]
debug1: Offering public key: /home/me/.ssh/prod.pem RSA SHA256:3kq1... explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).
  • No Offering line → the key was never sent (path or permissions)
  • Offered, then rejected → that user on the server does not have this key (user or key pair)
  • send_pubkey_test: no mutual signature algorithm → an algorithm problem (cause 4)

Cause 1: user name and key pair

Each cloud image has its own default user. On official AWS images: ec2-user for Amazon Linux and RHEL, ubuntu for Ubuntu, admin for Debian, centos for CentOS and rocky for Rocky Linux. To check the key, derive the public key from the private key and compare it with the server's authorized_keys.

ssh-keygen -y -f ~/.ssh/prod.pem

If a crowded agent causes Too many authentication failures, use -o IdentitiesOnly=yes to offer only the key you name.

Cause 2: permissions, on both ends

If the client's private key is too open, ssh refuses to use it.

Permissions 0644 for '/home/me/.ssh/prod.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.

It comes with the WARNING: UNPROTECTED PRIVATE KEY FILE! banner; fix it with chmod 600 ~/.ssh/prod.pem. On the server, StrictModes (on by default) ignores the keys if the home directory or .ssh is writable by others.

chown -R deploy:deploy /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys
chmod go-w /home/deploy

Cause 3: sshd configuration — the server log has the answer

tail -n 30 /var/log/auth.log      # RHEL family: /var/log/secure
journalctl -u ssh -n 30           # RHEL family: -u sshd
sshd -T | grep -Ei 'pubkeyauthentication|authorizedkeysfile|allowusers'
  • Authentication refused: bad ownership or modes for directory /home/deploy/.ssh → cause 2
  • Invalid user ubuntu from 203.0.113.7 port 51234 → the user does not exist (cause 1)
  • User deploy from 203.0.113.7 not allowed because not listed in AllowUsers → AllowUsers

sshd -T shows the effective values, including files pulled in by Include from sshd_config.d. For a repeated keyword, the first value wins. After a fix, check the syntax with sshd -t and reload.

Cause 4: ssh-rsa (SHA-1) signatures disabled

Since OpenSSH 8.8, RSA/SHA-1 signatures (ssh-rsa) are disabled by default. RSA keys still work through rsa-sha2-256/512, but if one side is too old to support those, authentication fails. The real fix is a new key made with ssh-keygen -t ed25519.

What not to do

  • Working around it with PasswordAuthentication yes — the cause remains and the attack surface grows.
  • Skipping the permission checks with StrictModes no or chmod 777.

The order to work in

  1. Use ssh -v to see which key was offered as which user
  2. Check the image's default user, and compare ssh-keygen -y output with authorized_keys
  3. Client key 600; server .ssh 700 and authorized_keys 600
  4. Check the server log and sshd -T for configuration or algorithm refusals

To practise the same situation as a problem, start with the one-minute quiz SSH Permission denied (publickey): cannot log in to a new EC2 instance.

Quick diagnostic checklist

  • Use ssh -v to see which key was offered and whether it was rejected.
  • Make sure you log in as the image's default user (ec2-user, ubuntu, admin and so on).
  • Check that the output of ssh-keygen -y -f <private key> is in the server's authorized_keys.
  • Set the client's private key to chmod 600.
  • On the server, keep ~/.ssh at 700, authorized_keys at 600 and the home directory not group- or world-writable.
  • Read the refusal reason in the server log (/var/log/auth.log or /var/log/secure).
  • Check the effective PubkeyAuthentication, AuthorizedKeysFile and AllowUsers with sshd -T.
  • If you see no mutual signature algorithm, switch to an ed25519 key.

Practice with this guide

Reviewed problems about the same failure. Write the cause, recovery, and prevention yourself, then compare with the model answer.

Read next