How to Fix Connection Refused by Port 22 on Ubuntu

How to Fix Connection Refused by Port 22 on Ubuntu

We’re here to talk about how to fix the connection refused issue by port 22 on Ubuntu. Which is among the common occurrences on the operating system.

This article discusses the solutions to resolve connection refused errors encountered when someone tries to connect to servers in Linux using a secure shell (ssh). This error “Connection refused” implies that the server doesn’t accept the port when someone from a remote computer attempts to open a connection on it. 

You may get an error like the following:

error

How to Fix Connection Refused by Port 22 on Ubuntu 

SSH on Ubuntu utilizes port 22 to share data with other computers on the network. The remote system can also be accessed using port 22. If you are using port 22 and getting the error “Connection rejected by port 22,” read this post. This post will teach you how to fix the “Connection rejected by port 22” Ubuntu problem.

Possible reasons for connection refused error

There can be multiple reasons for the connection refused error that one may encounter when connecting via ssh. Following are some of the possible reasons:

  • The ssh service may be inactive
  • The port may be blocked by ufw firewalls of Linux
  • The server may be using a different port other than the default port 22
  • There can be a possible internet protocol (IP) address conflict

Tip: Before attempting the steps below, ensure your internet connection is active by pinging the system.

Resolving the connection refused error

Now, we will discuss the possible steps to troubleshoot the connection refused error. 

Verify OpenSSH installation on your system

The first step is to verify OpenSSH software (i.e., if the software is installed on your system or not). It is possible that some of the newly installed systems don’t have ssh daemon installed. The following command can be used to verify the OpenSSH installation:

$sudo apt list --installed | grep openssh-server

In the above command, first, we obtained the list of the software installed on your system and then used the grep utility to extract the OpenSSH server entry from the list. If the OpenSSH server is installed on your system, you will see an entry as output.

Install OpenSSH server

You can skip this step if the OpenSSH server is installed on your system. Otherwise, install the OpenSSH server first, and update your system using this command:

$sudo apt-get update

Then, install the OpenSSH server using this:

$sudo apt install openssh-server

If the problem persists, you may remove the old OpenSSH server and do a fresh installation. OpenSSH server can be removed via the following command:

$sudo apt-get remove openssh-client openssh-server

Check the ssh service status

We will check the status of the ssh service installed on the system. It should be active or running. You may get the connection refused error because of the inactive state of this service. Run the following command to check the status of the ssh service:

$sudo service ssh status

You can also use the systemctl utility to check for the status of the service:

$sudo systemctl status ssh

Start the ssh service

The command’s output in the above step can be used to verify if the ssh service is active. If it is not active, you can start it by running the following command:

$sudo service ssh start

To enable the service on system start, you can run the following command:

$sudo service enable ssh

Check the listening port of the ssh server

It is likely that the server is listening on a different port than the default port 22. For instance, the server may be listening on port 1068, and you are trying to connect on port 22. You can use the following command syntax to connect to a remote server:

ssh [username]@[remoteserver IP or hostname]

Now use the netstat command to check which port the OpenSSH server is listening to:

$sudo netstat -ltnp | grep sshd

You will be able to identify if the port 22 or another port is being used to open the connection. If there is a different port, you can use the following syntax to connect:

ssh -p [Port][username]@[ip_address]

Add the rule in ufw firewall to allow ssh

You may receive the connection refused message if the firewall doesn’t allow the ssh connection. The firewall may be blocking this port. To allow opening a connection on port 22, you may use the following command:

$sudo ufw allow port /tcp

If there is some other port, you may use the following command:

$sudo ufw allow 2244/tcp

You will see a message that the rules have been successfully updated. Alternatively, you can use the following command:

$sudo ufw allow 22

Now, you can reload the firewall service via the following command:

$sudo ufw reload

You can check the status of the firewall the ssh service has been allowed or not with the help of the following command:

$sudo ufw status

Resolve Duplicate IP address conflict

There can be a duplicate IP address conflict if the system has a duplicate IP address. We will use the “arping” utility for this purpose. First, install the utility with the following command:

$sudo apt install arping

You can then ping the IP address of the server with the help of the following command:

$ping <ip-address>

The output of this command will tell if you are using a duplicate IP address. If you receive a response from more than one medium access control (MAC) address, it means there is a duplicate IP address conflict. This issue can be resolved by changing the IP address of the ssh server.

Other issues

There could be a host of another issue that may not be allowed to connect to the ssh server. For instance, your domain name service (DNS) may be causing this issue. You may connect with the public IP of the host instead of the hostname. 

Also, try to change your DNS server. Check your IP is whitelisted and verify your login credentials. Sometimes, you may provide invalid credentials to connect to the remote server. Then, try to resolve the issue by using the steps above. 

In this article, we have discussed several approaches to resolve the connection refused error on ssh port 22. There could be multiple reasons for this issue. We first listed down those issues and then discussed the resolution for them. These steps can be used to troubleshoot the connection error problem of ssh.

If this guide helped you, please share it.

Leave a Reply
Related Posts