Port 22: Connection Refused is a common SSH error that generally happens because users try to connect to the wrong port.

The default SSH port is 22, but it’s generally changed to something else due to security concerns. As such, when users try to connect to port 22 (by default), they inevitably encounter this error.

systemd-resolved-conf-dns-fallbackdns

In addition to this, reasons like the firewall blocking the SSH port or the SSH service being down also usually lead to this error. We’ve detailed more reasons, as well as steps to deal with each one in the sections below.

How to Fix Port 22: Connection Refused Error

Before you start troubleshooting, we recommend rebooting your router, as that could be all that’s needed to resolve the issue. Additionally, you should perform a ping test to confirm that this is actually an SSH issue rather than ageneral internet issue. If there’s packet loss, you’d better off fixing that first.

Also, this error is, for the most part, a server-end issue. As such, the first two fixes are applicable on the client end, but there’s not much you’re able to do from the client system. Your best bet will be to try out the server-end fixes.

sudo-apt-install-openssh-server

Verify Login Credentials

It’s easy to miss, but users sometimes enter incorrect credentials on the client end when attempting to connect to the SSH server. Try reconnecting with the correct credentials if this is the case before moving on to the other fixes.

Resolve DNS Issues

The second possible issue on the client end is that the system isn’t resolving the hostname, leading to this error. you’re able to use the Public IP of the SSH server instead of the domain name to confirm this.

If there does indeed seem to bea DNS issue, you couldtry flushing the DNS cachewith the following commands:sudo systemd-resolve –flush-cachessudo resolvectl flush-caches

sudo-service-ssh-status

If that doesn’t help, you shouldchange your DNS servers.Here are the steps to do so:

If on a private network, you could also contact your system admin to verify that the SSH server’s DNS entry is correct.

grep-port-etc-ssh-sshd-config

Ensure SSH is Installed

This might seem like a no-brainer, but users often encounter SSH issues on fresh installations as they forget that SSH doesn’t come preinstalled.

Toinstall the OpenSSH server application and related fileson Debian-based distros, use the following command:sudo apt install openssh-server

ssh-specify-port

It’s also worth mentioning that some users were able to resolve the issue by purging the currently installed package withsudo apt purge openssh-serverand reinstalling it.

Check SSH Daemon Status

The SSH Daemon must be running for your connection to be successful. If you’ve made any changes to the SSH configurations, such as Port, for instance, you’ll also need to restart the service to apply the changes. Here’s how you can do so on Debian-based distros:

Check SSH Listening Port

The default SSH port is 22, but for security reasons, it’s generally changed to something else. Use thegrep Port /etc/ssh/sshd_configcommand to find out which port the server is listening to.

Since you’re encountering this error, it’s likely not port 22. In such cases, you canconnect to that specific port with the following command:ssh -p <port_no> @

add-rule-reload-firewall-ubuntu

Allow SSH in Firewall

Another common reason for this error is that thefirewall is blocking the SSH port. In such cases, you’re able to allow the SSH port with the following steps:

Ensure IP Is Whitelisted

There have also been cases where the connection failed because SSH was only configured to listen to certain addresses. In such cases, you can ensure the IP address you’re trying to access is whitelisted with the following steps:

sshd-config-listen-address