Sub topic 3
Key pairs and the right login user for your AMI
This chapter assumes you can use a Linux shell, know what an AMI is, and have the AWS CLI configured. By the end you will know how EC2 key pairs work, how to create one, and how to pick the login user that matches your AMI.
How a key pair works
A key pair is two linked files: a public key and a private key. EC2 places the public key on the instance. You keep the private key. When you connect, SSH proves you hold the private key without sending it over the network.
The public key is copied into the ~/.ssh/authorized_keys file of the instance's default user. This happens at first boot, through cloud-init. If you attach a different key pair later, the existing instance does not change. The key is only injected at launch.
Creating a key pair
You can create a key pair in the console, or with the CLI. The CLI command below saves the private key straight to a file.
aws ec2 create-key-pair \
--key-name my-key \
--key-type ed25519 \
--query "KeyMaterial" \
--output text > ~/.ssh/my-key.pem
chmod 400 ~/.ssh/my-key.pemEC2 shows you the private key only once, at creation. AWS keeps the public half, not the private half. If you lose the file, you cannot recover it.
Choose ed25519 or rsa. Ed25519 keys are short and modern. RSA is still needed for some older tools and for Windows instances, where the key decrypts the administrator password.
You can also import a key you already have, with aws ec2 import-key-pair. This keeps the private key off AWS entirely, so it is the better habit.
Choosing the login user
The key pair does not name a user. The user is set by the AMI, and the wrong one gives "Permission denied (publickey)" even with a correct key.
| AMI family | Default user |
|---|---|
| Amazon Linux 2 and 2023 | `ec2-user` |
| Ubuntu | `ubuntu` |
| Debian | `admin` |
| RHEL | `ec2-user` |
| SUSE Linux | `ec2-user` |
| Fedora | `fedora` |
| CentOS | `centos` |
| Bitnami | `bitnami` |
Some images differ from this list, so treat it as a starting point. To check, open the AMI's description in the EC2 console, or read the console output of a running instance.
If you are not sure, try one of these to see which user the instance accepts:
ssh -i ~/.ssh/my-key.pem -o IdentitiesOnly=yes [email protected]
ssh -i ~/.ssh/my-key.pem -o IdentitiesOnly=yes [email protected]The IdentitiesOnly=yes option tells SSH to offer only the key you named. Without it, an SSH agent may offer many keys, and the server can close the connection after too many failed attempts.
Common mistakes
- Wrong region. Key pairs belong to one region. A key named
my-keyinus-east-1does not exist ineu-west-1. - Wrong key for the instance. The instance records the key pair name it launched with. Check it with
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 --query "Reservations[].Instances[].KeyName". - Root login. Most AMIs block
rootover SSH and print a message naming the correct user. - Lost private key. You cannot add a key to a running instance through EC2. Use EC2 Instance Connect or Session Manager (later chapters), or launch a new instance.
Exercise
You launch an Ubuntu instance with a valid key pair, set chmod 400 on the private key, and run ssh -i my-key.pem [email protected]. You get "Permission denied (publickey)". What is the most likely cause, and what do you change?
Show answer
The login user is wrong. Ubuntu AMIs use ubuntu, not ec2-user. Run ssh -i my-key.pem [email protected]. The key and its permissions are fine, because a bad file mode would give an "UNPROTECTED PRIVATE KEY FILE" warning instead.