Skip to the workspace
Pebellslearn by building
EC2 — Virtual Servers  ›  Lesson

Key Pairs and Logging In

Keypairs 12 minAn EC2 key pair has a public key that AWS places on the instance and a private key that only you hold, and the private key proves your identity when you connect over SSH.

aws / Key Pairs and Logging In

Explore each scene at your own pace.

01 · The rule

The rule

Key Pairs and Logging In

Inkwell's server is running. Now the developer needs to log in to check the web server. EC2 Linux instances do not use passwords for this by default. Instead they use a key pair, and understanding who holds which half explains nearly every login error you will see.

The rule

AWS keeps the lock; you keep the only key.

A key pair is two mathematically linked keys:

  • The public key is the lock. AWS stores it and, at launch, places it in the instance's ~/.ssh/authorized_keys file. It can be shown to anyone.
  • The private key is the key. You download it once, as a .pem file, when you create the key pair. AWS does not keep a copy.

When you connect over SSH, your computer proves it holds the private key, and the instance checks that proof against the public key. The private key itself never crosses the network.

chmod 400 inkwell-key.pem
ssh -i inkwell-key.pem ec2-user@13.232.10.25
  • chmod 400 makes the file readable only by you; SSH refuses a private key that others can read.
  • The username depends on the image: ec2-user for Amazon Linux, ubuntu for Ubuntu.
  • Key types are RSA or ED25519.

For Windows instances the key pair works differently: it decrypts the Administrator password once, and you log in with that password over Remote Desktop.

You can also avoid long-lived keys: EC2 Instance Connect pushes a short-lived key for each session, and Systems Manager Session Manager gives a shell without opening port 22 at all.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.