← Back to Notes
AWSGUIDE 01

Connect Local Terminal to an AWS EC2 Server

This guide explains how to securely connect your local terminal to an AWS EC2 Linux server using an SSH private key. It covers finding your EC2 public IP, preparing the PEM key, connecting to Ubuntu or Amazon Linux, verifying the server and creating an SSH shortcut.

AWSEC2SSHLinuxUbuntuAmazon LinuxServer
✓
BEFORE YOU START

Requirements

✓A running AWS EC2 instance
✓The EC2 Public IPv4 address or Public DNS
✓The EC2 private key file (.pem)
✓SSH port 22 allowed in the EC2 security group
✓SSH installed on your local computer
STEP-BY-STEP GUIDE

Follow these steps

Execute each step in order. Copy the commands directly when needed.

01

Get your EC2 public IP address

Open the AWS Console, go to EC2, open Instances, select your running server and copy either the Public IPv4 address or Public IPv4 DNS.

Example EC2 public IP

Replace this example with your actual EC2 Public IPv4 address.

all
TERMINAL
13.234.123.45
02

Locate your PEM private key

When the EC2 instance was created, AWS provided a private key file ending in .pem. You need this key to authenticate with the server.

IMPORTANT

Never share your .pem private key, upload it publicly, or commit it to Git.

Check the Downloads directory

Check whether your EC2 PEM key is stored in Downloads.

linux
TERMINAL
ls ~/Downloads

Find all PEM files

Search your home directory for PEM files.

linux
TERMINAL
find ~ -name "*.pem"

Example result

Your PEM file may be located somewhere similar to this.

linux
TERMINAL
/home/your-user/Downloads/my-ec2-key.pem
03

Create your SSH directory

Store your private SSH keys inside the local .ssh directory.

Create SSH directory

This command safely creates the directory if it does not already exist.

linux
TERMINAL
mkdir -p ~/.ssh

Check the SSH directory

Verify that the directory exists.

linux
TERMINAL
ls ~/.ssh
04

Move the PEM key into your SSH directory

Move your EC2 private key from Downloads or its current location into the local SSH directory.

Move your EC2 PEM key

Replace my-ec2-key.pem with your actual PEM filename.

linux
TERMINAL
mv ~/Downloads/my-ec2-key.pem ~/.ssh/

Verify the PEM key

Check that the private key is now inside the SSH directory.

linux
TERMINAL
ls ~/.ssh
05

Set secure permissions on the PEM file

AWS SSH keys must have restricted permissions. If the private key is too publicly accessible, SSH will refuse to use it.

IMPORTANT

Do not change the PEM key permissions to allow public or group access.

Secure the private key

Replace my-ec2-key.pem with your actual PEM filename.

linux
TERMINAL
chmod 400 ~/.ssh/my-ec2-key.pem

Verify key permissions

Check the permissions applied to your private key.

linux
TERMINAL
ls -l ~/.ssh/my-ec2-key.pem
06

Connect to an Ubuntu EC2 server

Most Ubuntu EC2 instances use ubuntu as the default SSH username. Replace YOUR_EC2_PUBLIC_IP with your actual EC2 public IP.

Connect to Ubuntu EC2

Use your PEM key and EC2 public IP address.

all
TERMINAL
ssh -i ~/.ssh/my-ec2-key.pem ubuntu@YOUR_EC2_PUBLIC_IP

Example connection

Example using a sample EC2 public IP address.

all
TERMINAL
ssh -i ~/.ssh/my-ec2-key.pem ubuntu@13.234.123.45
07

Accept the server fingerprint

The first time you connect to a new EC2 server, SSH may ask you to confirm the server fingerprint.

Confirm connection

Type yes when SSH asks whether you want to continue connecting.

all
TERMINAL
yes
08

Connect to an Amazon Linux EC2 server

Amazon Linux instances commonly use ec2-user as the SSH username instead of ubuntu.

Connect to Amazon Linux

Replace YOUR_EC2_PUBLIC_IP with your actual EC2 public IP.

all
TERMINAL
ssh -i ~/.ssh/my-ec2-key.pem ec2-user@YOUR_EC2_PUBLIC_IP

Example Amazon Linux connection

all
TERMINAL
ssh -i ~/.ssh/my-ec2-key.pem ec2-user@13.234.123.45
09

Verify that you are connected to the server

After a successful connection, your terminal prompt will change to the remote server. Run the following commands to inspect the server.

Check current user

Verify which Linux user you are logged in as.

all
TERMINAL
whoami

Check server information

View hostname and operating system information.

linux
TERMINAL
hostnamectl

Check current directory

Display the directory you are currently working in.

all
TERMINAL
pwd

List files

Display files and directories including hidden files.

all
TERMINAL
ls -la
10

Exit the EC2 server

When you finish working on the remote server, exit the SSH session to return to your local terminal.

Exit SSH session

all
TERMINAL
exit
11

Create a simple SSH shortcut

Instead of typing the complete SSH command every time, you can create an SSH configuration entry and connect using a short name.

Open SSH configuration file

Create or edit your local SSH configuration file.

linux
TERMINAL
nano ~/.ssh/config

Add EC2 SSH configuration

Replace the hostname, username and key filename with your actual values.

linux
TERMINAL
Host my-ec2
    HostName 13.234.123.45
    User ubuntu
    IdentityFile ~/.ssh/my-ec2-key.pem

Secure the SSH configuration

Restrict access to your SSH configuration file.

linux
TERMINAL
chmod 600 ~/.ssh/config

Connect using the shortcut

After configuring the Host entry, connect with the short name.

all
TERMINAL
ssh my-ec2
12

Complete command flow

The following commands show the complete Linux workflow from finding your PEM key to connecting to the EC2 server.

Complete EC2 SSH workflow

Replace the PEM filename and EC2 public IP with your actual values.

linux
TERMINAL
# Find PEM key
find ~ -name "*.pem"

# Create SSH directory
mkdir -p ~/.ssh

# Move key
mv ~/Downloads/my-ec2-key.pem ~/.ssh/

# Secure key
chmod 400 ~/.ssh/my-ec2-key.pem

# Connect to Ubuntu EC2
ssh -i ~/.ssh/my-ec2-key.pem ubuntu@YOUR_EC2_PUBLIC_IP
TROUBLESHOOTING

Common problems

Permission denied (publickey)

Verify that you are using the correct PEM private key and the correct SSH username. Ubuntu usually uses ubuntu, while Amazon Linux commonly uses ec2-user.

Connection timed out

Check that the EC2 instance is running and verify that the EC2 security group allows inbound SSH traffic on port 22 from your current IP address.

Unprotected private key file

Restrict the PEM file permissions with chmod 400 ~/.ssh/your-key.pem and make sure the key is not accessible by other users.

Could not resolve hostname

Verify that you copied the EC2 Public IPv4 address or Public DNS correctly and that you did not accidentally include extra characters.

The SSH shortcut does not work

Check the Host, HostName, User and IdentityFile values inside ~/.ssh/config and make sure the PEM key path is correct.