Securely SSH into AWS EC2 Linux instance
In this article, we will learn how to configure an EC2 instance in AWS cloud and connect through SSH
First of all, we need an account to access the AWS console. I am using my free tier account for this article.
Login to the AWs console account. It will launch the Home screen of the AWS console. We need to go to the Services > EC2 section of the console. EC2 is available under the Compute section.
After selection EC2 service, it will launch the EC2 Dashboard where we can see the list of running instances and other details related to the EC2 service.
To launch a new EC2 instance, we need to click on the Launch Instance button. By clicking on the Launch Instance button it will ask to select Amazon Machine Instance (AMI). I selected the Amazon Linux (free tier) AMI. Because of two reasons, I selected Amazon Linux:
- Free tier
- Amazon Linux comes with default AWS CLI configured
The next step is to select Instance type. In my case, I have selected t2.micro because this is free tier eligible.
Then it will navigate to Review and Launch screen.
After reviewing all the configurations, click on the Launch button. Then it will ask for the key pair. Either we can use an existing key pair or create a new one. In my case, I have created a new key pair and clicked on the “Download Key Pair” button to download the key pair in the form of a .pem file.
A key pair consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely. For Windows AMIs, the private key file is required to obtain the password used to log into your instance. For Linux AMIs, the private key file allows you to securely SSH into your instance.
Then click on “Launch Instances” button and then click on “View Instances” button to view the list of instances and their status.
After launching a new instance, it initially shows Instance State as “pending” then after some time, it will show “running”. Below are the status options:
- pending: just after launching a new instance
- running: currently active and running
- stopped: Instance is stopped
- terminated: After terminating an instance. This status shows for some time, then instance will be removed from the list automatically.
Securely SSH into the EC2 instance
Now, time to connect to the instance using terminal or we can say securely SSH into the instance.
Select the EC2 instance into which you want to SSH. Then click on Actions > Connect button.
This will launch “Connect To Your Instance” popup:
Open the terminal and run below command:
sudo chmod 400 ec2-amazon-linux.pem
In the above command, ec2-amazon-linux.pem is the .pem file name. This needs the full path to the .pem file.
Now, ssh into the instance:
sudo ssh -i ec2-amazon-linux.pem ec2-user@ec2–3–84–75–186.compute-1.amazonaws.com
After successfully running above command, your terminal will appear like:
To verify if we have SSH into the EC2 virtual machine or not, we can try few commands:
In the above image, I have created one file (foo.txt) and ran ls command the list of file files in the /home/ec2-user folder.
Hope this helps. Please share your feedback.
Thanks!