Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

SSH to EC2 fails: refused key, or told to log in as ec2-user not root

ErrorPlease login as the user "ec2-user" rather than the user "root".

Short answer

Almost always the instance is fine and the login is not. EC2 images create one default user, and that user name differs by operating system. Use that name, not root, with the key pair chosen at launch. If both are right, check the PuTTY key format and the permissions on authorized_keys.

  • Applies to Amazon Linux 2023, Ubuntu 24.04, OpenSSH 9, PuTTY 0.8x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Connect with triple verbose output and read the last lines. They show which key was offered and which user was tried.

    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. 1The login banner names a different user, or publickey is refused straight awayYou used root or another user name than the image's defaultFix
    2. 2Permission denied (publickey) with the right user name; ssh -vvv offers a key and gets no acceptanceThe key file is not the key pair the instance was launched withFix
    3. 3Server refused our key or No supported authentication methods available, from PuTTYPuTTY is using a .pem file, or an old PuTTY versionFix
    4. 4It worked before, and nothing about the key or user changedFile modes on the instance were changedFix

What does the error look like?

  • Please login as the user "ec2-user" rather than the user "root".
  • Server refused our key
  • Permission denied (publickey)
  • No supported authentication methods available

Cause 1 of 4

You used root or another user name than the image's default

1Check

Default users differ by image: Amazon Linux ec2-user, Ubuntu ubuntu, Debian admin, Rocky Linux rocky, Bitnami bitnami, CentOS centos or ec2-user. Check which image launched the instance (AMI details are on the instance page in the console).

ssh -vvv -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

2Fix

Log in with the default user for that image and use sudo for root tasks.

ssh -i ~/.ssh/my-key.pem ubuntu@ec2-203-0-113-25.compute-1.amazonaws.com

3Verify

You get a shell prompt instead of a refusal.

whoami

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

The key file is not the key pair the instance was launched with

1Check

Compare the key pair name shown on the instance details tab with the .pem file you are using. A key from another pair, or none selected at launch, cannot work. Also confirm the key is RSA; DSA keys are not accepted.

ls -l ~/.ssh/my-key.pem

2Fix

Use the .pem that belongs to that key pair. If it is lost, EC2 documents a recovery by attaching the root volume to another instance and replacing authorized_keys.

chmod 400 ~/.ssh/my-key.pem
ssh -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

3Verify

The login succeeds.

ssh -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com whoami

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

PuTTY is using a .pem file, or an old PuTTY version

1Check

PuTTY reads .ppk files, not .pem. The user name goes in the PuTTY configuration window, and an outdated PuTTY can also fail.

head -n 1 my-key.pem

2Fix

Load the .pem in PuTTYgen, use Save private key (not Generate), enter the default user name in PuTTY, and update PuTTY.

# PuTTYgen: Load my-key.pem, then Save private key -> my-key.ppk
# PuTTY: Host name = ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

3Verify

Select the .ppk in the PuTTY auth settings and connect, or test the same key with OpenSSH.

ssh -vvv -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

File modes on the instance were changed

1Check

authorized_keys must be limited to its owner. If someone changed permissions on the home directory, SSH ignores the key. The EC2 fix is done from a temporary instance with the volume attached.

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

2Fix

Restore the modes on the mounted volume, replacing the path and user.

chmod 600 /mnt/tempvol/home/ec2-user/.ssh/authorized_keys
chmod 700 /mnt/tempvol/home/ec2-user/.ssh
chmod 700 /mnt/tempvol/home/ec2-user

3Verify

Re-attach the volume, start the instance and connect.

ssh -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Write the default user name next to the AMI in your team notes or launch template.
  • Keep each .pem with the name of its key pair, and chmod 400 it.
  • Create an AMI from a working instance before changing home directory permissions.

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

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

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