EC2 Instance Unreachable By SSH

by Mark Shead on June 27, 2013

If you are using a Redhat 6.4 AMI EC2 instance on Amazon Webservices and after a reboot you can no longer connect to SSH, this might help you. There appears to be a bug in /etc/rc.local that tries to add the same lines over and over again to /etc/ssh/sshd_config. Here is what the problem looks like in /etc/rc.local:

cat <<EOL >> /etc/ssh/sshd_config
UseDNS no
PermitRootLogin without-password

Everytime the machines startup it is going to append to the file and eventually SSH will stop coming up correctly.  You may want to delete those lines or at least modify them to do something that you want them to do.

Also before you reboot take a look at  /etc/ssh/sshd_config and make sure it looks right. You may need to delete some of the extra entries from the end. You can test it by doing a:

sudo service sshd restart

If the sshd_config is bad, this will fail to restart sshd, but it should still leave you connected so you can change the config and try restarting it again.

If your machine is running but you can’t connect to it, you can disconnect the the volume, mount it on another EC2 instance, change those files and then put it back on the original instance and start it back up.


{ 3 comments… read them below or add one }

Mike September 4, 2014 at 12:12 pm

Thanks Mark, this saved my bacon today!


Mark Shead September 4, 2014 at 12:27 pm

Glad it was useful.


Leigh November 27, 2016 at 3:00 pm

This issue was driving me nuts! So thank you very much for explaining the cause – and the fix.


Leave a Comment

Previous post:

Next post: