How To: Connect To Your AWS EC2 Instance

Amazon Web Services, or AWS for short offers cloud computing services and products that can relieve your IT department of a lot of time consuming tasks such as managing server hardware. It can also help save you money with services such as S3 where you only pay for what you use. In this article I’ll be discussing their EC2 cloud server product and how to connect to an instance using ssh. This article will assume you have already launched an EC2 instance.

Prerequisites

– Your key pair that is associated with your instance
– OpenSSH if you’re connecting from Windows (I will walk you through enabling it if you don’t have it already)
– Access to a terminal if you’re connecting from Linux

Connecting to Your AWS EC2 Instance in Linux

To connect using Linux the process is pretty straight forward since the ssh feature is usually preinstalled and doesn’t require any configuration. You should have downloaded a private key when you launched your instance. Notate the path of your private key, you will need it.

I’m using CentOS but the same steps apply to RHEL, Fedora, and SUSE systems. If you’re connecting to an Ubuntu instance then instead of using ec2-user you will use ubuntu as the username.

To Connect to an Amazon Linux or RHEL based system:

    Navigate to the location of your private key and set the proper permissions.

chmod 400 yourkey.pem

Run this command to initialize an ssh session to your instance
ssh -i path/to/yourkey.pem ec2-user@your.public.IP.address

For Ubuntu:
ssh -i path/to/yourkey.pem ubuntu@your.public.IP.address

You can also use your public IPv4 address instead of the public DNS name.

Connecting To EC2 From a Windows Machine

There are two ways you can connect to your EC2 instance in Windows, PowerShell and PuTTY, I’ll be covering both.

Connecting to Your EC2 Instance Using PowerShell

In the past there was only one secure method (that I know of) to ssh into a Linux machine on Windows, which was to use PuTTY. PowerShell has made some advancements since then and has implemented OpenSSH, allowing you to connect to any machine running Linux from your Windows PC using just PowerShell. At the time of writing this article I know of two different PowerShell tools (OpenSSH and PoshSSH) that allow you to ssh into a Linux box. I’ll be discussing OpenSSH here because if you’re running Windows version 1803 or newer then the OpenSSH feature is already enabled on your machine. If you’re on an older version you will need to turn on Developer Mode and enable the OpenSSH client.

To turn on developer mode go to Start > Settings > For Developers and selecting Developer Mode.

To enable the OpenSSH feature go to Start > Settings > Apps & Features > Manage Optional Features and turning on the OpenSSH feature.

You can then check to see if the OpenSSH feature is enabled by running the following in your terminal

Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'

The output should look like this
new1

You should now have the ssh cmdlet available. Check for it by simply typing ssh in PowerShell.

Let’s Connect to Our Instance

This is where you will need to have the path to key pair ready. Run the following command in PowerShell.

ssh -i yourkeyname.pem ec2-user@your.public.IP.address

You can also use your public DNS name which you can find in the EC2 dashboard under the Description section (You must have your EC2 instance selected in order to view the description)
aws3

tip: you may get a permission denied error this just means you need to change the permissions of your key pair. you can do this by using chmod 400 if you have Gnu On Windows installed or by simply going to the properties of the .pem file and giving yourself full permissions.

Connecting to Your EC2 Instance Using PuTTY

If you don’t already have PuTTY installed you can get it from their official website

In order to SSH into your AWS EC2 instance using putty you will need four things:
1. The AMI ID of the instance
2. The Public DNS Name
3. The private key
4. The default username of the AMI
(You can find the default username for each type of AMI here, in this case is will either be ec2-user or root).

Since PuTTY doesn’t natively support .pem files we will need to convert our private key into a file that it can read. PuTTYGen is a feature that comes built in to PuTTYGen which will allow us to convert our private keys. In your Windows search bar type PuTTYGen and run it.

aws4

Click Load key
Find your key (you may need to change the drop down to show all files)
puttygen-load-key
Save the key

Once you have .ppk file saved you can now use it to connect to your EC2 Instance.
Run PuTTY
Underneath the category section, in the Session pane enter your host name. For Linux instances it will be ec2-user@public-dns-name
Select Connection > SSH > Auth and browse for the .ppk file we created earlier.
aws4
If this is your first time connecting to this instance PuTTY displays a security alert, you can disregard it.
Once you enter your username you will be connected to your EC2 instance.
aws5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: