Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

ssh to a Windows machine says Connection refused on port 22

Errorssh: connect to host 192.168.1.20 port 22: Connection refused

Short answer

Connection refused means nothing answered on port 22. On Windows that is one of three things: the OpenSSH Server feature is not installed, the sshd service is stopped, or the OpenSSH-Server-In-TCP firewall rule is missing or off. Check them in that order from an elevated PowerShell.

  • Applies to Windows 10 1809 and later, Windows 11, Windows Server 2019 to 2025, PowerShell 5.1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    On the Windows machine, open PowerShell as administrator and see whether the server feature is installed and the service is running.

    Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
    Get-Service sshd
  2. 2Match what you see, then jump to its fix

    1. 1State : NotPresent for OpenSSH.Server, and Get-Service finds no sshd.OpenSSH Server is not installedFix
    2. 2Status Stopped in Get-Service sshd; it may have worked before a reboot.The sshd service is stoppedFix
    3. 3The service is Running, ssh from the same machine works, but other machines are refused or reset.The firewall rule for port 22 is missing or disabledFix

What does the error look like?

  • ssh: connect to host 192.168.1.20 port 22: Connection refused

Cause 1 of 3

OpenSSH Server is not installed

1Check

The client and the server are separate optional features, so having ssh.exe does not mean the server exists.

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'

2Fix

Install the server feature, then start the service as in the next cause.

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

3Verify

The state reads Installed and the service exists.

Get-Service sshd

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The sshd service is stopped

1Check

Read the service status and its start type.

Get-Service sshd | Select-Object Status, StartType

2Fix

Start the service and set it to start automatically.

Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'

3Verify

Status is Running; ssh from another machine now gets a prompt or a key question.

Get-Service sshd

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The firewall rule for port 22 is missing or disabled

1Check

Look for the rule that the server install creates, OpenSSH-Server-In-TCP.

Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP'

2Fix

If it is missing, create it for inbound TCP 22; if it exists but is disabled, enable it.

New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
# or, if the rule exists:
Enable-NetFirewallRule -Name 'OpenSSH-Server-In-TCP'

3Verify

From another computer, connect again.

ssh username@servername

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Set the sshd service to Automatic so a reboot does not leave it stopped.
  • Remember that sshd reads its config only at service start: restart the service after editing %programdata%\ssh\sshd_config.
  • Test from a second machine after each change, not only from the server itself.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.