AWS Certified Cloud Practitioner / EC2 - Elastic Compute Cloud

How to SSH using Linux and Mac

πŸ–ΌοΈ How SSH Works

  • Your Mac/Linux terminal β†’ connects via internet

  • Through port 22 (SSH)

  • Into your EC2 instance (Amazon Linux 2)


βš™οΈ Step 1: Prepare Your Key File

πŸ‘‰ You downloaded: During EC2 instance creation phase

EC2 Tutorial.pem

⚠️ Important Fix

Rename it to remove spaces:

EC2Tutorial.pem

Place It in a Folder

Example:

aws-course/
 └── EC2Tutorial.pem

πŸ’» Step 2: Navigate to Your File

Open terminal and go to your folder:

cd aws-course

Check if file exists:

ls

πŸ‘‰ You should see:

EC2Tutorial.pem

🧭 If You're Lost (Helpful Commands)

pwd     # shows current directory
ls      # lists files
cd ..   # go up one level
cd <folder-name>

🌐 Step 3: Get Your EC2 Public IP

From AWS Console:

  • Go to EC2 β†’ Instances

  • Copy Public IPv4 address

Example:

3.250.xxx.xxx

πŸ” Step 4: Check Security Group

Make sure your EC2 has:

  • βœ… Port 22 (SSH) open

  • Source: 0.0.0.0/0

πŸ‘‰ If not β†’ add it


πŸš€ Step 5: First SSH Attempt (Will Fail β€” That’s OK)

ssh ec2-user@<your-ip>

πŸ‘‰ Example:

ssh ec2-user@3.250.xxx.xxx

❌ You’ll likely see:

Permission denied / Authentication failure

πŸ‘‰ This is normal β€” you didn’t provide the key yet


πŸ”‘ Step 6: Use Your Key File

ssh -i EC2Tutorial.pem ec2-user@<your-ip>

❌ Step 7: Fix Permission Error

If you see:

UNPROTECTED PRIVATE KEY FILE!

πŸ‘‰ Run:

chmod 400 EC2Tutorial.pem

βœ… Step 8: Connect Successfully

Run again:

ssh -i EC2Tutorial.pem ec2-user@<your-ip>

πŸ‘‰ First time:

Are you sure you want to continue connecting? (yes/no)

Type:

yes

πŸŽ‰ You’re In!

You should now see:

ec2-user@ip-172-31-xx-xx:~$

πŸ§ͺ Try These Commands

whoami

πŸ‘‰ Output:

ec2-user
ping google.com

πŸ‘‰ Stop with:

Ctrl + C

πŸšͺ Step 9: Exit Instance

exit

πŸ” Reconnect Anytime

ssh -i EC2Tutorial.pem ec2-user@<your-ip>

⚠️ Reminder: Public IP may change after stop/start


⚠️ Common Mistakes

  • ❌ Wrong directory (can’t find .pem)

  • ❌ Wrong permissions (forgot chmod 400)

  • ❌ Missing port 22 in security group

  • ❌ Typo in IP or filename