How to Fix SSH Connection Refused Error?
Are you facing an SSH connection refused error? It’s very unlucky to find many scenarios occurring when you try to connect to your server via SSH, maybe ending in an error reading Connection refused.
Reasons for SSH Connection Refused
Review below the most common effects that might be causing problems for you:
Your SSH Service Is Down
To get connected to your server utilizing SSH, it must run an SSH daemon – a program running the background to listen for and accept connections.
In-state this service is down, you won’t be able to successfully connect to your server and may receive a connection refused error.
There may be several causes for your server’s SSH daemon to be down. These include resource outages, unexpected traffic spikes, or even a Distributed Denial of Service (DDoS) attack.
In fact, you are in doubt that you’re SSH service might be down, you can run the below command to find out:
sudo service ssh status
If you get the status down in the command line, then you’ve likely found the reason behind your connectivity error.
You Have the Wrong Credentials
Though it may seem very simple, you may have entered the wrong credentials while trying to connect to your server. To run SSH, you require to have the below four pieces:
- Hostname:It is the server’s IP address you’re trying to connect to or your domain name.
- Username:It is your (S)FTP username.
- Password:It is your (S)FTP password.
- Port: By default, the port is 22. But some hosting providers vary their SSH port number for security reasons.
Therefore, you should be able to get it by logging in to your hosting account dashboard.
You can also hold for the port used for SSH by running the below command:
grep Port /etc/ssh/sshd_config
With the command line, you should know the correct port.
Assure that you enter the right credentials and considering the chance of types or entering the wrong port or IP address.
The Port You’re Trying to Use Is Closed
The endpoint to which you are directed while connecting to your server is called a port. Additionally, to guarantee that you have the correct one, you will need to check to see if the port you are trying to use is open.
It’s a security vulnerability if the port is open, as hackers can exploit it and gain server access. Therefore, unused ports are closed many times to avoid attacks.
If port 22 or the custom SSH port for your server is closed, you will see a connection refused error. To ensure if the port is listening, run the following command:
sudo netstat -plnt
The command line returns a list of ports and their individual states. If port 22’s state is LISTEN, then it is normally working. But if it’s not, it is asked to open the port again for connecting to your server.
SSH Isn’t Installed on Your Server
As mentioned first, SSH daemons are used by the servers to listen for and accept connections. So, if the server you are trying to connect to doesn’t have an SSH daemon, it won’t allow you to access it via SSH.
Usually, most of the hosting providers have SSH daemons installed on their servers by default. This specific issue is prevalent on localhost or dedicated servers.
Firewall Settings Are Preventing an SSH Connection
Because open ports are a sign of security hazard, firewalls installed to secure servers from hackers also block connections to them. It indicates that users who are harmless and trying to SSH into their servers also receive a connection refused error due to firewall settings.
In-state, your setup is in order, and you aren’t able to connect, check your firewall’s rules. Those can be seen in your command-line interface by the following commands:
sudo iptables-save # display IPv4 rules
sudo ip6tables-save # display IPv6 rules
The effects will vary, but check for these factors for finding if your firewall is blocking SSH connections:
dport 22: It shows the destination port, which for SSH is generally port 22.
REJECT: This shows the connections are being refused from the specified destination.
DROP: This shows the connections to the relevant port are being blocked.
In fact, you search the results of the commands above for dport 22, you should be able to find if your firewall is securing an SSH connection. If this is the status, you will need to change the rules to accept requests.
Reasons for PuTTY Saying Connection Refused
PuTTY is an SSH client, and if you know FTP very well, this platform is the FileZilla equivalent to SSH on Windows machines. PuTTY allows users to input their credentials and launch an SSH connection.
So, if you’re a PuTTY user and see the Connection refused error, the reason is likely one of those listed above.
This is an SSH connection error related to any other. The important tips below need to work whether you are using Terminal, PuTTY, or any additional program for connection to your via SSH.
Ways to Troubleshoot SSH Connectivity Errors
When there is an SSH connectivity mistake, you require to take a few rounds for troubleshooting it, based on the case.
Review the below tips for resolving the causes for a connection refused error seen above:
- If your SSH service is down: contact your hosting provider to obtain the reason for your SSH service not running. You can apply the command sudo service ssh restartfor localhost or dedicated servers to get it running again.
- You entered the wrong credentials mistakenly:After rechecking the SSH port using the grep Port /etc/ssh/sshd_config command twice, try to connect again with the correct details.
- If your SSH port is closed: This may happen as a side effect of one of the two reasons listed below. To fix this, install an SSH daemon on the server you want to connect to or modify your firewall rules to accept connections to your SSH port.
- If SSH isn’t installed on your server: Install OpenSSH or any additional SSH tool on the server you want to connect to applying the sudo apt install OpenSSH-server
- If your firewall is blocking your SSH connection: Disable the firewall controls that block your SSH connection by rotating the destination port settings to ACCEPT.
In fact, you are attempting to connect to your hosting provider’s server, contact support instead of trying to resolve the problem yourself. If you are a localhost or dedicated server user, you might find support on technical forums if none of the above tricks work.