fbpx

Archive

How to resolve “localhost connection refused”?

When on windows you usually use putty to connect to remote SSH servers or terminal when on Linux. While connecting sometimes you can get error such as localhost connection refused on putty or your Linux terminal. Now, this can be very annoying as you can’t get to SSH of your remote server and perform your tasks.

 

This problem can be from the server or client-side. In this tutorial, we will see probable causes of this error and how to fix them. If you are using our managed vps, then you just have to contact us via ticket or live chat and we should fix this for you.

 

Let’s continue and see reasons for this error and how to resolve localhost connection refused.


Possible error messages on Putty or Linux Terminal

 

We will first see possible error messages that you will see.

 

In putty

 

Putty Fatal Error
Network Error: Connection Refused

On Linux Terminal

 

ssh: connect to host 51.xx.xx.xx port 22: Connection timed out

 

Now the connection to the server is properly routed, but the server is not accepting your connection for many reasons that we will discuss below.


Reasons and Fixes for localhost connection refused

 

We will now discuss reasons and fixes for this error one by one.

 

1. SSHD Daemon not running

 

It is possible that SSHD daemon on your remote server is not running. For SSH to work there is a daemon that works behind the scene and it is called SSHD. If this daemon is not running your server will not be able to accept SSH incoming connections. Check if this service is running or not:

systemctl status sshd

If it is not running you can turn it on:

systemctl start sshd

 

SSHD does not start?

Now for some reasons if your SSHD daemon still won’t start make sure you have enough disk space using:

 

df -h

 

If you have enough space you can see if your server is receiving heavy traffic or if the server is under heavy load. You can hire our managed vps service as well we offer 3 days free trial (no credit card required)

 

2. Firewall blocking SSH Port

 

Most of the time there is some firewall blocking the SSH port. First, see if you can telnet to SSH port 22

 

telnet 192.xx.xx.xx 22

 

Then check if you or user IP is blocked on IP Tables firewall

 

iptables -nL

 

If IP is listed, you need to unblock this IP to restore connectivity to SSH port 22. There is also a possibility that firewall has dropped or reject rule for SSH port 22, you need to remove that rule and add run following command:

 

iptables -A input -p tcp –dport 22 -j ACCEPT

 

If using CyberPanel

 

If you are using CyberPanel it comes with Firewalld by default unless you have installed CSF. You can go to :8090/firewall/ by default port 22 is opened, if not you can add port 22 there.

 

If you have installed CSF with CyberPanel, you can use csf.allow file to allow this IP. The file is located at

 

nano /etc/csf/csf.allow

 

Then turn off and turn on CSF again from CyberPanel interface.

 

3. Custom SSH Port

 

If you have defined a custom port for SSH (for security reasons, because standard SSH ports are prone to brute force attacks). Then you need to open that port on the firewall as well. Because firewall doesn’t know you choose a custom port for SSH.

 

First, confirm if you are using custom port using

 

cat /etc/sshd_sshd_config | grep Port

 

If you see something other then port 22, you need to fix it.

 

iptables -A input -p tcp –dport custom_port_here -j ACCEPT

 

Replace with custom port here.

 

If using CyberPanel

 

Go here :8090/firewall/secureSSH and see if you have set custom ssh port. Normally if you set custom SSH port, CyberPanel will open this port for you on the firewall. But if it still does not work, you can open this port manually by going to :8090/firewall/ or here :8090/firewall/csf if you are using CSF.

 

4. Incorrect IP, SSH Port or Key File

 

It is also possible that you did a human error and used incorrect IP or wrong SSH port. Make sure you are using right IP on putty or Linux terminal along with correct SSH port.

 

Sometimes people also use key-based authentication, and they get issues while connecting to SSH if the wrong private key is used or there are wrong permissions on your SSH private key. On your local server make sure that your private key has the permission of 600.


Why use CyberPanel?

 

We’ve discussed many reasons and fixes for localhost connection refused error. But we recommend that you install CyberPanel on your VPS, it will help you solve many issues and if you are hosting sites then you will get super speed as well because CyberPanel is based on OpenLiteSpeed and LiteSpeed Enterprise.

 

You will get easy access to Firewalld (installed by default) where you can easily open/close any ports at your wish. Or you can also install CSF too.

 

If you are very novice user you can also hire our managed vps service, we are super affordable and provide top-notch support to all our customers. You can start with 3 days trial (no credit card required at all).