Troubleshooting SSH Connection Issues with AWS EC2 Instances

BharteeTechRubyOnRails
2 min readOct 27, 2023

--

Problem 1: Connection Timed Out:

Issue:

You try to SSH into your AWS EC2 instance using a command like this:

ssh -i "pem_file_name.pem" ubuntu@ec2-here_is_your_ip.ap-south-1.compute.amazonaws.com

But you encounter the following error:

ssh: connect to host ec2-here_is_your_ip.ap-south-1.compute.amazonaws.com port 22: Connection timed out

Solutions:

2. Restart Your EC2 Instance:

Sometimes, instances can become unresponsive. In such cases, a simple solution is to restart the instance through the AWS Management Console.

2. Inbound Security Group Configuration:

Make sure your EC2 instance’s inbound security group allows SSH traffic from your IP address. To do this:
— Go to the AWS Management Console.
— Navigate to the EC2 dashboard.
— Select your instance.
— In the “Security groups” section, click on the associated security group.
— In the “Inbound rules” tab, add a rule that allows SSH (port 22) traffic from your computer’s IP address. Select “My IP” instead of “Custom” or “Any location” for the source.

Problem 2: Bad Permissions on Private Key File:

Issue:

You attempt to SSH into your EC2 instance using the private key file:

ssh -i "pem_file_name.pem ssh -i "pem_file_name.pem" ubuntu@ec2-here_is_your_ip_name.ap-south-1.compute.amazonaws.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0664 for 'pem_file_name.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "pem_file_name.pem": bad permissions
ubuntu@ec2-here_is_your_ip_name.ap-south-1.compute.amazonaws.com: Permission denied (publickey).

However, you encounter an error warning about unprotected private key file and bad permissions, and you are denied permission to access the instance.

Solution:

The issue is related to the permissions on the private key file. To fix this:

1. Open a terminal on your local machine.

2. Navigate to the directory containing your private key file (pem_file_name.pem).

3. Run the following command to change the permissions of the key file:

chmod 400 pem_file_name.pem

Gives the user read permission, and removes all other permissions.

These troubleshooting steps should help you resolve common SSH connectivity issues with AWS EC2 instances. Make sure to follow security best practices when managing your private key files and ensure that your security group configurations allow the necessary traffic for SSH access.

Happy troubleshooting AWS EC2 in Ruby on Rails! 🚀 Please follow for more updates BharteeTechRoR

--

--