How To Setup and SSH into an Ubuntu 18.04 Virtual Machine on AWS

July 5, 2020

Many modern web applications today are served from the cloud. Public cloud providers, like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, to name a few, offer service models that enable this trend.

Introduction

An Introduction To Cloud Computing can be found on Section’s Engineering Education blog.

Web applications can be served on Virtual Machines provisioned on a public cloud. In this tutorial, we will cover exactly how to provision a Virtual Machine running Ubuntu 18.04 on AWS, and SSH into it.

The tutorial takes advantage of the FREE services offered under the AWS Free Tier. We will use the Infrastructure As A Service Virtual Machine offering Amazon EC2.

Note: Under the Free tier (for eligible users), you get 750 hours per month (up to 12 months) of a Linux t2.micro or t3.micro instance, dependent on region. This is subject to Amazon’s terms and conditions. You should thus ensure that you are eligible first, in order to avoid being billed for the services you will use in this tutorial.

Prerequisites

To complete this tutorial you will need to have an AWS account and a Linux machine or SSH client that you will use to SSH into the virtual machine.

Launch an Amazon EC2 Virtual Machine

  1. Navigate to the AWS console.
  2. On the navbar, navigate to Services > Compute > EC2. This will take you to the EC2 console.
  3. Scroll down to the “Launch Instance” prompt and click on it.
  4. You will then navigate to a page where you get to choose an Amazon Machine Image.
  5. Under QuickStart, you should select the Ubuntu Server 18.04 LTS that is Free tier eligible. Choose an Amazon Machine Image
  6. Navigate to the tab where you select the Instance type. For this case, a General Purpose t3.micro instance that is Free tier eligible is sufficient. Select Instance Type
  7. Click the button labeled “Next: Configure Instance Details”.
  8. On the next three subsequent pages, you do not need to make any changes. (Click the buttons labelled “Next: Add storage”, “Next: Add Tags” and “Configure Security Group” respectively.)
  9. On the Configure Security Group page, select create a new security group and leave everything else as is. (Ignore the displayed warning.) Click “Review and Launch”. Configure Security Group
  10. On the Launch Page, click on “Launch”, which will trigger a prompt. Select “create a new key pair”. Name the key pair and download it. (Later on, you will use your_pem_file.pem to SSH into the Virtual Machine instance you are about to launch.) When done, you can then click on “Launch Instances”. Launch Instance

CONGRATULATIONS!! You have managed to set up and launch your own Virtual Machine Instance. Now you can move on to connecting to it remotely via the SSH protocol.

SSH into the Amazon EC2 Virtual Machine

The operating system of your local computer determines the options available to SSH from your local computer to your Virtual Machine instance running Linux (Ubuntu 18.04).

Moving forward, the tutorial assumes your local computer operating system is Linux or macOS X. If this isn’t the case, see Amazon’s guide.

First, you need to locate your private key file (your_pem_file.pem) that you downloaded earlier, and move it to the ~/.ssh directory. To do this, from the terminal in the directory where your_pem_file.pem was downloaded, run:

mv your_pem_file.pem ~/.ssh/your_pem_file.pem

Next, you need to ensure your private key file (your_pem_file.pem) is not publicly viewable for SSH to work. To do this, run:

chmod 400 ~/.ssh/your_pem_file.pem

To gain an understanding of how the chmod command works, read up on Linux File Permissions.

At this point, you’re almost ready to SSH into your Virtual Machine Instance. You need one last thing – your Virtual Machine’s IP address. You can get this from the AWS console, as shown in the image below. Virtual Machine IP address
Now you can ssh into your instance by running the ssh command:

ssh -i ~/.ssh/your_pem_file.pem ubuntu@your_virtual_machine_ip_address -v

You will then get a response like:

The authenticity of host '13.244.115.171 (13.244.115.171)' can't be established.
ECDSA key fingerprint is SHA256:hTaJnzw/oDXxzLCMHcp9ieHcHxEISfxubiEkylIhtkc.
Are you sure you want to continue connecting (yes/no)?

Enter yes.

And You DID IT!!! You have successfully used SSH to get remote access to your Virtual Machine on AWS. Feel free to poke around!!

To better understand the ssh command you used above, see the breakdown below.

  1. ssh: the ssh command is used to start the SSH client program that enables secure connection to the SSH server on a remote machine.
  2. -i: identity_file, a file from which the identity key (private key) for public key authentication is read.
  3. “~/.ssh/your_pem_file.pem”: the absolute path to your_pem_file.pem
  4. ubuntu: the username you will use to ssh into the virtual machine.
  5. @: used to refer the virtual machine IP address much like @gmail denotes a Gmail email servers.
  6. your_virtual_machine_ip_address: used to denote the IP address you wish to SSH into.
  7. -v: verbose mode, where every action is printed to the screen as it happens.

For more information read SSH Command. I also suggest that you see ssh-add and ssh-agent to add your_pem_file.pem to the ssh-agent. The ssh-add command adds private key identities like your_pem_file.pem to the authentication agent(ssh-agent) so that the ssh agent can take care of authentication for you. Shortening the ssh command you used earlier to:

ssh ubuntu@your_virtual_machine_ip_address -v

When you’re done, exit the SSH connection by running:

logout

Note: Remember to terminate the instance to avoid charges when the trial period ends.

To terminate the instance, right click on the instance in the console:

Instance State > Terminate

Terminate Virtual Machine Instance


About the author

Adrian is an undergraduate student pursuing a degree in Applied Computer Technology. Adrian loves technical writing, contributing to open source projects, and involving himself in the creation of learning material for aspiring software engineers.