EC2 Initial Setup and SSH Connection
On This Page
Step 1: Launch an EC2 Instance
- Go to the AWS Console → Navigate to EC2.
- Click Launch Instance.
- Choose an AMI (e.g., Amazon Linux, Ubuntu).
- Select an instance type (e.g.,
t2.micro
for free tier). - Configure instance details (default settings are fine).
- Add storage (default 8GB is sufficient, adjust as needed).
- Configure security groups:
- Allow SSH (port 22) from
My IP
(recommended). - Add rules for other services (e.g., HTTP, HTTPS) if needed.
- Allow SSH (port 22) from
- Choose or create a key pair (important for SSH access).
- Click Launch.
Step 2: Connect to EC2 via SSH
Using a .pem
Key (Linux/macOS)
Move the key to a secure location:
mv ~/Downloads/my-key.pem ~/.ssh/
Set permissions:
chmod 400 ~/.ssh/my-key.pem
Connect:
ssh -i ~/.ssh/my-key.pem admin@your-ec2-public-ip # ssh -i ~/.ssh/my-key.pem admin@52.31.239.202
- Replace
ec2-user/admin
withubuntu
for Ubuntu AMIs. - Replace your-ec2-public-ip with the instance’s public IP.
Using PuTTY (Windows)
- Convert .pem to .ppk using PuTTYgen.
- Open PuTTY.
- Under Session, enter your-ec2-public-ip in Host Name.
- Go to SSH → Auth, load the .ppk file.
- Click Open.
Step 3: Configure Automatic SSH Login
Method 1: Add Key to SSH Agent (Recommended)
Start the SSH agent (if required):
eval "$(ssh-agent -s)"
Add the key:
ssh-add ~/.ssh/my-key.pem
Now, connect with:
ssh admin@your-ec2-public-ip
Method 2: Add Key to SSH Config File
Open (or create) the SSH config file:
nano ~/.ssh/config
Add the following:
Host my-ec2 HostName your-ec2-public-ip User ec2-user IdentityFile ~/.ssh/my-key.pem
Save and exit (
CTRL+X
, thenY
, thenENTER
).Now, connect easily with:
ssh my-ec2
Connected
Once connected: