When ssh: connect to host ip_address port 22 Connection refused
Unable to access server???
Exactly when you see the error - “ssh: connect to host ip_address port 22: Connection refused” while connecting your AWS EC2 Instance. In order to find solution of the problem, you will go to AWS forum and other channels where you need to answers several questions first. But it's very difficult to find the actual problem.
In order to get clues what the problem is, we should provide as many details as possible about what we have tried and the results we are getting. Because there are hundreds of reason why a server or service might not be accessible, also connectivity is one of the toughest issue to diagnose, especially when you are hosting something critical on your box.
I've seen several topics on this problem, but none offers a solution to it. I was not aware for what should I look at first. So I walk through from the very basics and investigated the following thing
Use of verbose while ssh
$ ssh -vvv user@x.x.x.x
This didn’t help me as I haven't found any meaningful information except connection refused.
- After that I looked for my security groups, well they haven’t provide me any hint for further steps.
- Then I tried telnet at port 22 from my public and private network which was again a hard luck for me.
$ telnet X.X.X.X 22
- Tried creating AMI and building new instance of it.
- I've mounted the EBS of a broken instance on a running instance, look for the file configuration of my ssh.
$ cat /etc/ssh/sshd_config
and compare that with running instance.
- Also checked for the entries in /etc/fstab, but entries were all perfect as per knowledge.
- Tried starting the instance from the broken instance, but again the same error occured on the screen.
Coming to AWS UI console :-
- Further moved over the AWS UI, under Action I found option to put user data
So below entry were made
#cloud-config: snappy: ssh_enabled: True |
- I had gone through different option in UI , just went through the system logs
And found that the issue is with swap, which is showing error while mounting.
- So I stopped the broken instance and mount the broken ebs volume to the running one and commented the swap entry from /etc/fstab
- Finally I found that my instance is up and running, again I looked for the system logs in aws UI, where login was prompt was able to access my instance again.
Conclusion :-
If you come across any such error then follow the AWS console of the machine & look for the issue and get to the core of the problem.