ssh to a Windows machine says Connection refused on port 22
ssh: connect to host 192.168.1.20 port 22: Connection refusedShort 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.
Fix it fast
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 sshd2Match what you see, then jump to its fix
- 1
State : NotPresent for OpenSSH.Server, and Get-Service finds no sshd.OpenSSH Server is not installedFix - 2
Status Stopped in Get-Service sshd; it may have worked before a reboot.The sshd service is stoppedFix - 3
The 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
- 1
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.03Verify
The state reads Installed and the service exists.
Get-Service sshdConfirmed by learn.microsoft.com
Cause 2 of 3
The sshd service is stopped
1Check
Read the service status and its start type.
Get-Service sshd | Select-Object Status, StartType2Fix
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 sshdConfirmed by learn.microsoft.com
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@servernameConfirmed by learn.microsoft.com
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
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix