Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

ErrorPermission 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.

  • Applies to Amazon Linux 2023, Ubuntu 24.04, Debian 12 and 13 AMIs, OpenSSH 9.x client
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.com
  2. 2Match what you see, then jump to its fix

    1. 1Permission denied (publickey) right after 'Offering public key' or 'we sent a publickey packet'You used the wrong login name for the AMIFix
    2. 2ssh -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. 3It worked before, nothing else changed, and the same key now failsModes on the home folder or authorized_keys were changed on the instanceFix

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.com

3Verify

You get a shell prompt for that account.

whoami

Confirmed by docs.aws.amazon.com

Did this fix it?

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.com

3Verify

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 ok

Confirmed by docs.aws.amazon.com

Did this fix it?

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_keys

2Fix

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_keys

3Verify

Log out and connect again with the key.

ls -ld ~ ~/.ssh ~/.ssh/authorized_keys

Confirmed by docs.aws.amazon.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.