How to connect to AWS ec2 instance from Ubuntu terminal

Are you using Ubuntu or any other Linux such as CentOS, Fedora, Mint, and want to connect to Amazon cloud running ec2 Instance, then here are the steps to follow.

Use Ubuntu Distro to Connect Amazon ec2 Instance

When we create an Amazon Elastic Compute Cloud Instance of Linux or Windows, the system automatically creates a Private key file in .pem format. In case you haven’t created one, then follow our tutorial to generate a new Keypair.

1. Download Keypair

If you have a fresh Ubuntu system then you can download the key pair assigned to your instance from Amazon Management Interface.

  • Go to your Ec2 Dashboard.
  • Click on the “Instance” option from the left panel.
  • After that select the Instance of Linux or Windows that you want to connect remotely.
  • As you select it, some information will be revealed, go to the “Key pair name” section and click on the AWS Ec2 to download the assigned key pair.

Download AWS EC2 Key pair


2. Open Command terminal

If you are using a graphical Ubuntu then simply press the Ctrl+Alt+T shortcut to open the Terminal app. Whereas the CLI server users are already on the command line.


3. Switch to the directory where the key is saved

On your Ubuntu Terminal switch to the directory where you have saved the downloaded Pem private key of your Instance.

For example, if it is in the Downloads directory, then simply switch to that:

cd Downloads


4. Change Key Permission

For security reasons, we have to change the permission for the key so that it can only be readable by the user and removes all other permissions.

chmod 400 AWS EC2.pem


5. Connect to your Instance using Publix Ip-address or DNS

Now, from your Ec2 Instance page either note down the public ipv4 IP address or DNS

Copy Public Ip adress or DNS

Syntax to connect

ssh -i path-to-key ec2-user@ip-address


ssh -i ~/Downloads/AWSEC2.pem [email protected]


ssh -i ~/Downloads/AWSEC2.pem

Connect AWS ec2 from Ubuntu

Set an environment variable for the Private PEM key

Pro Tip:  On Linux, you can set an environment variable for your key so that we can use it globally without specifying its path again and again:

ssh -i $aws_key ec2-user@instace-ip-address

Declare Key path as environmental key


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.