How to Fix SSH Connection Refused by Port 22?
SSH (Secure Shell) is a popular protocol that allows you to securely connect and manage systems over the network. Just like any other program, you can face several errors when using SSH. However, the most common is the “Connection refused by port 22” error.
There can be various reasons for getting the connection refused by port 22 error. In this article, I will show you some of the most common reasons for this error along with their solutions.
Check Connectivity Between Client and Server
Connectivity issues are common when dealing with systems over a network. You can check if the client can reach the server using the ping command.
ping 10.0.2.15
(Replace “10.0.2.15” with the SSH server IP.)
If you get a reply, this means that the server is reachable through the client machine, else you need to troubleshoot the connectivity issue. If your client and server machine are VirtualBox VMs, then make sure you have created and configured Nat Networking properly.
Sometimes, the machine takes time to boot. So, wait for some time to let the machine start completely before trying to log in via SSH.
OpenSSH is not installed
Make sure the OpenSSH server is installed and running on the remote computer. This can also be a reason you’re seeing the connection refused error.
To check if OpenSSH-server is installed, use any of the below-mentioned commands according to your Linux distro.
Note: Run these commands on the server machine (the computer that you’re trying to connect to) and not the client machine.
On Debian-based distributions (Ubuntu, Linux Mint etc.):
sudo dpkg --list | grep ssh
For RedHat-based distributions (Fedora, Rocky Linux etc.):
sudo yum list installed | grep ssh
On Arch-based distributions (Manjaro):
sudo pacman -Q | grep ssh
Through the above commands, you’ll see if the OpenSSH-server package is installed on the server. If not installed, you can use the below-mentioned commands to install OpenSSH on your server.
On RHEL-based systems:
sudo dnf install openssh-server
For Ubuntu/Debian-based systems:
sudo apt install openssh-server
On Arch-based systems:
pacman -S openssh
Check the SSH Server Status
Another reason why you’re getting connection refused by port 22 is because the SSH server is disabled or not active on the server. You can check whether the SSH server is running or not using the using the “systemctl” command.
sudo systemctl status sshd
If you see the status as inactive (dead), then start the SSH server using the following command:
sudo systemctl start sshd
This will start the SSH server. If you check the status again, you’ll see “active (running)” in the command output.
You can also make the SSH service start automatically during boot by enabling it. Use the following command:
sudo systemctl enable sshd
SSH Service is Running on a Different Port
The default port of SSH is 22 however due to security reasons people often change the default port. You can check the SSH port using the following command:
sudo netstat -ntlp | grep sshd
Now you know on which port the SSH server is running. If the SSH server is using any other port than the default port 22, you can use the -p option to specify the port in your SSH command.
ssh -p 2222 [email protected]
(Replace “2222” with your SSH port number, “rahul” with the username you’re trying to connect to and “10.0.2.15” with the IP address of the SSH server.)
Firewall is Blocking Port 22
Another very common issue for connectivity-related issues is the firewall blocking some ports or services. If you followed through all the previously mentioned fixes and none of them works, then it’s time to check the firewall.
The best way to make sure the firewall is the problem of the connection refused error is by disabling the firewall temporarily.
Note: Don’t disable the firewall on mission-critical servers (corporate environment). Only use the below steps on the server in the home environment.
On Debian and Arch-based Linux distributions:
sudo ufw disable
On RedHat based distributions:
sudo systemctl disable firewalld
After disabling, try to connect to the server via SSH and see if you still get the connection refused error. If you don’t get the error anymore, then the firewall is the actual culprit.
Now, you must re-enable the firewall and add some rules to prevent it from blocking the SSH connection.
On Debian and Arch-based Linux distributions, you can use the UFW (Uncomplicated Firewall) to manage the firewall rules. Use the following command to allow SSH:
sudo ufw allow ssh
If SSH is running on any other port than the default port, then create a rule to add that port:
sudo ufw allow 2222
(Replace “2222” with the appropriate port number.)
Check the UFW status to verify if the rule has been added successfully:
sudo ufw status
On RHEL based systems, use the firewall-cmd to manage firewall rules. Use the following command to allow SSH:
sudo firewall-cmd --permanent --add-service=ssh
To allow SSH by a specific port number, use the following command:
sudo firewall-cmd --permanent --add-port=2222/tcp
(Replace “2222” with the appropriate port number.)
You can use the following command to verify the successful addition of the rule in the system’s firewall:
sudo firewall-cmd --list-all
Resolve Duplicate IP Address Conflict
Although not common, still there is a chance of a duplicate IP address on a network. This happens when two different systems claim the same IP address. Make sure the IP of the SSH server is not in conflict with another system.
You can do so by using a simple tool called “arp-scan”. Run the following command:
sudo arp-scan 10.0.2.0/24
(Replace “10.0.2.0/24” with your network)
If there is an IP address conflict in your network, you’ll see it in the command output.
To fix a duplicate IP address, make sure you’ve not assigned the same static IP address to multiple machines on the same network and that the static IP address you’ve assigned doesn’t overlap with the DHCP pool address.
After solving the IP conflict, try connecting to the server via SSH.
Wrapping up!
Connection refused by port 22 is a common error that can be usually solved using any of the above-mentioned fixes. However, there can also be more reasons for this error to occur.
If you have solved the error using any other means, then do share it in the comments. This will help someone in need.
If you like this post, then follow CenturyBuzz on Facebook and X (Twitter) for more reviews, tips and tutorials.