Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Refused to my Running Amazon EC2 instance

Tags:

amazon-ec2

I created a micro ec2 instance. Installed all the necessary web software, mysql and git. Created an AMI out of that instance. Since that instance was using EBS as its root device, it took an EBS snapshot as well when I was creating my AMI.

I terminated that running instance. I then tried creating an instance out that amazon machine image (AMI), the new instance started along with a new EBS volume getting attached to the instance.

Now when I use my key pair to login to that instance via my ssh key to its public DNS address with a command as

ssh -i aws/mykey.pem ubuntu@thepublicdnsname 

it says

ssh: connect to host <thepublickdnsname> port 22: Connection refused 

Why is this happening. I was able to connect to my first instance with the same keys via ssh. Now the new instance is the exact copy and im not able to login to it. Any help on this ... ? Am I missing something?

I used the same key pairs to create the second new instance from the AMI.

like image 625
Anand Avatar asked Feb 27 '11 13:02

Anand


People also ask

Why does EC2 instance refuse to connect?

Error message: "ssh: connect to host ec2-X-X-X-X.compute-1.amazonaws.com port 22: Connection refused". This message comes from a host remotely. The following are common causes for this error: The host reached the instance but there was no service listening on the SSH port.

Can not connect to AWS EC2?

Verify that your instance is ready Check your instance to make sure it is running and has passed its status checks. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances, and then select your instance.

How do I enable Internet connection on AWS instance?

To create an internet gateway and attach it to your VPCOpen the Amazon VPC console at https://console.aws.amazon.com/vpc/ . In the navigation pane, choose Internet gateways, and then choose Create internet gateway. Optionally name your internet gateway.


2 Answers

I found that it takes variable amount of time for an EC2 instance to come up and get initialized. One is the time between calling ec2-run-instances till the instance state changes from "pending" to "running". After that there is additional time when ssh server becomes ready. That time can be a couple of minutes.

like image 92
OutputLogic Avatar answered Sep 23 '22 13:09

OutputLogic


I had the same issue: my problem was that I had a volume attached to my instance, then I detached the volume and deleted it. I followed aws docs to mount my instance and edited /etc/fstab. This was the problem: when the volume is detached and you try to reboot (or stop and start) the instance it goes to this file and tries to attach the nonexistent volume and ssh daemon is not started.

The solution is: I had to create another instance, detach the volume from problematic instance then edit the mounted_point/etc/fstab file to comment the line where its trying to mount the nonexistent instance, the reattach volume to problematic instance and then it worked everything fine.

like image 35
Jose Gabriel Giron Avatar answered Sep 22 '22 13:09

Jose Gabriel Giron