Corrective action: Connection from port 22 Debian / Ubuntu refused

SSH provides a secure way to access and manage Linux servers. Sometimes, when connecting to SSH servers, users frequently encounter the “Connection Denied” error on port 22. This can occur for a number of reasons, such as: B. for various reasons, e.g. It can also occur due to the IP conflict problem. In this article, we are going to discuss some of the solutions that you should try to fix the error.

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:

$ sudo matching list –Set up | grep opensh-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:

$ sudo suitable To install opensh-server

Then enter the sudo password and when prompted to confirm, press ‘y’. After the installation, confirm the installation with the same command

$ sudo matching list –Set up | grep opensh-server

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:

$ sudo service ssh status

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:

$ sudo service ssh Beginning

To restart the service use the following command:

$ sudo service ssh start anew

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:

$ ssh [username]@[remoteserver IP or hostname]

If it is a different port than the standard port, you must connect to the SSH server using this port:

$ ssh -P [port_number] [username]@[ip_address]

To check which port the OpenSSH server is listening on; Use the following command in the terminal:

$ sudo netstat -ltnp | grep sshd

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.

$ ssh -P [2244] [username]@[ip_address]

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:

$ sudo Allow ufw port /tcp

For example, if the SSH server listens on port 2244, you can allow this in the firewall as follows:

$ sudo ufw allow 2244/tcp

Reload the firewall with the following command:

$ sudo reload ufw

To confirm that the rules have been added, check the status of the firewall with the following command in the terminal:

$ sudo ufw status

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:

$ sudo suitable To install arping

Then ping the IP address of the SSH server.

$ Ring <IP address>

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.

Related Posts