Troubleshooting & Comparisons

SSH Connection Refused: Troubleshooting Common Errors

Fix SSH connection errors including "connection refused" with actionable troubleshooting steps for server and client setups.

4 min read

SSH Connection Refused: Troubleshooting Common Errors

Encountering the dreaded "SSH connection refused" error can be frustrating, especially when you're trying to access or manage a server. This common issue indicates a failure in setting up a secure connection. Here's a complete guide to diagnosing and fixing this error step-by-step.

Identifying the SSH "Connection Refused" Error

The error message usually reads:

Connection refused

This happens when your SSH client cannot establish a connection with the server. It's important to pinpoint the cause to resolve the issue effectively. Common scenarios include:

  • The SSH service failing to start or crashing unexpectedly.
  • Misconfigured credentials or firewall rules.
  • Ports required for SSH being closed.
  • The absence of the SSH daemon on the server.

Understanding these possibilities is the first step toward troubleshooting.

Potential Causes Behind "SSH Connection Refused"

Below are the most likely reasons for this error, grouped for clarity:

comparison

SSH Service Issues

  • SSH daemon is not running.
  • Required system resources are unavailable due to traffic spikes.

Credential Problems

  • Incorrect username, password, or port number.
  • Mismatched key authentication.

Networking and Firewall

  • Closed or blocked SSH port.
  • Firewall rules set to reject SSH connections.

Server Setup

  • SSH daemon is not installed.
  • Configurations missing or corrupted on the server.

Host Specific Reasons

  • Hosting providers may enforce specific SSH setup rules.

Fixing "SSH Connection Refused" Step-by-Step

Follow these steps to systematically identify and resolve the error:

steps

  1. Ensure the SSH service is running
    Check the SSH status on the server.

    bash
    sudo systemctl status ssh

    If it’s inactive, start it:

    bash
    sudo systemctl start ssh
  2. Verify credentials
    Double-check your username, password, and port number. If not default, confirm the server's correct SSH port.

  3. Check connectivity on the SSH port
    Use the nc command to verify if the port is accessible:

    bash
    nc -vz <server-ip> <port>
  4. Install or configure the SSH daemon
    If missing, install OpenSSH on the server:

    bash
    sudo apt install openssh-server
  5. Inspect firewall rules
    Review current firewall rules to ensure SSH connections are permitted.

    bash
    sudo iptables -L -n

    Allow access to port 22 or your custom SSH port:

    bash
    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  6. Restart services after changes
    Restart SSH:

    bash
    sudo systemctl restart ssh

    If you altered firewall settings, reload the rules:

    bash
    sudo iptables-save > /etc/iptables/rules.v4

Common Mistakes to Avoid

How to Verify the Fix Worked

steps

  1. Retry your SSH connection
    Attempt to connect from your client:

    bash
    ssh user@<server-ip>
  2. Confirm port status
    Run ss or netstat commands to ensure the port is in LISTEN state:

    bash
    sudo netstat -tuln | grep <port>
  3. Inspect response logs
    Verify that the connection attempt logs from /var/log/auth.log show successful communication.

Troubleshooting FAQs

FAQ

Why is SSH showing "Connection refused" even though the server is reachable?

This likely indicates that the SSH daemon is inactive, not installed, or the port is blocked by a firewall. Check the daemon's status and ensure proper firewall configuration.

How can I test firewall permissions for SSH?

Use the iptables -L -n command or an equivalent firewall-checking tool. Specifically, look for a rule allowing TCP traffic on port 22.

Can I use SSH on a non-default port without errors?

Yes, but you'll need to specify the port explicitly during login:

bash
ssh -p <custom-port> user@<server-ip>
What can I do if PuTTY shows "Connection refused"?

Verify the SSH service on the server, ensure the correct port number, and check firewall rules. PuTTY requires the same troubleshooting steps as other SSH clients.

Is systemd socket activation relevant for SSH troubleshooting?

Yes. You can utilize systemd socket activation to ensure SSH listens on its specified port. For example:

bash
sudo systemctl enable ssh.socket

Official reference: OpenSSH manual pages.