SSH Connection Refused: Troubleshooting Guide
Secure Shell (SSH) is a widely used protocol for secure remote administration of servers. However, sometimes you might encounter a "connection refused" error when trying to establish an SSH connection to a remote server, even though you can successfully ping the server and SSH into localhost.
Understanding the "Connection Refused" Error
The "connection refused" error typically occurs when the server is actively refusing the connection. This can be caused by several reasons, including:
- The SSH service is not running on the remote server
- A firewall is blocking the SSH connection
- The remote server is not configured to allow SSH connections from your IP address
Verifying the SSH Service
The first step in troubleshooting the "connection refused" error is to verify that the SSH service is running on the remote server. You can check the status of the SSH service using the following command:
sudo systemctl status ssh
If the SSH service is not running, start it using the following command:
sudo systemctl start ssh
To ensure that the SSH service starts automatically on boot, use the following command:
sudo systemctl enable ssh
Checking for Firewall Rules
If the SSH service is running, the next step is to check for firewall rules that might be blocking the SSH connection. The following command can be used to check for firewall rules:
sudo ufw status
If the output shows that the SSH connection is blocked, add a rule to allow SSH connections using the following command:
sudo ufw allow 22
Checking Remote Server Configuration
If the SSH service is running and there are no firewall rules blocking the connection, the next step is to check the remote server's configuration to ensure that it is configured to allow SSH connections from your IP address. The SSH daemon configuration file is typically located at /etc/ssh/sshd_config.
Check the following settings in the configuration file:
ListenAddress: Ensure that this is set to the IP address of the remote server or to0.0.0.0to listen on all available IP addresses.PermitRootLogin: Ensure that this is set toprohibit-passwordornoto prevent root login via password.PermitEmptyPasswords: Ensure that this is set tonoto prevent empty passwords.PubkeyAuthentication: Ensure that this is set toyesto enable public key authentication.AuthorizedKeysFile: Ensure that this is set to the correct location of the authorized keys file.PasswordAuthentication: Ensure that this is set toyesto enable password authentication.ChallengeResponseAuthentication: Ensure that this is set tonoto disable challenge-response authentication.UsePAM: Ensure that this is set toyesto enable Pluggable Authentication Modules (PAM).
After making any changes to the configuration file, restart the SSH service using the following command:
sudo systemctl restart ssh
Checking for Failed Login Attempts
If the SSH service is running, there are no firewall rules blocking the connection, and the remote server is configured to allow SSH connections from your IP address, the next step is to check for failed login attempts that might be causing the connection to be refused. You can check for failed login attempts using the following command:
sudo journalctl -u ssh | grep Failed
If there are multiple failed login attempts, the remote server might be temporarily blocking SSH connections from your IP address. In this case, wait for a few minutes and try connecting again.
In conclusion, the "connection refused" error when trying to establish an SSH connection to a remote server can be caused by several reasons, including the SSH service not running, firewall rules blocking the connection, or the remote server not being configured to allow SSH connections from your IP address.