SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).Short answer
On EC2 this error almost always means the instance did not accept your key for that user. Check the user name for the AMI (ec2-user, ubuntu, admin, rocky), then confirm the .pem belongs to the key pair named on the instance. If you had access before, a changed home folder or authorized_keys mode is the next suspect.
Fix it fast
1Find out which cause you have
Run the connection with verbose output. The line 'Authentications that can continue' and the lines about the key tried show whether your key was sent at all.
ssh -vvv -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com2Match what you see, then jump to its fix
- 1
Permission denied (publickey) right after 'Offering public key' or 'we sent a publickey packet'You used the wrong login name for the AMIFix - 2
ssh -vvv shows 'Trying private key' for your file, then 'No more authentication methods to try'The .pem file is not the key pair the instance was launched withFix - 3
It worked before, nothing else changed, and the same key now failsModes on the home folder or authorized_keys were changed on the instanceFix
- 1
What does the error look like?
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).Connection closed by 203.0.113.25 port 22
Cause 1 of 3
You used the wrong login name for the AMI
1Check
The key is installed for one account only, and the account name depends on the AMI. Amazon Linux uses ec2-user, Ubuntu uses ubuntu, Debian uses admin, Rocky Linux uses rocky, Bitnami uses bitnami; RHEL and SUSE may use ec2-user or root.
aws ec2 describe-images --image-ids ami-0123456789abcdef0 --query 'Images[0].[Name,Description]'2Fix
Reconnect with the user name for that AMI. If the AMI came from a third party, ask its provider.
ssh -i ~/.ssh/my-key.pem ubuntu@ec2-203-0-113-25.compute-1.amazonaws.com3Verify
You get a shell prompt for that account.
whoamiConfirmed by docs.aws.amazon.com
Cause 2 of 3
The .pem file is not the key pair the instance was launched with
1Check
Read the key pair name on the instance and compare it with the file you are using. Key pairs are only attached at launch; a new .pem with a similar name will not work.
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 --query 'Reservations[0].Instances[0].KeyName'2Fix
Use the .pem that belongs to that key pair. If you generated the key yourself and imported it, it must be an RSA (or another supported type) key, because AWS says DSA keys are not accepted. If the private key is gone, AWS documents replacing the key through the root volume on a temporary instance.
ssh -i ~/.ssh/the-right-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com3Verify
The login succeeds with the key pair name shown on the instance.
ssh -i ~/.ssh/the-right-key.pem -o IdentitiesOnly=yes ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com true && echo okConfirmed by docs.aws.amazon.com
Cause 3 of 3
Modes on the home folder or authorized_keys were changed on the instance
1Check
sshd ignores authorized_keys when the home folder or the .ssh folder is writable by others. AWS says authorized_keys must be limited to the owner. If you can still log in some other way (Session Manager, EC2 Instance Connect), read the modes.
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys2Fix
Set the three modes AWS lists. If you cannot log in at all, mount the root volume on a temporary instance in the same Availability Zone and run the same commands against the mounted path.
chmod 700 ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys3Verify
Log out and connect again with the key.
ls -ld ~ ~/.ssh ~/.ssh/authorized_keysConfirmed by docs.aws.amazon.com
How do I stop it coming back?
- Tag every instance with its login user and key pair name so nobody has to guess.
- Set an IdentityFile and User per host in ~/.ssh/config.
- Keep Session Manager or EC2 Instance Connect enabled as a second way in, so a damaged authorized_keys is not a lockout.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Security
SSH refuses your key and ends with Permission denied (publickey)
user@203.0.113.10: Permission denied (publickey,password).
The server accepted the connection but found no key it would take.
5 causes, each with a fixSecurity
ssh ignores your EC2 .pem file because its permissions are too open
Permissions 0644 for 'my-key.pem' are too open.
OpenSSH refuses a private key that other users can read or write, then falls back to Permission denied (publickey).
3 causes, each with a fixSecurity
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
gcloud init cannot finish the sign-in step
Enter authorization code
gcloud init signs in through a browser.
3 causes, each with a fixSecurity
A GitHub Actions SSH deploy step cannot log in to the server
ssh: handshake failed: ssh: unable to authenticate
The runner reached your server but the server refused the login.
4 causes, each with a fixSecurity
SSH to GitHub asks to confirm the host, or hangs on that prompt
ED25519 key fingerprint is SHA256:
SSH has never seen github.com from this machine, so it asks you to trust the server key.
2 causes, each with a fix