Summary
This article discusses how to troubleshoot issues that affect OpenSSH commands that pass through Windows Firewall.
By default, OpenSSH uses TCP port 22. If this port is blocked or not listening, SSH commands fail. By default, OpenSSH listens on both IPv4 (0.0.0.0:22) and IPv6 ([::]:22). Windows Firewall settings, service status, and network permissions all play a crucial role in making sure that port 22 is both listening and accessible. Many factors can block OpenSSH communication, such as the following issues:
- The OpenSSH service isn't running or is misconfigured.
- Port 22 isn't listening because of service or firewall problems.
- Windows Firewall blocks inbound SSH traffic.
- Conflicting applications or policies use port 22.
- Network connectivity problems, NAT appliances, or other firewall appliances block access to port 22.
How to troubleshoot OpenSSH communication through Windows Firewall
Step 1: Verify the basic functionality
-
To verify that OpenSSH installed correctly, open a Windows PowerShell Command Prompt window, and then run the following command:
Get-Service -Name sshd[!IMPORTANT]
Make sure that you install the latest OpenSSH version, and keep it updated. -
To verify that OpenSSH is listening on port 22, run the following command at either a PowerShell command prompt or a Windows command prompt:
netstat -an | findstr :22The following example shows the response to this command when port 22 is listening for traffic:
TCP 0.0.0.0:22 0.0.0.0:0 LISTENING TCP [::]:22 [::]:0 LISTENING
Step 2: Make sure that Windows Firewall allows OpenSSH to use port 22
Important:
- If possible, limit access to trusted IP addresses.
- Avoid using port 22 for non-OpenSSH traffic.
- Audit your firewall rules regularly.
-
To check for existing firewall rules, go to your OpenSSH client computer, and run the following cmdlet at a PowerShell command prompt:
Get-NetFirewallRule -DisplayName "*SSH*" | Get-NetFirewallPortFilter | Where-Object {$_.LocalPort -eq 22} -
If you can't find an existing SSH rule, open an administrative PowerShell command prompt window. Then, run the following cmdlet:
New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (SSH)" -Enabled True -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow -
To verify that the rule is correctly configured, run
Get-NetFirewallRuleagain.
Step 3: Review the event logs for errors
In Event Viewer, review the entries in Application and Services Logs > OpenSSH.
Step 4: If the port isn't listening, configure it
In the previous steps, if netstat didn't report port 22 as "Listening," and the firewall rule is correctly configured, follow these steps:
-
To check the service status, run the following command at a PowerShell command prompt:
Get-Service -Name sshd -
To check the OpenSSH configuration, open the C:\ProgramData\ssh\ folder, and then use a text editor to open the sshd_config file.
-
Go to the
Portsection of the file. If the port information is commented out, uncomment it, and then make sure that the value is22.[!IMPORTANT]
If you use port forwarding, see the sshd_config manual page for configuration information. -
To test the settings, run the following cmdlet at a PowerShell command prompt:
sshd -t -
To restart the OpenSSH server service, run the following cmdlet at a PowerShell command prompt:
Restart-Service sshd
Step 5: Test for firewall issues
-
To create a temporary test rule, run the following cmdlet at a PowerShell command prompt:
New-NetFirewallRule -Name "SSH-Test" -DisplayName "SSH Test Rule" -Enabled True -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow -Profile Any[!NOTE]
This cmdlet creates a temporary rule that's named "SSH Test Rule" to open port 22. By using this approach, you avoid having to disable the entire firewall. -
Test some OpenSSH commands. If they work correctly, review and adjust your permanent firewall rules.
-
When you're finished, remove the test rule by running the following cmdlet at a PowerShell command prompt:
Remove-NetFirewallRule -Name "SSH-Test"
Step 6: Test for other network and network address translation (NAT) issues
-
Make sure that external firewalls, routers, or NAT devices forward port 22 to your Windows host.
-
Use the actual host name or IP address to test-run OpenSSH commands from another client computer. Run commands that resemble the following example:
ssh username@192.168.1.100 # or ssh username@servername.domain.com -
To further test port connectivity, run the following cmdlet at the PowerShell command prompt:
Test-NetConnection -ComputerName <hostname_or_IP> -Port 22
Logging and diagnostics
For information about verbose logging for OpenSSH, see How to enable OpenSSH verbose logging.
For information about connection attempts and errors, review the logs in %ProgramData%\ssh\logs.
Example scenarios
Scenario 1: SSH connection is refused
Cause: Firewall blocks port 22.
Solution: Turn on or create an inbound rule for TCP port 22.
Scenario 2: Port 22 isn't listening
Cause: OpenSSH service isn't running or is misconfigured.
Solution: Start the service, fix the configuration, and then run sshd -t to verify the result. If you changed the configuration, restart the service.
Scenario 3: Intermittent connectivity
Cause: Network device or NAT doesn't forward port 22.
Solution: Check the router or firewall settings, and then test by using Test-NetConnection. For configuration information, see sshd_config.
Related articles
- OpenSSH Server configuration for Windows
- How to enable OpenSSH verbose logging
- OpenSSH Manual Pages
- Windows Firewall with Advanced Security
- Troubleshooting OpenSSH in Windows
Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.
