Comprehensive Guide to Resolving SSH Connection Refused on localhost Port 22

Nov 14, 2025 · Programming · 12 views · 7.8

Keywords: SSH Connection | Port Configuration | Hadoop Installation

Abstract: This article provides an in-depth analysis of the 'Connection refused' error when connecting to localhost port 22 via SSH. Based on real Hadoop installation scenarios, it offers multiple solutions covering port configuration, SSH service status checking, and firewall settings to help readers completely resolve SSH connection issues.

Problem Background and Error Analysis

When installing distributed systems like Hadoop, SSH connectivity is a fundamental requirement. The command ssh localhost attempts to connect to port 22 by default. The error message ssh: connect to host localhost port 22: Connection refused indicates that no service is listening on the target port.

Core Solution: Port Specification Method

According to best practices, when SSH service runs on a non-standard port, the most direct solution is to specify the port number using the -p parameter. For example:

ssh localhost -p 8088

This method is simple and effective, establishing connections without modifying system configurations. During Hadoop installation, if SSH service is configured on port 8088, this command successfully establishes the connection.

Supplementary Solutions

SSH Service Status Check and Reinstallation

First verify if SSH client and server are installed:

which ssh
which sshd

If services are not properly installed, perform reinstallation:

sudo apt-get remove openssh-client openssh-server
sudo apt-get install openssh-client openssh-server

System-Specific Configuration

On Mac OSX systems, ensure remote login functionality is enabled:

Navigate to System PreferencesSharing, and check the Remote Login option. This setting allows SSH service to run on the default port 22.

In-Depth Technical Analysis

Port Mapping and Container Environments

In containerized environments like Docker, SSH port mapping configuration is crucial. The Gitea deployment case in the reference article shows that when SSH service is configured on port 222, correct port mapping is essential for connectivity:

ports:
  - "22:22"  # Standard port mapping
  # or
  - "222:22" # Custom port mapping

Network Connection Principles

localhost resolves to 127.0.0.1 (IPv4) or ::1 (IPv6). Connection refusal indicates:

Debugging and Verification Steps

Use verbose mode to diagnose connection issues:

ssh -vvv localhost

Output shows detailed connection attempt process, including:

Practical Application Scenarios

Hadoop Cluster Configuration

In Hadoop pseudo-distributed mode installation, NameNode and DataNode communicate via SSH. Proper SSH configuration ensures:

Version Compatibility Considerations

Different OpenSSH versions may have configuration differences. Ensure client and server version compatibility to avoid connection issues due to protocol mismatches.

Security Configuration Recommendations

When modifying SSH configurations, consider security best practices:

Conclusion

SSH connection refusal on localhost port 22 is a common configuration issue. Through multi-dimensional solutions including port specification, service status checking, and system configuration adjustments, problems can be quickly identified and resolved. In distributed system deployments, proper SSH configuration is fundamental to ensuring normal system operation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.