SSH stands for Secure Shell, which creates a connection between the server & client. This connection can be used to communicate or transfer media/files through the server and client. In certain situations, users face the “Connection refused port 22” message during connecting servers. By default, the SSH service runs on port 22, which is a secure medium to transfer sensitive data. This article will explain all feasible solutions to tackle the “ssh connection refused port 22” error, along with reasons.
The content carried this post is as follows:
- How to Fix the “ssh connection refused port 22” Error?
- Reason 1: OpenSSH is not installed
- Solution: Install OpenSSH in System
- Reason 2: SSH Server Not Active
- Solution: Check the Active Status of SSH
- Reason 3: SSH Service is Running on a Different Port
- Solution: Check the SSH Port Number
- Reason 4: Firewall is Blocking Port 22
- Solution: Configure Firewall Through UFW Utility
How to Fix the “ssh connection refused port 22” Error?
The “ssh connection refused port 22” error arises during the establishing connection to a server. There are many other reasons, such as incorrect “username”, “IP address”, “Internet connection”, and many more. Some technical reasons with solutions are discussed as follows. Before digging into the details, let’s look at what the error looks like:
Reason 1: OpenSSH is not installed
In the Linux systems, the OpenSSH packages are not installed by default. The missing SSH (server, client) is one reason for this error.
Solution: Install OpenSSH in System
To resolve the issue, install the ssh services on the operating system. To do so, execute the below script for installing the “OpenSSH” packages:
$ sudo apt install openssh-server
After executing the command, users can connect to the system on port 22.
Install OpenSSH on Other Linux Distributions
For other Linux distributions, including RHEL/Centos/Fedora, OpenSSH can be installed by executing the below script:
$ sudo yum install openssh-server
Let’s carry on to another error.
Reason 2: SSH Server Not Active
Another reason may cause the “SSH” server not to respond actively. For this, the request will never reach the required destination for executing the communication process. It is due to the server not working.
Solution: Check the Active Status of SSH
A solution is provided to check the current status of SSH through the “systemctl” command. It is quite useful to display the complete information, including “Main PID”, “Tasks”, “Memory”, “CPU”, and “Port”.
$ sudo systemctl status ssh
The output shows that the current status is “active(running)”. Else, you can execute the below scripts to active it:
$ sudo systemctl start ssh #Start SSH Service
$ sudo systemctl enable ssh #Enable SSH Service
$ sudo systemctl restart ssh #Restart SSH Service
Reason 3: SSH Service is Running on a Different Port
It may be a reason that SSH services are running on one port and users requesting through a different port. Due to this problem, Port 22 will not respond to the particular request and display an error.
Solution: Check the SSH Port Number
A solution is provided here to find the SSH Port number through the “sshd_config” configuration file. For instance, the “Nano Editor” is utilized to open this configuration file by following the below script:
$ sudo nano /etc/ssh/sshd_config
After executing the script, it pops up the “sshd_config” configuration file on which you can verify the SSH Port number 22.
Once the port number is confirmed, verify that the SSH service is listening to the same port. To do so, use the below-stated command:
$ sudo lsof -i -P -n | gre ssh
Reason 4: Firewall is Blocking Port 22
If you cannot fix the problem through the above-stated solutions, then you should check the firewall and set its rule for port 22. So that it should allow all the connections over that port number
Solution: Configure Firewall Through UFW Utility
To add Port 22 over the firewall, which allows incoming SSH connection. To do so, execute the below script:
$ sudo ufw allow 22
In our case, the rules are already added.
After adding the rules, you need to reload the firewall as follows:
$ sudo ufw reload
To display the configured firewall after adding rules, run the command below:
$ sudo ufw show added
Now, you can verify that the rules have been added by configuring the firewall through the ufw utility.
Conclusion
The “ssh connection refused port 22” error can be resolved by installing “OpenSSH” packages or checking the active status of SSH services or port numbers. Additionally, incorrect “username” and “IP address” cause the same error of connection refusal. Users can also edit the port number after accessing the “sshd_config” configuration file to make a new connection. This post has listed down all the possible reasons, and their solutions to fix the error “ssh connection refused port 22”.