How to Fix the SSH Connection Refused Error?


SSH is a cryptographic network protocol that is widely used for secure data communication, remote shell services or command-line interface access to a computer. SSH replaced the older and less-secure telnet protocol as the go-to method for remote shell access.

SSH provides strong encryption and authentication features for data confidentiality, integrity, and authenticity during communication over unsecured networks like the Internet. SSH has become an essential tool for system administrators, developers, network engineers, and enthusiasts who need to securely manage or access computing resources remotely.

The Dreaded SSH Connection Refused Error

It's common to encounter errors while trying to establish an SSH connection with a remote machine. One of the most frustrating errors is "connection refused," which occurs when your client fails to connect with the server due to various reasons.

The error message implies that your client was able to communicate with the server but couldn't establish a connection on the specified port (usually 22). A connection refused error can indicate either a problem with your client software configuration or issues on the server-side such as firewall settings blocking incoming traffic or incorrect login credentials.

Common Causes of SSH Connection Refused Error

Firewall settings blocking the connection

One of the most common reasons for experiencing an "SSH connection refused" error is due to firewall settings. Firewalls are designed to block or allow incoming and outgoing network traffic based on specific rules. If the firewall is improperly configured, it can block incoming SSH connections, causing the "connection refused" error message.

To check whether the firewall is causing this error, you must ensure that it allows incoming connections over port 22 (the default port for SSH). Additionally, it may be necessary to add an exception in the firewall configuration settings to allow SSH connections from specific IP addresses.

Incorrect login credentials or permissions

Another common cause of this error message is incorrect login credentials or incorrect permissions on the server. The server may deny access if incorrect usernames or passwords are presented during authentication checks. Also, if user permissions are not correctly configured on your remote server account, then you may encounter issues while trying to connect via SSH.

SSH service not running on the server

You can check whether or not sshd is running by logging into the remote system and typing 'systemctl status sshd' command in terminal window. If status shows inactive (dead), then start process using 'systemctl start sshd' command otherwise some other issue needs troubleshooting further.

systemctl status sshd

Checking Firewall Settings

One of the most common causes of the SSH connection refused error is a firewall blocking the connection. To troubleshoot this issue, you should first check if any firewalls are enabled on both your local machine and the server you are trying to connect to.

If a firewall is enabled, it is likely that it is blocking incoming connections and causing the SSH connection refused error. If no firewall is present or it's disabled, you should verify if port 22 (default port for SSH) is open or not.

If it's closed, then this will prevent incoming connections from being established. You can use a port scanner tool such as nmap to check if port 22 is open or closed.

Verifying Login Credentials and Permissions

If there are no issues with your firewalls settings but you still cannot establish an SSH connection, it may be due to incorrect login credentials or permissions. Firstly, double-check that both your username and password details are correct by retyping them into both fields accurately.

It's also worth verifying that your account has permission to access ssh on that machine. You may also need administrative privileges on either end of the connection - contact whoever administers either side if unsure about whether permissions have been granted correctly.

Restarting the SSH Service

Another troubleshooting step is to restart your SSH service. This can help resolve any issues that could be preventing the service from running correctly.

The process for doing this may vary depending on your operating system but generally involves stopping and starting the service on the server. You can usually do this via command-line tools such as systemctl or service.

After restarting, check the service status and logs for any errors that may have been causing problems initially. These logs can often provide additional diagnostic information that can help pinpoint issues with your connection.

Advanced Troubleshooting Techniques

Using Alternative Ports for SSH Connections

While the SSH service commonly runs on port 22, it is possible to change the default port to a different number. This can be useful in cases where port 22 is blocked by a firewall or other network restrictions. However, changing the port requires updating the configuration files on both the server and client sides.

To change the default port, you must first access the server's configuration file located at `/etc/ssh/sshd_config`. Within this file, locate and update the `Port` line with your desired number.

Save and then restart the SSH service using `sudo systemctl restart sshd.service` for Linux-based systems or `sudo service sshd restart` for macOS systems. After these changes are made to the server-side configuration file, update your client-side configuration to reflect these changes.

sudo systemctl restart sshd.service
sudo service sshd restart

It is important to note that using non-standard ports may provide some level of security through obscurity, but it should never be relied upon as a sole method of security. Attackers can still scan for open ports and may eventually find your alternate SSH port if it is not properly secured in other ways.

Generating New Keys for Authentication Purposes

SSH keys are used for authentication purposes when connecting to an SSH server. These keys come in pairs: one private key stored locally on your computer and one public key stored on the remote server you wish to connect to.

If either key becomes compromised, it is important to generate new keys as soon as possible. The process of generating new keys involves creating a new key pair and updating both sides with their respective keys.

On Linux systems, this can be done through running `ssh-keygen` in your terminal. Follow prompts that ask you where you would like to store your new key pair and whether or not you want a passphrase associated with your private key.


Once your new key pair is generated, upload the new public key to your server's `authorized_keys` file located at `~/.ssh/authorized_keys`. Then, update your client-side configuration file to include the new private key.

This can be done by adding the following line: `IdentityFile /path/to/new/private/key`. By generating a new set of SSH keys and updating them on both sides of the connection, you ensure that no unauthorized person can gain access to your server through old keys.


In this guide, we have explored the SSH connection refused error, its common causes and how to troubleshoot it on your server. We have outlined the most common causes of the error, including firewall settings blocking the connection, incorrect login credentials or permissions, and SSH service not running on the server.

We have also provided detailed steps for troubleshooting each of these issues. To fix firewall issues, one can check for enabled firewalls, open port 22 (default port for SSH), and add an exception to allow incoming connections.

For verifying login credentials and permissions, users should verify their username and password are correct as well as checking user permissions on the server. Restarting the SSH service can help resolve SSH connection refused error.

Updated on: 05-Jun-2023


Kickstart Your Career

Get certified by completing the course

Get Started