Note: The commands discussed here were tested on Ubuntu 20.04 LTS. The same commands apply to the Debian system.
Fix connection refusal errors
This is the “Connection Refused” error that can occur when connecting to a remote system through SSH.
Please follow the step-by-step solutions below to resolve the connection refused error.
Make sure OpenSSH is installed
One of the reasons you might get a connection refused error is because the OpenSSH server is not installed on the target server.
First of all, you need to make sure that the OpenSSH server is installed on the system that you want to access via SSH. To check if OpenSSH is installed or not, enter the following command in the terminal of the target server:
This command basically filters the term “openssh-server” from the list of installed packages. If you get the following similar output, it indicates that the OpenSSH server is installed. On the other hand, if you don’t get any output, it means that OpenSSH is missing on the target server.
In case it is not installed on the target server, you can install it as sudo with the following command:
Then enter the sudo password and when prompted to confirm, press ‘y’. After the installation, confirm the installation with the same command
Check SSH service
The OpenSSH service runs in the background and listens to incoming connections. The stopped OpenSSH service could be one of the reasons why you are receiving the “Connection Refused” error message.
Therefore, the following command in the terminal must be used to check whether the OpenSSH service is running or not:
If you see the following output, it means the service is up and running in the background.
On the other hand when you receive inactive (dead), That means the service is not running. You can run the OpenSSH service as sudo in the terminal with the following command:
To restart the service use the following command:
Check SSH server listening port
Another reason for the “Connection Refused” error message is that you are trying to connect to the wrong port. For example, if the server is configured to listen on port 2244 and you try to connect to its default port 22, you will receive a “Connection Refused” error message.
Before trying to connect, you need to check the SSH server’s listening port. If it’s the default port (22) you can connect it with the following command:
If it is a different port than the standard port, you must connect to the SSH server using this port:
To check which port the OpenSSH server is listening on; Use the following command in the terminal:
You will get output similar to the following:
In the third column, you can see that the server listening port is 2244. If so, you need to connect to the SSH server using this port.
Allow SSH in the firewall
A firewall blocking the SSH port can be another important reason for the connection refused error. If the SSH server is running a firewall, you will need to allow the SSH port in it with the following command. Replace that Harbor based on the port number on which the SSH server listens:
For example, if the SSH server listens on port 2244, you can allow this in the firewall as follows:
Reload the firewall with the following command:
To confirm that the rules have been added, check the status of the firewall with the following command in the terminal:
The following output shows that port 2244 is allowed in the firewall.
Resolve conflict with duplicate IP address
The “Connection Denied” error can also occur due to a duplicate IP address conflict. So make sure that the system does not have a duplicate IP address.
Install the arping utility on your system with the following command:
Then ping the IP address of the SSH server.
If you see the response from more than one MAC address in the output, it indicates that the system is running a duplicate IP. In this case, change the IP address of the SSH server and try again to connect with the new IP address.
How to fix port 22 connection refused error on Linux systems. In this article, we’ve described a few things that are sure to help you troubleshoot the connection refused error.