Note: The commands discussed here have been tested on Ubuntu 20.04 LTS. The same commands are also valid for the Debian system.
Fix Connection refused error
This is the “Connection refused” error you might encounter while connecting to a remote system over SSH.
Follow the below solutions step by step in order to solve the “Connection refused” error.
Make sure OpenSSH is installed
One of the reasons you may receive a “Connection refused” error is because the OpenSSH server is not installed on the target server.
First, you will need to make sure that the OpenSSH server is installed on the system which you are trying to access via SSH. In order to check whether OpenSSH is installed or not, issue the following command in the Target server’s Terminal:
This command basically filters the term “openssh-server” from the list of installed packages. If you receive the following similar output, it indicates the OpenSSH server is installed. On the other hand, if you receive no output, it means OpenSSH is missing on the target server.
In case it is not installed on the target server, you can install it using the following command as sudo:
Then type sudo password, and when asked for confirmation, press ‘y’. Once installed, confirm it using the same command
Check SSH service
OpenSSH service runs in the background and listens to incoming connections. The stopped OpenSSH service can be one of the reasons you are receiving a “Connection refused” error.
Therefore, it is necessary to check whether the OpenSSH service is running or not using the following command in Terminal:
If you see the following output, it means the service is active and running in the background.
On the other hand, if you receive inactive (dead), that means the service is not running. You can run the OpenSSH service using the following command as sudo in Terminal:
In order to restart the service, use the following command:
Check SSH server listening port
Another reason for receiving a “Connection refused” error is because you are trying to connect to the wrong port. For instance, if the server is configured to listen on port 2244, and you are trying to connect to its default port 22, then, in this case, you will receive a “Connection refused” error.
Before trying to connect, you need to check the SSH server listening port. If it is the default port (22), then you can connect it using the following command:
If it is some port other than the default port, you will need to connect to the SSH server using this port:
In order to check which on which port the OpenSSH server is listening to; use the following command in Terminal:
You will receive the output similar to the following:
In the third column, you can see the server listening port is 2244. If this is the case, you will need to connect to the SSH server using this port.
Allow SSH in firewall
Firewall blocking the SSH port can be another major reason for the “Connection refused” error. If a firewall is running on the SSH server, you will need to allow the SSH port in it using the following command. Replace the port by the port number the SSH server is listening to:
For instance, if the SSH server is listening to port 2244, then you can allow it in the firewall as:
Reload the firewall using the following command:
In order to confirm if the rules have been added, check the status of the firewall using the following command in Terminal:
The following output shows that the port 2244 is allowed in the firewall.
Resolve Duplicate IP address conflict
The “Connection refused” error can also occur because of duplicate IP address conflict. So, make sure the system does not have a duplicate IP address.
Install the arping utility on your system using the following command:
Then ping the SSH server’s IP address.
In the output, if you see the reply from more than one MAC address, then it shows that there is a duplicate IP running on the system. If this is the case, change the IP address of the SSH server and try connecting again with the new IP address.
This is how to fix the “Connection refused” error by port 22 in Linux systems. In this article, we have described a few ways that will surely help you in solving the “Connection refused” error.