Ravindra BagaleCourses & study guides मराठी Track your progress

Chapter 5: Amazon EC2 (Elastic Compute Cloud)

5.4 Connecting to your instance

In my classes, this is where most students get stuck the first time – wrong username, wrong key permissions, or port 22 blocked. Don't worry, follow the steps below exactly and it will definitely work.

Default usernames

AMI SSH user
Amazon Linux 2023 / Amazon Linux 2 ec2-user
Ubuntu ubuntu
CentOS Stream 9 (official) ec2-user (older CentOS 7/8 AMIs: centos)
RHEL ec2-user
Debian admin
SUSE ec2-user

From Linux or macOS (OpenSSH)

cd ~/Downloads
chmod 400 mykey.pem                                  # private key must not be readable by others
ssh -i mykey.pem ec2-user@<PUBLIC_IP>                # Amazon Linux / CentOS Stream
ssh -i mykey.pem ubuntu@<PUBLIC_IP>                  # Ubuntu

Type yes when asked "Are you sure you want to continue connecting?" (first time only — it stores the server fingerprint in ~/.ssh/known_hosts).

This fingerprint (a unique mark that identifies the server) is saved on the first connection. The next time, SSH matches this fingerprint, which is why "yes" is asked only once.

UNPROTECTED PRIVATE KEY FILE!

If you see Permissions 0644 for 'mykey.pem' are too open, SSH refuses to use the key. Fix with chmod 400 mykey.pem. Never share your .pem, never upload it to GitHub, never e-mail it.

From Windows — option 1: built-in OpenSSH (Windows 10/11)

Open PowerShell and fix the key permissions (Windows equivalent of chmod 400):

cd $env:USERPROFILE\Downloads
icacls.exe mykey.pem /reset
icacls.exe mykey.pem /grant:r "$($env:USERNAME):(R)"
icacls.exe mykey.pem /inheritance:r
ssh -i .\mykey.pem ec2-user@<PUBLIC_IP>

From Windows — option 2: PuTTY

  1. Install PuTTY (includes PuTTYgen).
  2. If you downloaded a .pem: open PuTTYgen → Load → choose All files → select mykey.pem → Save private key as mykey.ppk.
  3. Open PuTTY → Session → Host Name: ec2-user@<PUBLIC_IP> (or ubuntu@...), Port 22.
  4. Connection → SSH → Auth → Credentials → Private key file for authentication → browse to mykey.ppk.
  5. Back to Session, type a name under Saved Sessions → Save → Open → Accept the host key.

From Windows — option 3: MobaXterm

  1. Install MobaXterm (Home edition is free).
  2. Session → SSH → Remote host: <PUBLIC_IP>, tick Specify username: ec2-user / ubuntu.
  3. Advanced SSH settings → tick Use private key → select mykey.pem (MobaXterm accepts .pem directly).
  4. OK. The left panel gives a graphical SFTP browser — drag-and-drop files to upload.

Other connection options

  • EC2 Instance Connect (browser-based): select instance → Connect → EC2 Instance Connect tab → Connect. Works out-of-the-box on Amazon Linux and Ubuntu AMIs when port 22 is open to the EC2 Instance Connect IP range for your region (or to 0.0.0.0/0).
  • Session Manager (AWS Systems Manager): no port 22 needed at all; requires the SSM agent and an IAM role (AmazonSSMManagedInstanceCore). Recommended in companies.

First commands after logging in

cat /etc/os-release                 # confirm distribution
sudo yum update -y                 # AL2023 / CentOS Stream
sudo apt update && sudo apt upgrade -y   # Ubuntu
curl -s http://169.254.169.254/latest/meta-data/ -H "X-aws-ec2-metadata-token: $(curl -s -X PUT http://169.254.169.254/latest/api/token -H 'X-aws-ec2-metadata-token-ttl-seconds: 60')"
sudo timedatectl set-timezone Asia/Kolkata

The first job after logging in: sudo yum update -y and the timezone Asia/Kolkata. The times of Mauli Dairy's orders should show in IST in the logs; otherwise the server stays in UTC and an 8 a.m. order shows up in the logs as 2:30 at night – and then there is confusion.

Instance metadata (IMDSv2)

169.254.169.254 is a special address inside every instance that returns its metadata — instance ID, public IP, AZ, IAM role credentials. New instances require IMDSv2 (token-based), which is why the command above first requests a token. Example: append public-ipv4 or placement/availability-zone to the URL.

Ravindra Bagale's Tip

Keep one folder like ~/aws-keys/ for all your .pem files, run chmod 400 on each file and give keys clear names (mumbai-lab-key.pem). If you lose a private key, AWS cannot give it back. And never, ever push a key to GitHub – bots find secrets in public repositories within minutes.

Ravindra Bagale's Tip

Read the error carefully – it tells you the answer. If you get Permission denied (publickey), check three things: the username (ec2-user or ubuntu, depending on the AMI), the key file (the same .pem of the key pair you launched with) and chmod 400. If you get Connection timed out, the problem is the network – check port 22 in the security group and your current IP.

Lab

Tumchya mauli-web-1 la teen prakare connect kara: OpenSSH (Linux/macOS kiwa Windows PowerShell), PuTTY (.ppk) aani EC2 Instance Connect. Pratyek veli cat /etc/os-release aani sudo yum update -y chalva. Mag muddam ubuntu@ username vaprun connect karun bagha aani kontya error yeto te lihun theva.