22

I've set up the EC2 instance couple days ago and even last night I was able to SSH to it with no problems. Today morning, I can't ssh to it. Port 22 is already open in the security group and I haven't changed anything since last night.

Error:

ssh: connect to host [ip address] port 22: Connection refused

I had similar issue recently and i couldn't figure out why it was happening, so I had to create a new instance, set it up again, and connect and configure all EBS storages to the new one. Took me couple hours... and now it's happening again. In the previous one, I've installed denyhost, which might have blocked me, but in the current one, there are only apache2, and mysql running.

The current instance has been up for 16 hours now, so I don't think it's because it didn't finish booting... Also, port 22 is open to all sources (0.0.0.0/0) and is using tcp protocol.

Any ideas?

Thanks.

5
  • Did you set the SSH security on the instance to allow all IPs or just yours? If just yours, did your IP change?
    – Kirk
    Commented Dec 24, 2012 at 22:32
  • @Kirk: source is 0.0.0.0/0 for all ports including 22. Protocol: tcp.
    – Sherzod
    Commented Dec 24, 2012 at 22:34
  • Have you created AMI from your Instance? If so, run new Instance from it. Commented Dec 27, 2012 at 3:13
  • Just in case you did something wrong in config files and now you are outside can't get in, the system log in EC2 won't see you anything. If it's just about to comment out something from config file (which it happened to me while ago and I was changing my ssh and couldn't get in anymore) you can simply connect via ftp (I have vsftpd and Adobe DW) with access to "/" and just go to /etc/ssh/ssh_config or other places you may want to comment out things you've done and reboot you system from EC2 dashboard. If it's about the config files, you may want to have ftp access.
    – Maziyar
    Commented Mar 22, 2013 at 9:39
  • 1
    @kirk Thank you so much! My IP had changed and I had set my security group to accept only from a single IP. I had lost SSH access to 4 boxes because of this! Fixed now :).
    – CodeManiak
    Commented Feb 11, 2015 at 6:58

11 Answers 11

30

With the help of @abhi.gupta200297, we were able to resolve it.

The issue was the error in /etc/fstab, and sshd was supposed to be started after fstab is successful. But it wasn't, hence, the sshd wouldn't start and that's why it was refusing the connection. Solution was to create a temporary instance, mount the root EBS from the original instance, and comment out stuff from the fstab and voila, it's letting me connect again. And for the future, I just stopped using fstab and created bunch of shell commands to mount the EBS volumes to directories and added them in /etc/init.d/ebs-init-mount file and then run update-rc.d ebs-init-mount defaults to initialize the file and I'm no longer having issues with locked ssh.

UPDATE 4/23/2015

Amazon team created a video tutorial of similar issue and show how to debug using this method: https://www.youtube.com/watch?v=_P29ZHu_feU

4
  • 1
    Could you make a blog post or comment here the shell commands / init script you used to replace fstab? I am experiencing this same problem.
    – S-K'
    Commented Apr 26, 2013 at 16:19
  • You sir shershams, are a lifesaver. This note should be included in the amazon docs.
    – s29
    Commented Apr 23, 2015 at 12:18
  • My problem specifically was that filesystem on ephemeral storage was wiped on machine turnoff and therefore fstab couldn't mount it after boot. The idea of your solution was an excellent fit for my problem too.
    – asaad
    Commented Nov 18, 2015 at 15:02
  • OMG!! Glad that I found this. Same this happened to me. Had to comment out, gain access and fsck the drive.
    – Sree
    Commented Mar 24, 2019 at 7:59
7

Looks like sshd might have stopped for some reason. Is the instance EBS backed? if thats the case, try shutting it down and starting it back up. That should solve the problem.

Also, are you able to ssh from AWS web console? They have a java plugin there to ssh into the instance.

6
  • aws web console also says connection refused. I will try to reboot right now. But is there any other way other than rebooting it? It makes services and websites running there unavailable for users...
    – Sherzod
    Commented Dec 24, 2012 at 22:59
  • Try doing a telnet to the instance on port 22. telnet hostname 22. If it connects, that will at least tell us that sshd is running, but we are getting blocked for some reason and we can troubleshoot from there. Commented Dec 24, 2012 at 23:00
  • connection refused... I've rebooted the instance and still cannot access it. Also, now apache and mysql are not running as well. Help?
    – Sherzod
    Commented Dec 24, 2012 at 23:03
  • Thats very weird. So telnet also cannot connect to port 22? Can you also try ssh'ing with the -v switch? what does that output? Commented Dec 24, 2012 at 23:06
  • Seems like some weird issue. Without ssh, we really cannot do anything. I would try involving AWS support Commented Dec 24, 2012 at 23:14
6

For those of you who came across this post because you are unable to SSH into your EC2 instance after a reboot, this is cross-posted to a similar question at serverfault:

From the AWS Developer Forum post on this topic:

Try stopping the broken instance, detaching the EBS volume, and attaching it as a secondary volume to another instance. Once you've mounted the broken volume somewhere on the other instance, check the /etc/sshd_config file (near the bottom). I had a few RHEL instances where Yum scrogged the sshd_config inserting duplicate lines at the bottom that caused sshd to fail on startup because of syntax errors.

Once you've fixed it, just unmount the volume, detach, reattach to your other instance and fire it back up again.

Let's break this down, with links to the AWS documentation:

  1. Stop the broken instance and detach the EBS (root) volume by going into the EC2 Management Console, clicking on "Elastic Block Store" > "Volumes", the right-clicking on the volume associated with the instance you stopped.
  2. Start a new instance in the same region and of the same OS as the broken instance then attach the original EBS root volume as a secondary volume to your new instance. The commands in step 4 below assume you mount the volume to a folder called "data".
  3. Once you've mounted the broken volume somewhere on the other instance,
  4. check the "/etc/sshd_config" file for the duplicate entries by issuing these commands:
    • cd /etc/ssh
    • sudo nano sshd_config
    • ctrl-v a bunch of times to get to the bottom of the file
    • ctrl-k all the lines at the bottom mentioning "PermitRootLogin without-password" and "UseDNS no"
    • ctrl-x and Y to save and exit the edited file
  5. @Telegard points out (in his comment) that we've only fixed the symptom. We can fix the cause by commenting out the 3 related lines in the "/etc/rc.local" file. So:
    • cd /etc
    • sudo nano rc.local
    • look for the "PermitRootLogin..." lines and delete them
    • ctrl-x and Y to save and exit the edited file
  6. Once you've fixed it, just unmount the volume,
  7. detach by going into the EC2 Management Console, clicking on "Elastic Block Store" > "Volumes", the right-clicking on the volume associated with the instance you stopped,
  8. reattach to your other instance and
  9. fire it back up again.
3
  • This is the most useful post on this problem! Thanks so much. I'd add that to make the volume a root volume name it /dev/sda1 under Red HaT.
    – Sych
    Commented May 29, 2014 at 10:30
  • @Sych: happy to help. There is a section within the volume attachment documentation that gives guidance on root volume naming: docs.aws.amazon.com/AWSEC2/latest/UserGuide/… Commented May 29, 2014 at 13:11
  • In my case it was wrong ownership on /var/empty/sshd. It should have been root:root, but it was something else. No idea why it changed.
    – cucu8
    Commented Mar 12, 2018 at 14:35
4

This happened to me on a Red Hat EC2 instance because these two lines were being automatically appended the end of the /etc/ssh/sshd_config file every time I launched my instance:

PermitRootLogin without-password
UseDNS no

One of these append operations was done without a line break, so the tail of the sshd_config file looked like:

PermitRootLogin without-password
UseDNS noPermitRootLogin without-password
UseDNS no

That caused sshd to fail to start on the next launch. I think this was caused by the bug reported here: https://bugzilla.redhat.com/show_bug.cgi?id=956531 The solution was to remove all the duplicate entries at the bottom of the sshd_config file, and add extra line breaks at the end.

1
  • 5
    These lines get added every time the instance boots (or reboots) by the /etc/rc.local file. To prevent this from happening over and over, you also need to comment out the 3 related lines in the /etc/rc.local file. This will fix the problem for good.
    – Telegard
    Commented Feb 14, 2014 at 19:19
1

Go to your AWS management console > select instance > right click and select "Get System Logs" This will list what went wrong.

1
  • 3
    nothing useful there... last logs are talking about EBS volumes, which I was working with last night.
    – Sherzod
    Commented Dec 24, 2012 at 22:38
1

Had the same issue, but sys logs had this:

Starting sshd: /var/empty/sshd must be owned by root and not group or world-writable. [FAILED]

Used the same steps described above to detach volume and attach to connectable instance. Then used:

sudo chmod 755 /var/empty/sshd

sudo chown root:root /var/empty/sshd

(https://support.microsoft.com/en-us/help/4092816/ssh-fails-because-var-empty-sshd-is-not-owned-by-root-and-is-not-group)

Then detached and reattached to original EC2 Instance and could now access via ssh.

0

I got similar ssh locked out by detach an EBS but forgot to modify the /etc/fstab

0

If your ubuntu has systemd, you can edit /lib/systemd/system/local-fs.target and comment out the last two lines:

#OnFailure=emergency.target
#OnFailureJobMode=replace-irreversibly

I haven't tested this extensively and don't know if there are any risks or side effects involved, but so far it works like a charm. It mounts the root volume and all other volumes (except those that are misconfigured, obviously), then it continues the boot process until SSH is up, so you can connect to the instance and fix the incorrect fstab entries.

0

In my case, the volume was out of space and a service was failing to start. I used the AWS tutorial (from Sherzod's post) to mount it on a good EC2 instance and clean it up and remove the service from startup before remounting it and verifying that things worked.

0

For me it was that my IP had changed. Hope this helps someone. Navigate to the security groups and update your My IP in the inbound rules.

0

I had the same issue not able to connect to the aws instance with permission denied error.

I was able to connect with aws team on screen share call and they guided me to change the folder permission on the aws instance using the following user meta script.

steps :

  1. stop the instance
  2. Actions > Instance setting > Edit user meta

enter image description here

  1. Enter the below script and save

**Content-Type: multipart/mixed; boundary="//" MIME-Version: 1.0 --// Content-Type: text/cloud-config; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cloud-config.txt" #cloud-config cloud_final_modules:

  • [scripts-user, always] --// Content-Type: text/x-shellscript; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="userdata.txt" #!/bin/bash chown root:root /home chmod 755 /home chmod 700 /home/ubuntu chmod 700 /home/ubuntu/.ssh chmod 600 /home/ubuntu/.ssh/authorized_keys ls -ld /home /home/ubuntu /home/ubuntu/.ssh /home/ubuntu/.ssh/authorized_keys chown ubuntu:ubuntu /home/ubuntu -R --//**
  1. save and connect to the instance with correct pem key.

Resolved my problem *change ubuntu to your instance username

Not the answer you're looking for? Browse other questions tagged or ask your own question.