About Me

I have decades of experience is software development using .Net Technologies, PHP and wordpress. I love coding and discovering new tech.

Blog

Connect to AWS Ec2 Instance using Terminal on Mac

Linux, Server Management

Connect to AWS Ec2 Instance using Terminal on Mac

Posted on July 1, 2022  - By Kaustav Halder - 0 Comments

One of the best things I love about the MAC is the number of tools that are inbuild. One of them is the Terminal. Ya windows also have the Power Shell but if you have used the terminal on the mac, you would know what I mean and why I love it so much.

One of the most important tasks I need the Terminal for, other than to install some Homebrew apps, is to connect to Aws EC2 Linux instances using SSH. And let me tell you this, using the terminal to run commands on Linux SSH instances is just SUPERB!

So how do you connect to AWS EC2 instances using the terminal

To connect to an EC2 instance via SSH using the MAC terminal, you will need the following 3 things.
  1. The IP or hostname for the EC2 instance
  2. The UserName
  3. A security file called the PEM file which usually has the extension .pem or in some cases .cer.

Some Pre Assumptions

Ok, so there are a couple of things we need to get clear before we actually begin connecting to the server.
  1. The EC2 instance is an Amazon EC2 instance running some form of Linux. For the example, I am using ubuntu.
  2. The EC2 is accessible publicly or is set to be accessible to your current IP address
  3. The EC2 instance already has an SSH connection allowed. The default SSH port is 22 and for this example, we will be using the default port only.

Setting up the PEM file permissions correctly

If you are trying to connect to an EC2 instance using the PEM file and the terminal, then there is one hurdle that you will have to cross. That’s you will have to set the permissions for the PEM file correctly. The terminal will not allow you to use a PEM file that is accessible to others and everyone. And it should only have “Read Only” access from you. No writing to Pem files 😕. So let’s see how to do that.
  1. Navigate to the properties of the PEM file. (Hoping you already know how to navigate to the PEM file folder else you better learn how to use a MAC first 🥶).
  2. Next, you need to unlock the settings by clicking the lock icon on the bottom right. (You will need to provide your password for this 🔓)
  3. Then remove everything other than your account and Everyone.
  4. Set the permission for Everyone to “No Access”
  5. Set your permission to “Read Only”.
  6. Again click on the lock symbol.

  7. Nicely done. You have now successfully set up the file to be used

Time to Tango

So now that we have all the ingredients set up to make the SSH connection recipe, let us see the steps to finally connect to our instance.
After all the heavy lifting work the task should now be as simple as the below command.

ssh -i “[PEMFILE LOCATION][UserName]@[Server IP/HOST]

 

In the above command replace the following keywords:

  1. [PEMFILE LOCATION] will be replaced with the full path to your PEM file.
  2. [UserName] will be replaced with the user name to connect to the EC2 instance.
  3. And finally [Server IP/HOST] will be replaced with your server IP or the hostname for your EC2 instance.
Once you have made all the replacements to the command, then just paste it on the terminal window and hit enter. You should now be successfully connected to your Linux EC2 instance running on AWS.
Cheers! Have a nice day.


About Kaustav

I have decades of experience is software development using .Net Technologies, PHP and wordpress. I love coding and discovering new tech.


0 Comments

Be the first to comment


Leave a reply

Leave a Reply

Your email address will not be published. Required fields are marked *