How to Fix the SSH Connection Refused Error

Understanding SSH and Its Uses

What Is SSH?

Secure Shell (SSH) is a network protocol that provides a secure method to access and manage devices remotely over unsecured networks. It encrypts the session, ensuring that the data exchanged between the client and server remains confidential and protected from eavesdropping or man-in-the-middle attacks.

This article is part of Rackset’s Server guides — browse the rest of the category for more in-depth tutorials on this topic.

SSH is often used for secure logins, file transfers, and executing commands remotely. It is a preferred method for system administrators and developers because of its robust security features.

Common Uses of SSH

  • Remote Server Management: SSH is primarily used to remotely manage servers. For instance, when you purchase Linux VPS hosting, SSH or PuTTY is typically used to access and manage the server.
  • Secure File Transfers: Using tools like SCP (Secure Copy Protocol) or SFTP (SSH File Transfer Protocol), SSH facilitates secure file transfers between local and remote machines.
  • Tunneling: SSH can be used to tunnel other protocols, providing a secure path for otherwise insecure protocols.
  • Port Forwarding: SSH allows for port forwarding, which can redirect network traffic from one network node to another.
  • Secure Remote Command Execution: Developers and administrators can execute commands on remote systems securely using SSH, which is essential for automation and maintenance tasks.

Benefits of Using SSH

The primary advantage of SSH is its security. By encrypting the data transferred between the client and server, SSH protects sensitive information from being intercepted by unauthorized users. Additionally, SSH offers a variety of authentication methods, including password-based and key-based authentication, allowing users to choose the level of security they require.

SSH’s versatility and security make it an indispensable tool for network management and development tasks. Its ability to connect securely to remote systems makes programming, server management, and other activities simpler, quicker, and more efficient.

Conclusion

Understanding SSH and its uses is crucial for anyone involved in web hosting, server management, or development. Despite its complexity, SSH’s security benefits and wide range of applications make it a vital tool in the technology landscape. Whether you’re managing a Linux VPS or transferring files securely, SSH is an essential protocol for secure and efficient operations.

Common Causes of SSH Connection Refused Error

Encountering a “Connection refused” error when attempting to connect via SSH can be frustrating. However, understanding the common causes can help you troubleshoot and resolve the issue quickly. Below are some typical reasons why you might face an SSH connection refused error:

Incorrect SSH Configuration

The SSH server configuration file, usually located at /etc/ssh/sshd_config, might have incorrect settings. Common misconfigurations include:

  • SSH service not running on the default port (22).
  • IP address restrictions preventing access.
  • Disabled password or key-based authentication.

Firewall Blocking SSH Port

Firewalls are designed to protect your server but might inadvertently block SSH connections. Ensure that your firewall allows traffic through port 22 (or the port you have configured for SSH). You can check your firewall rules using:

sudo iptables -L

or for systems using ufw:

sudo ufw status

SSH Service Not Running

If the SSH service is not active on the server, you won’t be able to connect. To check and start the SSH service, use:


sudo systemctl status ssh
sudo systemctl start ssh

Network Connectivity Issues

Network problems can prevent SSH connections. Verify that your network is functioning correctly and that there are no issues with your internet connection or the server’s network configuration.

IP Address Changes

If the server’s IP address has changed and you are attempting to connect to an old IP, you will encounter a connection refused error. Ensure you are using the correct IP address.

Host Key Verification Failed

When the server’s host key changes, SSH will refuse the connection for security reasons. You may need to remove the old key from the ~/.ssh/known_hosts file:

ssh-keygen -R [hostname]

Server Overload

If the server is under heavy load, it might not be able to accept new connections. Check the server’s resource usage and address any performance issues.

By identifying and addressing these common causes, you can resolve SSH connection refused errors and ensure a stable and secure connection to your server.

If you need this service, Rackset’s team provides professional Linux VPS with full support so you can rely on the quality and get set up quickly.

Network Issues Affecting SSH Connections

Network issues are among the most common causes of SSH connection refused errors. Understanding and diagnosing these issues can help restore access to your server quickly. Below are some potential network-related problems and how to address them:

Firewall Restrictions

Firewalls are designed to protect your network by controlling incoming and outgoing traffic. However, they can sometimes block SSH connections if not configured correctly.

  • Ensure that your firewall allows traffic on port 22, which is the default port for SSH.
  • If you have changed the default SSH port, make sure the firewall rules are updated to allow traffic on the new port.
  • Use the following command to check firewall rules on a Linux server:
sudo iptables -L

To allow SSH traffic through the firewall, you can use:

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Network Configuration Issues

Incorrect network configurations can lead to SSH connection problems. Here are some common issues and how to resolve them:

  • IP Address Changes: If the server’s IP address has changed, update your SSH client to connect to the new IP.
  • DNS Resolution Issues: Ensure that domain names are correctly resolving to the appropriate IP addresses. Use nslookup or dig to verify DNS settings.

Network Latency and Congestion

High network latency or congestion can cause SSH connections to time out or be refused. Consider the following:

  • Check your network speed and latency using tools like ping or traceroute.
  • If latency is high, try connecting at a different time when the network is less congested.

Example Commands

Here are some commands to help diagnose network issues:


# Check connectivity to the server
ping yourserver.com

# Trace the route to the server
traceroute yourserver.com

# Test DNS resolution
nslookup yourserver.com

By systematically addressing these network issues, you can often resolve SSH connection refused errors and regain access to your server.

Checking SSH Configuration on Your Server

When you encounter a “Connection refused” error while attempting to access your server via SSH, one of the first steps is to verify the SSH configuration on your server. This involves ensuring that the SSH service is running correctly and that the configuration settings are properly defined.

Verify SSH Service Status

First, check if the SSH service is active and running on your server. You can do this by executing the following command:

sudo systemctl status ssh

If the service is running, you will see an output indicating that the SSH service is “active (running).” If it is not running, you can start it with:

sudo systemctl start ssh

Review SSH Configuration File

The SSH configuration file, typically located at /etc/ssh/sshd_config, contains crucial settings that determine how SSH operates. Open this file using a text editor like nano or vim:

sudo nano /etc/ssh/sshd_config

Look for the following key settings to ensure they are correctly configured:

  • Port: Confirm that the SSH service is listening on the correct port. The default port is 22. If you have changed this, ensure your client is attempting to connect to the same port.
  • PermitRootLogin: If root login is disabled, ensure you are attempting to connect with a valid non-root user.
  • PasswordAuthentication: If set to “no,” ensure you are using key-based authentication.

After making any changes, save the file and restart the SSH service to apply the changes:

sudo systemctl restart ssh

Check Firewall Settings

Firewalls can block SSH connections if not configured correctly. Check your firewall settings to ensure that the correct port (default 22) is open:

sudo ufw status

If the SSH port is not listed as “ALLOW,” enable it with:

sudo ufw allow 22

Replace “22” with your custom port if you have changed the default.

For projects that need guaranteed resources and full root control, a VPS is the right step up from shared hosting. With VPS hosting you get dedicated CPU/RAM, NVMe storage, a dedicated IP and high uptime, deployed in minutes.

Conclusion

By verifying the SSH service status, reviewing the SSH configuration file, and checking firewall settings, you can resolve many common causes of SSH “Connection refused” errors. Ensure all configurations align with your intended setup to maintain secure and reliable access to your server.

Firewall Settings and SSH Access

Firewall settings are a common cause of SSH connection refused errors. Firewalls are designed to protect your server from unauthorized access by blocking or allowing traffic based on predefined security rules. However, if not configured correctly, they can inadvertently block legitimate SSH traffic. Here’s how to ensure your firewall settings permit SSH access:

Check Firewall Rules

To troubleshoot SSH access issues, first ensure that your firewall rules allow traffic on the SSH port, which is typically port 22. You can check and modify these rules using firewall management tools like iptables, firewalld, or ufw, depending on your Linux distribution.

Using iptables


# List current iptables rules
sudo iptables -L -n

# Allow SSH traffic on port 22
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Save the changes
sudo iptables-save

Using firewalld


# Check the status of firewalld
sudo systemctl status firewalld

# Allow SSH service
sudo firewall-cmd --permanent --add-service=ssh

# Reload the firewall to apply changes
sudo firewall-cmd --reload

Using ufw


# Enable ufw if not already enabled
sudo ufw enable

# Allow SSH traffic
sudo ufw allow 22

# Verify the rule is added
sudo ufw status

Check Port Configuration

Ensure that SSH is configured to listen on the correct port. This is defined in the SSH configuration file, typically located at /etc/ssh/sshd_config. Open this file and look for the line that specifies the port:


# Open the SSH configuration file
sudo nano /etc/ssh/sshd_config

# Ensure the port is set to 22
Port 22

# Save and exit the editor

After making changes, restart the SSH service to apply them:


# Restart SSH service
sudo systemctl restart sshd

Conclusion

By ensuring your firewall rules and SSH port configurations are correctly set, you can resolve many SSH connection refused errors. Always double-check these settings when troubleshooting SSH access issues to maintain a secure and accessible server environment.

Verifying SSH Port and Service Status

When encountering a “Connection refused” error while attempting to connect via SSH, one of the initial steps is to verify the SSH port and service status. SSH typically uses port 22, but this can be changed for security reasons. Here’s how you can check and ensure that the SSH service is running correctly on your server.

Check the SSH Port

By default, SSH listens on port 22. However, if you’ve configured your server to use a different port, you’ll need to verify this in your SSH configuration file. Follow these steps:

  1. Access your server’s terminal directly or via a control panel console.
  2. Open the SSH configuration file using a text editor. The file is usually located at /etc/ssh/sshd_config:
sudo nano /etc/ssh/sshd_config
  • Look for the line that starts with Port. This line will indicate the port number SSH is configured to use. For example:
Port 22

If the port is different from 22, ensure you are using the correct port number when attempting to connect.

Check SSH Service Status

Ensuring that the SSH service is running is crucial. Use the following commands to check the status of the SSH service:

  1. Run the command to check the SSH service status:
sudo systemctl status ssh
  • If the service is running, you should see an output similar to:
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since...

If the service is not running, you can start it using:

sudo systemctl start ssh

And enable it to start on boot with:

sudo systemctl enable ssh

Firewall Settings

Ensure that your server’s firewall is not blocking the SSH port. You can check your firewall settings with:

sudo ufw status

If SSH is not listed, allow it by running:

sudo ufw allow 22/tcp

Replace 22 with the correct port number if you are using a non-default port.

For heavy workloads and maximum performance, a dedicated bare-metal server gives you an entire physical machine. Rackset’s dedicated server deliver full hardware, root access and configurable port speeds for the most demanding sites and apps.

By verifying the SSH port and ensuring the service is active, you can resolve many common connection issues. If problems persist, further investigation into network configurations or server logs may be necessary.

Troubleshooting SSH Key Authentication Problems

When using SSH, key-based authentication is a widely preferred method due to its enhanced security over password-based authentication. However, issues can arise that prevent successful key authentication. Below are steps to troubleshoot and resolve common SSH key authentication problems.

Verify SSH Key Pair

Ensure that your SSH key pair is correctly set up. The key pair consists of a private key, stored on your local machine, and a public key, stored on the remote server.

  • On your local machine, locate the private key, typically found in ~/.ssh/id_rsa or ~/.ssh/id_dsa.
  • Ensure the public key, typically named id_rsa.pub or id_dsa.pub, is present in the ~/.ssh/authorized_keys file on the remote server.

Check File Permissions

Incorrect file permissions can prevent SSH from reading your keys properly. Verify the permissions on both the client and server sides:


# On the client side
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

# On the server side
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Ensure Correct Ownership

Files and directories must be owned by the correct user. Check ownership using:


# On the server side
chown -R : ~/.ssh

Review SSH Configuration

Check the SSH configuration files for any misconfigurations:

  • On the client side, review ~/.ssh/config for any incorrect settings.
  • On the server side, inspect /etc/ssh/sshd_config to ensure PubkeyAuthentication is set to yes.

Enable SSH Debugging

For further insights, enable SSH debugging to gather detailed information about the authentication process:


ssh -vvv user@hostname

The -vvv flag increases verbosity, allowing you to see detailed output that can help pinpoint the issue.

Common Issues and Solutions

Issue Solution
Key not loaded into SSH agent Use ssh-add ~/.ssh/id_rsa to add the key to the agent.
Mismatch in key types Ensure both public and private keys are of the same type (e.g., RSA, DSA).

By following these steps, you can resolve most SSH key authentication problems, ensuring secure and reliable access to your Linux VPS hosting.

Testing SSH Connection from Another Device

If you encounter a “Connection refused” error when trying to connect to your server via SSH, testing the connection from another device can help determine if the issue is with your current device or network. Here’s a step-by-step guide to perform this test effectively:

Steps to Test SSH from Another Device

  1. Prepare Another Device: Ensure you have access to another computer or mobile device that can connect to the internet.
  2. Install SSH Client:
    • For Windows, you can use PuTTY.
    • For macOS and Linux, the terminal has a built-in SSH client.
    • For mobile devices, consider using apps like Termius for iOS or ConnectBot for Android.
  3. Connect to the Internet: Use a different network if possible. This helps determine if the issue is network-related.
  4. Attempt SSH Connection: Open the SSH client and attempt to connect to your server using the command:
ssh username@server_ip_address

Replace username with your actual username and server_ip_address with the IP address of your server.

Analyzing the Results

  • If the connection is successful, the issue likely lies with the original device or network configuration. Consider checking firewall settings or network restrictions on the original device.
  • If the connection is refused again, the problem might be with the server configuration or the SSH service on the server. Ensure the SSH service is running and correctly configured.

Common SSH Command Options

Option Description
-p Specify a custom port number if your server uses a non-standard SSH port.
-v Enable verbose mode to get more detailed output, which can help in troubleshooting.

By following these steps, you can effectively diagnose whether the SSH connection issue is specific to your device, network, or the server itself. This can significantly streamline the troubleshooting process and help you restore access to your server promptly.

Master SSH Access with Ease

Facing SSH connection issues can be frustrating, but with Rackset’s Linux VPS, you’ll have the reliability and control needed to troubleshoot any problem rapidly. Our robust infrastructure ensures your server is always accessible and secure, empowering you to focus on what matters most.
Unlock seamless connectivity with Linux VPS!

Linux VPS

Conclusion

Troubleshooting SSH connection refused errors can be a daunting task, but by systematically addressing common issues such as incorrect login credentials, disabled SSH services, or firewall restrictions, you can often resolve these errors efficiently. It’s crucial to ensure that both the client and server configurations are correctly set up and that network policies allow for SSH traffic. By following the steps outlined in this guide, you can minimize downtime and maintain secure, reliable access to your servers.

If you continue to experience issues despite following these troubleshooting steps, consider seeking further assistance from your hosting provider or consulting with a network security expert. Ensuring a stable SSH connection is vital for managing your server infrastructure effectively. For more information on server management and hosting solutions, explore our resources at Rackset to enhance your server’s performance and security.

Frequently Asked Questions

What does ‘SSH Connection Refused’ mean?

An ‘SSH Connection Refused’ error indicates that the SSH server is not accepting connections. This could be due to the SSH service not running, a firewall blocking the connection, or incorrect server settings.

How can I check if the SSH service is running on my server?

You can check if the SSH service is running by executing the command `sudo systemctl status ssh` on the server. If it is not active, you can start it with `sudo systemctl start ssh`.

What firewall settings might cause SSH connection issues?

A firewall could block SSH connections if port 22 is not open. Ensure that your firewall allows incoming connections on port 22 using commands like `sudo ufw allow 22` or by configuring your firewall settings appropriately.

How can I verify if the server’s SSH port has been changed?

Check the SSH configuration file, typically located at `/etc/ssh/sshd_config`, for the line that specifies the port (e.g., `Port 22`). If it has been changed, use the new port for your SSH connection.

What should I do if my SSH key is not working?

Ensure that your public key is correctly added to the server’s `~/.ssh/authorized_keys` file and that permissions on the `.ssh` directory and `authorized_keys` file are set correctly (e.g., `chmod 700 ~/.ssh` and `chmod 600 ~/.ssh/authorized_keys`).

Behzad Nategh
Behzad NateghFounder and CEO

Behzad Nategh is the Founder and CEO of Rackset, with 20+ years in web hosting, cloud infrastructure, and web design. He writes on hosting, domains, security, and growing an online business.

Leave a Reply

Your email address will not be published. Required fields are marked *

Count: 74 - Average: 4.7

Rating Submitted.