Contents

The Problem

On a quiet evening, I decided to connect to my remote server using SSH, but everything went awry due to the ISP.

Having said that, fellow Earthmate, are you experiencing problems establishing a connection via SSH? You see something like: "ssh: connect to host 'mygorgeoushost.com' port 22: Connection refused"? Keep reading, because my solution might work for your case.

The Solution

# First Attempt

IMPORTANT: Can you connect to your remote server using the browser? For example, in my Lightsail account, under the menu item 'Instances -> Connect,' there is a button:

That allows me to establish a connection with my server.

If not, go to '# Third Attempt'.

If so, please connect to your server and execute the following commands on the remote machine(My apologies to Windows users):

Check if openssh-server is installed: 

dpkg -L openssh-server | grep bin/

It should display the binary; if not, please install it with: sudo apt install -y openssh-server

Check the status of ssh with:

sudo service ssh status

If it is not currently running, you can start it with:

 sudo service ssh start

Verify whether port 22 is blocked by iptables

sudo iptables -L

You should observe something similar to this:

If it's not accepting, run this command:

 sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT

Finally, try to connect with SSH from the local machine (your PC):

ssh -vvv -i path/to/key.pem your_user@your_ip_address

# Second Attempt

If it didn't work, try changing the SSH port number from 22 to 2222 by editing the configuration file, on the remote server:

sudo vim /etc/ssh/sshd_config 

Now restart it and check if it is listening on port 2222:

sudo /etc/init.d/ssh restart
sudo netstat -tupan | grep sshd

Remember to add the new rule to your firewall settings. For example, in my Lightsail instance, I created a custom rule for port 2222:

On your local device run the command:

ssh -p 2222 -vvv -i path/to/key.pem your_user@your_ip_address

Now it should work. You welcome :)

Explanation: It's necessary to change the default SSH port number from 22 to 2222 because your Internet Service Provider (ISP) is blocking the port.

# Third Attempt

Change your internet connection until the normal command works on the local device, i.e:

ssh -vvv -i path/to/key.pem your_user@your_ip_address

Keep in mind that if you change two connection and the problem persists, it might be time to explore alternative solutions rather than retrying the same approach.

If you are hopeless, try running these commands on your LOCAL machine as my last remedy:
sudo apt-get purge openssh-server 
sudo apt-get install openssh-server