How to SSH into EC2 Instance: Simple Steps for AWS Access
To SSH into an EC2 instance, use the
ssh command with your private key file and the instance's public DNS or IP address like this: ssh -i /path/to/key.pem ec2-user@ec2-instance-address. Make sure your key file has the right permissions and your instance's security group allows SSH (port 22).Syntax
The basic SSH command to connect to an EC2 instance is:
ssh: The command to start a secure shell session.-i /path/to/key.pem: Specifies the private key file for authentication.ec2-user@ec2-instance-address: The username and public DNS or IP of the EC2 instance.
The username depends on the instance OS: ec2-user for Amazon Linux, ubuntu for Ubuntu, etc.
bash
ssh -i /path/to/key.pem ec2-user@ec2-instance-address
Example
This example shows how to SSH into an Amazon Linux EC2 instance using a private key file named mykey.pem and the instance's public DNS ec2-203-0-113-25.compute-1.amazonaws.com.
bash
ssh -i mykey.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com
Output
Warning: Permanently added 'ec2-203-0-113-25.compute-1.amazonaws.com' (ECDSA) to the list of known hosts.
[ec2-user@ip-172-31-16-139 ~]$
Common Pitfalls
- Incorrect key permissions: The private key file must have permissions set to
chmod 400to be secure. - Wrong username: Using the wrong username (like
root) will cause connection failure. - Security group blocking port 22: Ensure the EC2 instance's security group allows inbound SSH traffic on port 22.
- Using the wrong IP or DNS: Use the public IP or DNS if connecting from outside the VPC.
bash
chmod 400 mykey.pem ssh -i mykey.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com
Quick Reference
| Step | Action |
|---|---|
| 1 | Set key file permissions: chmod 400 key.pem |
| 2 | Find instance public DNS or IP |
| 3 | Use correct username (ec2-user, ubuntu, etc.) |
| 4 | Run SSH command: ssh -i key.pem username@public-dns |
| 5 | Ensure security group allows port 22 inbound |
Key Takeaways
Use the SSH command with the -i option to specify your private key file.
Set your private key file permissions to 400 to keep it secure.
Use the correct username for your EC2 instance's operating system.
Make sure your EC2 security group allows inbound SSH traffic on port 22.
Connect using the instance's public DNS or IP address from outside the VPC.