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:
- Wrong user name or wrong key — the image's default user, a missing
-i, a different key pair - The key was dropped because of permissions — the client's private key or the server's
~/.ssh - sshd configuration blocks it —
PubkeyAuthentication,AllowUsers - 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 2Invalid 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 noorchmod 777.
The order to work in
- Use
ssh -vto see which key was offered as which user - Check the image's default user, and compare
ssh-keygen -youtput withauthorized_keys - Client key
600; server.ssh700andauthorized_keys600 - Check the server log and
sshd -Tfor 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 -vto see which key was offered and whether it was rejected. - Make sure you log in as the image's default user (
ec2-user,ubuntu,adminand so on). - Check that the output of
ssh-keygen -y -f <private key>is in the server'sauthorized_keys. - Set the client's private key to
chmod 600. - On the server, keep
~/.sshat 700,authorized_keysat 600 and the home directory not group- or world-writable. - Read the refusal reason in the server log (
/var/log/auth.logor/var/log/secure). - Check the effective
PubkeyAuthentication,AuthorizedKeysFileandAllowUserswithsshd -T. - If you see
no mutual signature algorithm, switch to an ed25519 key.