Manjaro Linux

How to fix SSH connection refused error in Manjaro Linux

SSH (an acronym of “Secure Shell” or “Secure Socket Shell”) enables the system administrators to establish a secure connection between client and host machines. SSH is used for making a secure connection, no matter if the network is secure or not. The connection using SSH is quite tricky therefore it requires intense attention. And if you have not covered all the aspects of making a connection, you may encounter “connection refused” error while connecting computers using SSH.

Graphical user interface Description automatically generated with low confidence

In this descriptive post, we have listed the causes of “connection refused” error in SSH and their possible fixes.

Possible causes and their solutions for “connection refused” error in Manjaro

There are multiple possibilities that you are getting the connection refused error in Manjaro Linux. In this section we are listing those errors and the relevant solutions as well.

1 – SSH service inactive

There is a possibility that your SSH service is not working properly, and it is causing an interruption in connection. Firstly, look for the status of the SSH service with the help of the command written below.

$ sudo systemctl status sshd.service

Graphical user interface, text, website Description automatically generated

If the service is down, then it is recommended to restart and enable the SSH service. The commands provided below will assist you to restart, enable and check the status of SSH service:

$ sudo systemctl restart sshd.service

$ sudo systemctl enable sshd.service

$ sudo systemctl status sshd.service

Text Description automatically generated

If the problem still occurs, you may contact your hosting provider to fix the issue.

2 – SSH is not installed

There is a possibility that you do not have the OpenSSH (tool for SSH) installed on your system. To ensure the installation, check the version of OpenSSH with the help of the command written below.

$ ssh -V

If the above command returns an error (as in our case it returned connection refused), then it means the OpenSSH daemon is missing on your server. To get it, execute the command provided below:

$ sudo pacman -S openssh

3 – Firewall is blocking the SSH connection

If the SSH service is running properly and still you are not able to connect to the SSH server then the firewall may be blocking your SSH connections. Although disabling the firewall rules may put your system under a security risk, you have to allow SSH through the firewall. You can allow SSH over your firewall with the help of command provided below.

Note : The output below shows that the rules are already added for SSH service.

$ sudo ufw allow ssh

Text, application Description automatically generated

After doing so, it is recommended to reload the firewall with the help of the command written below.

$ sudo ufw reload

Graphical user interface, text, application Description automatically generated

You can check the status of the firewall and the following command shows the firewall is allowing connections at port 22.

$ sudo ufw status

Text Description automatically generated with low confidence

If the SSH server is listening to a port other than 22 then you have to mention that port number. For instance, the command written below will allow SSH server at your selected port number.

$ sudo ufw allow <port-number>/tcp

4 – SSH is listening to a wrong port

Whenever a connection request is initiated, the default port of SSH(22) is used. One of the reasons for connection refused error is that you may have changed the port but you are trying to connect to the default port. You can check the SSH listening port by accessing the /etc/ssh/sshd_config file. For instance the command provided below will filter(with respect to port) the content of /etc/ssh/sshd_config.

$ grep -i port /etc/ssh/sshd_config

Text Description automatically generated

Note : In our case the output shows that the port is 22.

Moreover, if the SSH port is changed from 22 then the connection syntax would be like shown below.

$ ssh -p <port-number> <user-name>@<IP or hostname of server>

Note : The -p flag is used to make a connection at the desired port. It is observed that if the default port of SSH is changed to another port then you have to specify that port number.

5 – SSH port is closed

Whenever an attempt for a connection is made, SSH sends a request to the specific port. So, to make it successful, the SSH port needs to be opened. As SSH uses port 22, it is mandatory that SSH must allow all incoming connections at port 22. Firslty, check the port 22 is opened or not by using the command stated below.

$ sudo lsof -i:22

A screenshot of a computer Description automatically generated with medium confidence

If the status is listening(as shown in the output) then it’s alright, but if the port is closed then you can use the following ip tables command to open port 22.

$ sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

There may be a possibility that you are using the wrong credentials and it is suggested to look for the IP address of the server that you want to connect to.

After getting the things on board, you can make an ssh connection as we have made using the command written below.

$ ssh adnan@localhost

Text Description automatically generated

Note : The username in the above command is “adnan” whereas localhost acts as a hostname.

Conclusion

To make a secure connection using SSH, it is recommended to keep in mind the hurdles in establishing the connection. These hurdles initiate the connection refused error in SSH. This article lists down the reasons for connection refused error and the possible solutions are also listed. Majorly, there are five reasons provided here that may be creating the “connection refused error“. We have provided the possible solutions to all the reasons that are generating the connection refused error.

About the author

Adnan Shabbir