Aws Ssh To Ec2

Aws Ssh To Ec2

Reading time1 min
#AWS#Cloud#DevOps#EC2#SSH#Linux

How to SSH into Your AWS EC2 Instance: A Simple Step-by-Step Guide

If you’re getting started with Amazon Web Services (AWS) and want to remotely manage your virtual servers, one of the most essential skills is knowing how to securely SSH into your EC2 instance. Whether you’re deploying websites, running applications, or managing cloud infrastructure, accessing your EC2 instance via SSH is a fundamental task every developer and sysadmin should master.

In this post, I’ll walk you through the exact steps to SSH into an AWS EC2 instance—from setting up your key pairs to connecting to your server using your terminal. No fluff, just practical guidance.


Why SSH into an EC2 Instance?

Secure Shell (SSH) allows you to securely connect and interact with your cloud server’s command line interface over the internet. Once connected via SSH:

  • You can install software and updates
  • Manage services and applications
  • Transfer files using SCP or SFTP
  • Debug issues directly

Think of it as having remote control of your AWS virtual machine.


Prerequisites Before You Start

  1. An active AWS account
  2. An EC2 instance running (Linux-based in this example)
  3. The private key file (*.pem) associated with your EC2 key pair downloaded on your local machine
  4. Your public IP or DNS of the EC2 instance
  5. Basic familiarity with command line/terminal

Step 1: Launch an EC2 Instance & Download Key Pair

If you haven’t spun up an EC2 instance yet:

  • Log in to AWS Console > EC2 Dashboard > Launch Instance
  • Choose an Amazon Machine Image (AMI), e.g., Amazon Linux 2 AMI (x86)
  • Select instance type e.g., t2.micro for testing (free tier eligible)
  • Configure instance details as needed
  • In “Configure Security Group,” allow SSH (port 22) from your IP address
  • At “Key Pair,” create a new key pair or use existing → Download the .pem file

Your .pem file is crucial for authentication—keep it safe and never share publicly.


Step 2: Set Proper Permissions on Your Private Key

Before connecting from your local machine, the private key file must have restricted permissions. Otherwise, SSH will refuse to use it for security reasons.

Run this command (replace your-key.pem):

chmod 400 your-key.pem

This makes sure only you can read the file.


Step 3: Find Your EC2 Instance’s Public DNS or IP Address

In the EC2 dashboard:

  • Select Instances > Your running instance
  • Note its Public IPv4 DNS or IPv4 Public IP

Example:

ec2-18-119-12-34.us-east-2.compute.amazonaws.com

or

18.119.12.34

You’ll use this address to connect.


Step 4: Connect via SSH from Local Terminal

Open your terminal or command prompt and run:

ssh -i /path/to/your-key.pem ec2-user@ec2-public-dns-name

Example with real values:

ssh -i ~/Downloads/mykey.pem ec2-user@ec2-18-119-12-34.us-east-2.compute.amazonaws.com

Notes:

  • The default username depends on AMI:
    • Amazon Linux / Amazon Linux 2: ec2-user
    • Ubuntu AMI: ubuntu
    • CentOS AMI: centos

You can usually find this in the AMI documentation.

On first connect, SSH may warn about authenticity — type yes to continue.


Bonus: Using Windows or PuTTY?

Windows users can use PuTTY or Windows Subsystem for Linux (WSL).

For PuTTY:

  1. Convert .pem to .ppk using PuTTYgen.
  2. Use PuTTY GUI with username (ec2-user) and server public DNS.
  3. Load .ppk under Connection > SSH > Auth.

Alternatively, WSL users can follow the same native ssh commands as Linux/macOS.


Troubleshooting Tips

If connection fails:

  • Ensure port 22 is open in the Security Group inbound rules.
  • Verify you are using correct username based on AMI.
  • Confirm that .pem permissions are set to 400.
  • Check that you’re connecting from an authorized IP address allowed by Security Group.

Run these commands if needed:

aws ec2 describe-instances --instance-id i-yourinstanceid --query 'Reservations[*].Instances[*].[PublicIpAddress]' --output text

to verify IP address via AWS CLI.


Wrapping Up

Connecting to AWS EC2 instances via SSH is straightforward once you know the steps:

  1. Create/download key pair with secure permissions.
  2. Find instance’s public DNS/IP.
  3. Use proper ssh command syntax (ssh -i <key> <username>@<ip>)
  4. Troubleshoot network/security group issues if needed.

Mastering this simple yet powerful skill empowers you to fully manage cloud servers with ease from anywhere in the world.

If you want me to cover further aspects like automating SSH access, securing instances with advanced methods, or transferring files over SCP/SFTP — just let me know!

Happy cloud computing! ☁️🚀


Did you find this guide helpful? Feel free to share your questions or tips in the comments below!