Introduction

SSH connection fails when sshd not running or port blocked. This guide provides step-by-step diagnosis and resolution.

Symptoms

Typical error output:

bash
ssh: connect to host server.example.com port 22: Connection refused
lost connection
sshd may not be running or firewall blocking port 22

Common Causes

  1. 1.Configuration file syntax or path incorrect
  2. 2.Service not running or port blocked
  3. 3.Authentication or permission denied
  4. 4.Resource not found or already exists

Step-by-Step Fix

Step 1: Check Current State

bash
# Check service or tool status
which git ssh terraform
# View logs or status
systemctl status sshd
# Verify configuration
cat ~/.ssh/config

Step 2: Identify Root Cause

bash
# Check configuration
cat ~/.gitconfig
# View logs
journalctl -xe
# Test connectivity
ssh -v user@host

Step 3: Apply Primary Fix

```bash # Primary fix: Check and reconfigure # Verify installation which git

# Check configuration git config --list

# Reconfigure if needed git config --global user.name "User" ```

Step 4: Apply Alternative Fix

```bash # Alternative: Reset and retry # View logs journalctl -xe

# Check permissions ls -la ~/.ssh/

# Test connectivity ssh -v user@host ```

Step 5: Verify the Fix

bash
# Test the operation
git status
# Or
ssh -T git@github.com
# Should complete without errors

Common Pitfalls

  • Not reading error messages carefully
  • Modifying configuration without backup
  • Using incorrect file paths or syntax
  • Forgetting to restart services after config changes

Best Practices

  • Keep configuration files in version control
  • Test changes in isolation before applying
  • Read official documentation for syntax
  • Use verbose mode for troubleshooting
  • Configuration Error
  • Authentication Failed
  • Connection Refused
  • Resource Not Found