Software⏱️ 2 min read📅 2026-06-03

How to Fix: connect to host localhost port 22: Connection refused

Connection refused error while connecting to localhost port 22.

Quick Answer: Check if SSH service is running on your local machine. You can do this by running the command 'systemctl status sshd' (for Linux) or 'net start ssh' (for Windows). If it's not running, start it and try again.

The error 'connect to host localhost port 22: Connection refused' occurs when the SSH server is not running or not listening on port 22. This can happen for several reasons, including:

💡 Why You Are Getting This Error

  • The SSH server is not running or has been stopped.
  • The SSH server is configured to listen on a different port, and the default port 22 is not being used.
  • There are firewall rules blocking the connection to port 22.

✅ Best Solutions to Fix It

Method 1: Restarting the SSH Server

  1. Step 1: Open a terminal and type 'sudo service ssh restart' or 'sudo systemctl restart sshd' to restart the SSH server.

Method 2: Changing the Port Number

  1. Step 1: Open the SSH server configuration file in a text editor using 'sudo nano /etc/ssh/sshd_config'.

Change the Port Number

Port 2222

Step 2:: Save and close the file, then restart the SSH server using 'sudo service ssh restart' or 'sudo systemctl restart sshd'.

🎯 Final Words

By following these steps, you should be able to resolve the 'connect to host localhost port 22: Connection refused' error and establish a successful SSH connection.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions