When you want to connect to a server remotely, SSH is the most reliable option, which is why it’s the most common way into an EC2 instance. To use it you need a key pair: a private key that stays with you and a public key you give the server. In this post I show you the three ways to create those keys for AWS (from the console, creating them yourself and importing them, and while launching an instance) and how they work under the hood, so you know what to share and what not to.

Note: the video is in Spanish (Spanish audio only).

Way 1: create the key pair from the AWS console

  1. Open the EC2 service and, in the left menu, click Key Pairs.
  2. Click Create key pair.
  3. Choose the key type. Correction to what I say in the video: it depends on the instance, not your computer. For a Windows instance use RSA (ED25519 doesn’t work there); for a Linux instance either works.
  4. Choose the format: .pem if you’ll connect from the command line, .ppk if you’ll use PuTTY.
  5. Click Create key pair. The private key downloads by itself.

Keep it safe: you can’t download it again, because AWS keeps no record of it. The only thing AWS stores is the public key, to attach later to the service you want to connect to.

Way 2: create your own keys and import them

AWS also lets you import your own keys. First you create them on your computer with ssh-keygen.

On Windows, from the console:

ssh-keygen

It asks for the folder and file name (I called mine ssh-windows) and then a passphrase. You can leave it blank, but I recommend setting one: the passphrase encrypts the private key, which adds another layer of security. Just store it somewhere safe, because without it you can’t use the key.

When it finishes it shows the fingerprint: a short, unique cryptographic hash of the public key, a kind of signature to verify identity and integrity. In the folder you’ll see two new files: one with no extension, which is the private key, and one ending in .pub, which is the public key. To view the public key on Windows:

type ssh-windows.pub

On Linux (I use Ubuntu) it’s the same: ssh-keygen, the folder and name, the passphrase twice, and done. To check it and view the public key:

ls
cat ssh-linux.pub

Copy the public key’s contents and go back to the AWS console:

  1. Under Key Pairs, click Actions → Import key pair.
  2. Give it a name (for example ssh-linux).
  3. Upload the .pub file from your disk or paste its contents into the text box.
  4. Click Import.

For the Windows key the process is exactly the same.

Way 3: create the key while launching an instance

When you launch an instance in EC2, scroll down to the key pair section and click Create new key pair. It works like the first way: give it a name (I used ssh-pem), choose the .pem format and create it. AWS reminds you to store it safely: if you lose it you can’t download it again and you won’t be able to connect to the instance. The advantage is that the key is automatically attached to the instance you’re creating.

To delete a key: under Key Pairs, select it, choose Actions → Delete, type the confirmation word it asks for, and confirm.

How SSH keys work

Why two keys? Because SSH uses asymmetric encryption. When you want to connect to a service, you share your public key with it. The same public key works for logging into a server over SSH, for SFTP, or for pushing changes to GitHub over SSH.

When you connect, a process called the handshake takes place: with the public and private keys, the encryption algorithms come into play and create temporary shared keys so the connection is secure.

Is it safe to share the public key? Yes, completely: the private key can’t be worked out from the public one. What you must never share is the private key: that one is yours alone.

And if someone else needs into the same server? Ask them to generate their own key pair and share their public key with you. You add it to the service and they can connect from then on, without anyone sharing a private key.

With this you can manage your keys with confidence. The next step is using them: in Launch your own server in minutes I launch an instance and log into it over SSH.


Found it useful? Watch the full video on YouTube (in Spanish) and subscribe to @Andrevops for more videos like this one.