大网数据广告活动
Windows Server 管理

Troubleshoot OpenSSH Communication Through Windows Firewall

· 52CE 我爱测

Discusses how to troubleshoot issues that affect OpenSSH commands that pass through Windows Firewall.

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

  1. 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.

  2. 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 :22
    

    The 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.
  1. 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}
    
  2. 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
    
  3. To verify that the rule is correctly configured, run Get-NetFirewallRule again.

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:

  1. To check the service status, run the following command at a PowerShell command prompt:

    Get-Service -Name sshd
    
  2. To check the OpenSSH configuration, open the C:\ProgramData\ssh\ folder, and then use a text editor to open the sshd_config file.

  3. Go to the Port section of the file. If the port information is commented out, uncomment it, and then make sure that the value is 22.

    [!IMPORTANT]
    If you use port forwarding, see the sshd_config manual page for configuration information.

  4. To test the settings, run the following cmdlet at a PowerShell command prompt:

    sshd -t
    
  5. To restart the OpenSSH server service, run the following cmdlet at a PowerShell command prompt:

    Restart-Service sshd
    

Step 5: Test for firewall issues

  1. 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.

  2. Test some OpenSSH commands. If they work correctly, review and adjust your permanent firewall rules.

  3. 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

  1. Make sure that external firewalls, routers, or NAT devices forward port 22 to your Windows host.

  2. 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
    
  3. 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

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.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 02/12/2026
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。