SSH to EC2 fails: refused key, or told to log in as ec2-user not root
Please 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.
Fix it fast
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.com2Match what you see, then jump to its fix
- 1
The login banner names a different user, or publickey is refused straight awayYou used root or another user name than the image's defaultFix - 2
Permission 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
Server refused our key or No supported authentication methods available, from PuTTYPuTTY is using a .pem file, or an old PuTTY versionFix - 4
It worked before, and nothing about the key or user changedFile modes on the instance were changedFix
- 1
What does the error look like?
Please login as the user "ec2-user" rather than the user "root".Server refused our keyPermission 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.com2Fix
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.com3Verify
You get a shell prompt instead of a refusal.
whoamiConfirmed by docs.aws.amazon.com
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.pem2Fix
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.com3Verify
The login succeeds.
ssh -i ~/.ssh/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com whoamiConfirmed by docs.aws.amazon.com
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.pem2Fix
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.com3Verify
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.comConfirmed by docs.aws.amazon.com
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_keys2Fix
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-user3Verify
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.comConfirmed by docs.aws.amazon.com
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 fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix