How to fix “port 22: Connection refused” issue in AWS Linux.


As we all know SSH provides a secure channel to access the Linux instance from your local system. Sometimes we do come across the Connection refused error while connecting to our Linux instance through SSH.
anoop@Anoops-MacBook-Air AWS-Personal Account Details- iam.avs and vinayak % ssh -i testing-vivian-centos-Jan28.pem centos@13.233.116.169
ssh: connect to host 13.233.116.169 port 22: Connection refused
There could be several reasons to point out. Here I will be discussing on how to fix those issues in the AWS console.
Method 1:
By Detaching and attaching the EBS volume :
Launch a new EC2 instance in your VPC. Make sure you launch the new instance in the same Availability Zone as the impaired instance. The new instance becomes your rescue instance.
Detach the Amazon Elastic Block Store (Amazon EBS) root volume (/dev/xvda or /dev/sda1) from your impaired instance.
Attach the EBS volume as a secondary device (/dev/sdf) to the rescue instance.
Run the lsblk command to view devices:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 8G 0 disk
└─xvda1 202:1 0 8G 0 part /
xvdf 202:80 0 8G 0 disk
└─xvdf1 202:81 0 8G 0 part
Create a mount point
$ sudo mkdir /mnt/rescue
Attach the mount point with file system
$ sudo mount /dev/xvdf1 /mnt/rescue
Run the lsblk command again to verify the volume mounted in the directory:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 8G 0 disk
└─xvda1 202:1 0 8G 0 part /
xvdf 202:80 0 8G 0 disk
└─xvdf1 202:81 0 8G 0 part /mnt/rescue
Now copy the sshd_config file from rescue instance to attached EBS volume.
$sudo cp /etc/ssh/sshd_config /mnt/rescue/etc/ssh/sshd_config
Once done unmount the EBS volume and attach it back to the impaired instance
You should be able to log in to your instance now :)
Method 2:
By using EC2 serial console
Before using this approach, make sure to enable the EC2 serial console access and check for supported instance type.
Also make sure you have Password set for your root user and Password Authentication enabled for your Linux instance.
Once you are done with the above, just go and click on Connect and proceed to login.
vi /etc/ssh/sshd_config
Make sure to revert back the changes which you have recently made to sshd_config file. In my case, I had changed the port number to 2222 which I will be reverting back.
Once you have updated the sshd_config file, make sure to run the below command:
[root@ip-172-31-7-27 ~]# systemctl restart sshd
That should be it. You should able to login to your ec2 instance using the pem key.
Happy Learning...
Subscribe to my newsletter
Read articles from Anoop Suvarna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Anoop Suvarna
Anoop Suvarna
🌱 I’m currently learning everything 🤣 👯 I’m looking for a cloud entusiast to work together for Devops Freelance Jobs 🥅 2023 Goals: To get more certifications. ⚡ Fun fact: I love to draw and play Cricket 😻 Setting up a own Alograding platform under progress...