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.
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.
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.
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
- Navigate to the AWS console.
- On the navbar, navigate to Services > Compute > EC2. This will take you to the EC2 console.
- Scroll down to the “Launch Instance” prompt and click on it.
- You will then navigate to a page where you get to choose an Amazon Machine Image.
- Under QuickStart, you should select the Ubuntu Server 18.04 LTS that is Free tier eligible.
- 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.
- Click the button labeled “Next: Configure Instance Details”.
- 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.)
- 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”.
- 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”.
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.
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 '184.108.40.206 (220.127.116.11)' can't be established. ECDSA key fingerprint is SHA256:hTaJnzw/oDXxzLCMHcp9ieHcHxEISfxubiEkylIhtkc. Are you sure you want to continue connecting (yes/no)?
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.
- ssh: the ssh command is used to start the SSH client program that enables secure connection to the SSH server on a remote machine.
- -i: identity_file, a file from which the identity key (private key) for public key authentication is read.
- “~/.ssh/your_pem_file.pem”: the absolute path to your_pem_file.pem
- ubuntu: the username you will use to ssh into the virtual machine.
- @: used to refer the virtual machine IP address much like @gmail denotes a Gmail email servers.
- your_virtual_machine_ip_address: used to denote the IP address you wish to SSH into.
- -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:
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
About the authorAdrian Murage
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.