How to Login to your AWS EC2 without a .pem Key

Essentially there is always a few steps to follow in order to easy SSH in to your AWS server without the need for your .pem key. For security reasons, this is not an option when you first setup a server, but with these few instructions, right when you setup your server can save you a lot of irritation always hunting down the correct .pem key.

Pre-Requisites

  • Must have an AWS Account
  • Must have a way to use commands line. (PuTTY is an option for windows)
  • EC2 Server (Click here you need help launching a EC2 on Amazon

Part 1: Generate a id_rsa.pub

Generate a id_rsa.pub if you have not done so already. Here are the steps needed for that.

  1. We need to navigate where we typically keep our system ssh key, in command line:
    $ cd ~/.ssh
  2. Next, we will generate a key with our email address.
    $ ssh-keygen -t rsa -C "your_email@example.com"

    It will ask you a few questions, you can just hit “Enter”.

  3. Then type
    $ ls -l

    This will list out all of the files, it will look something like this:cli ls image

  4. Next we need to copy our .pub key to our clipboard so we can post it in Github. Run:
    $ pbcopy < id_rsa.pub 

You can find more details if needed about id_rsa.pub here.

Part 2: Login to your EC2 Server and Add your Key

We are going to SSH into our current server and then add our key to your server so we do not have to use .pem any more to login.

  1. First lets, login to our AWS console.
  2. Next, we will select EC2 and then click on instances. With your instance selected, click on one of the servers and hit connect. It will launch a dialog box with your connection string in there. It should look something like this:
    amazon dialog box
  3. Now grab your connection string and goto command line and past it in. Ex:
    $ ssh -i erickey.pem ubuntu@54.215.171.225
  4. Once you're connected to your server, you will need so login into the root user. You will be able to do this by using:
    $ sudo su
  5. Next we will cd into our directory with our authorized_keys to add th id_rsa.pub that we have copied to our clipboard. Run:
    $ cd ~/.ssh

    Here you will see a file called authorized_keys

  6. We need to modify this file by adding our key. Run:
    $ nano authorized_keys

    and paste your key into the file under the default key at the top. To exit out of the file, hit ctrl+x and then hit y to save the file.

Final

Now you will be able to exit your server and log back in as root just by using

$ ssh root@xxx.xxx.xxx.xx

This makes life a lot easier, so you don't have to hunt down that pesky .pem file and where you put it.