Launch a Linux Virtual Machine

with Amazon EC2

Amazon Elastic Compute Cloud (EC2) is the Amazon Web Service you use to create and run virtual machines in the cloud. AWS calls these virtual machines 'instances'. This step-by-step guide will help you successfully launch a Linux virtual machine on Amazon EC2 within our AWS Free Tier.

Did you know? AWS made it even easier to launch a Linux virtual private server. Jumpstart your virtual machine with Amazon Lightsail >>

About this Tutorial
Time 10 minutes
Cost Less than $1
Use Case Compute
Products Amazon EC2
Level Beginner

Step 1. Sign up for AWS

There are no additional charge for Amazon EC2 for this tutorial. The resources you create in this tutorial are Free Tier eligible. The button and the link open a new tab so you can follow this tutorial in the AWS console.

 

Already have an account? Sign-in


Step 2. Launch an Amazon EC2 Instance


a.  Open the AWS Management Console, so you can keep this step-by-step guide open. When the screen loads, enter your user name and password to get started. Then type EC2 in the search bar and select Amazon EC2 to open the service console.

launch-instance
launch-instance

( click to enlarge )


b. Select Launch Instance to create and configure your virtual machine.

launch-instance
launch-instance

( click to enlarge )


Step 3. Configure your Instance

You are now in the EC2 Launch Instance Wizard, which will help you configure and launch your instance.


a.  In this screen, you are shown options to choose an Amazon Machine Image (AMI). AMIs are preconfigured server templates you can use to launch an instance. Each AMI includes an operating system, and can also include applications and application servers.

For this tutorial, find Amazon Linux AMI and click Select.

amazon-linux-ami
amazon-linux-ami

( click to enlarge )


b. You will now choose an instance type. Instance types comprise of varying combinations of CPU, memory, storage, and networking capacity so you can choose the appropriate mix for your applications. For more information, see Amazon EC2 Instance Types.

The default option of t2.micro should already be checked. This instance type is covered within the Free Tier and offers enough compute capacity to tackle simple workloads. Click Review and Launch at the bottom of the page.

ec2-t2-micro
ec2-t2-micro

( click to enlarge )


c. You can review the configuration, storage, tagging, and security settings that have been selected for your instance. While you have the option to customize these settings, we recommend accepting the default values for this tutorial.

Click Launch at the bottom of the page.

review-launch
review-launch

( click to enlarge )


d. On the next screen you will be asked to choose an existing key pair or create a new key pair. A key pair is used to securely access your Linux instance using SSH. AWS stores the public part of the key pair which is just like a house lock. You download and use the private part of the key pair which is just like a house key.

Select Create a new key pair and give it the name MyKeyPair. Next click the Download Key Pair button.

After you download the MyKeyPair key, you will want to store your key in a secure location. If you lose your key, you won't be able to access your instance. If someone else gets access to your key, they will be able to access your instance.

Windows users: We recommend saving your key pair in your user directory in a sub-directory called .ssh (ex. C:\user\{yourusername}\.ssh\MyKeyPair.pem).

Tip: You can't use Windows Explorer to create a folder with a name that begins with a period unless you also end the folder name with a period. After you enter the name (.ssh.), the final period is removed automatically.

Mac/Linux users: We recommend saving your key pair in the .ssh sub-directory from your home directory (ex. ~/.ssh/MyKeyPair.pem).

Tip: On MacOS, the key pair is downloaded to your Downloads directory by default. To move the key pair into the .ssh sub-directory, enter the following command in a terminal window: mv ~/Downloads/MyKeyPair.pem ~/.ssh/MyKeyPair.pem

After you have stored your key pair, click Launch Instance to start your Linux instance.

Getting-Started-VM9
Getting-Started-VM9

( click to enlarge )


e. Click View Instances on the next screen to view your instances and see the status of the instance you have just started.

Getting-Started-VM8
Getting-Started-VM8

( click to enlarge )


f. In a few minutes, the Instance State column on your instance will change to "running" and a Public IP address will be shown. You can refresh these Instance State columns by pressing the refresh button on the right just above the table. Copy the Public IP address of your AWS instance, so you can use it when we connect to the instance using SSH in Step 4.

Getting-Started-VM6
Getting-Started-VM6

( click to enlarge )


Step 4. Connect to your Instance

After launching your instance, it's time to connect to it using SSH.

Windows users: Select Windows below to see instructions for installing Git Bash which includes SSH.

Mac/Linux user: Select Mac / Linux below to see instructions for opening a terminal window.

  • Windows
  • Mac / Linux
  • Windows
  • a. Download Git for Windows here. Run the downloaded installer accepting the default settings (this will install Git Bash as part of Git).

    Getting-Started-VM1
    Getting-Started-VM1

    ( click to enlarge )


    b. Right click on your desktop (not on an icon or file) and select Git Bash Here to open a Git Bash command prompt.

    Getting-Started-VM2
    Getting-Started-VM2

    ( click to enlarge )


    c. Use SSH to connect to your instance. In this case the user name is ec2-user, the SSH key is stored in the directory we saved it to in step 3 part d, and the IP address is from step 3 part f. The format is:

    ssh -i {full path of your .pem file} ec2-user@{instance IP address}

    Enter the following:

    ssh -i 'c:\Users\yourusername\.ssh\MyKeyPair.pem' ec2-user@{IP_Address}

    Example: ssh -i 'c:\Users\adamglic\.ssh\MyKeyPair.pem' ec2-user@52.27.212.125

    You'll see a response similar to the following:

    The authenticity of host 'ec2-198-51-100-1.compute-1.amazonaws.com (10.254.142.33)' can't be established. RSA key fingerprint is 1f:51:ae:28:df:63:e9:d8:cf:38:5d:87:2d:7b:b8:ca:9f:f5:b1:6f. Are you sure you want to continue connecting (yes/no)?

    Type yes and press enter.

    Getting-Started-VM4
    Getting-Started-VM4

    ( click to enlarge )


    d. You'll see a response similar to the following:

    Warning: Permanently added 'ec2-198-51-100-1.compute-1.amazonaws.com' (RSA) to the list of known hosts.

    You should then see the welcome screen for your instance and you are now connected to your AWS Linux virtual machine in the cloud.

    Getting-Started-VM7
    Getting-Started-VM7

    ( click to enlarge )

  • Mac / Linux
  • a. Your Mac or Linux computer most likely includes an SSH client by default. You can check for an SSH client by typing ssh at the command line. If your computer doesn't recognize the command, the OpenSSH project provides a free implementation of the full suite of SSH tools that you can download.

    Mac users: Open a terminal window by pressing Command + Space and typing terminal in the search window. Then press enter to open the terminal window.

    Linux users: Open a terminal window.

     

    Getting-Started-CLI-OSX1
    Getting-Started-CLI-OSX1

    ( click to enlarge )


    b. Use the chmod command to make sure your private key file is not publicly viewable by entering the following command to restrict permissions to your private SSH key:

    chmod 400 ~/.ssh/mykeypair.pem

    You do not need to do this every time you connect to you instance, you only need to set this once per SSH key that you have.

    Getting-Started-VM3
    Getting-Started-VM3

    ( click to enlarge )


    c. Use SSH to connect to your instance. In this case the user name is ec2-user, the SSH key is stored in the directory we saved it to in step 3 part d, and the IP address is from step 3 part f. The format is:

    ssh -i {full path of your .pem file} ec2-user@{instance IP address}

    Enter the following:

    ssh -i ~/.ssh/MyKeyPair.pem ec2-user@{IP_Address}

    Example: ssh -i ~/.ssh/MyKeyPair.pem ec2-user@52.27.212.125

    You'll see a response similar to the following:

    The authenticity of host 'ec2-198-51-100-1.compute-1.amazonaws.com (10.254.142.33)' can't be established. RSA key fingerprint is 1f:51:ae:28:df:63:e9:d8:cf:38:5d:87:2d:7b:b8:ca:9f:f5:b1:6f. Are you sure you want to continue connecting (yes/no)?

    Type yes and press enter.

    Getting-Started-VM4
    Getting-Started-VM4

    ( click to enlarge )


    d. You'll see a response similar to the following:

    Warning: Permanently added 'ec2-198-51-100-1.compute-1.amazonaws.com' (RSA) to the list of known hosts.

    You should then see the welcome screen for your instance and you are now connected to your AWS Linux virtual machine in the cloud.

    Getting-Started-VM7
    Getting-Started-VM7

    ( click to enlarge )


Step 5. Terminate your Instance

You can easily terminate the instance from the EC2 console. In fact, it is a best practice to terminate instances you are no longer using so you don’t keep getting charged for them.


a.  Back on the EC2 Console, select the box next to the instance you created. Then click the Actions button, navigate to Instance State, and click Terminate.

launch-windows-vm-18
launch-windows-vm-18

( click to enlarge )


b. You will be asked to confirm your termination - select Yes, Terminate.

Note: This process can take several seconds to complete.  Once your instance has been terminated, the Instance State will change to terminated on your EC2 Console.

launch-windows-vm-19
launch-windows-vm-19

( click to enlarge )


Congratulations!

You have learned how to use Amazon EC2 to launch, configure, connect, and terminate an instance in the cloud.

Amazon EC2 is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. You can use Amazon EC2 for a variety of applications, including websites and web applications, development and test environments, and even back-up and recovery scenarios. Amazon EC2 offers a wide selection of instance types with varying combinations of CPU, memory, storage, and networking capacity that you can use to meet the unique needs of your applications.

Launch a WordPress Website

Set-up a simple blog in less than 10 minutes. View the tutorial >>

Get a Domain

Learn how to register a domain name and attach it to an instance. View the tutorial >>

Store and Retrieve a File

Securely store and access files at scale in the AWS Cloud. View the tutorial >>