Repository navigation
fix(install,doctor): Windows HNS port-22 reservation (continuum's diagnosis — sshd bind EPERM) - #160
Conversation
…(continuum-b69f diagnosis) Bug found by continuum-b69f mid-Windows-bringup 2026-04-27: \`Start-Service sshd\` failed with "Cannot bind any address" / permission denied even with admin. Root cause: Windows HNS (Host Network Service — backs Hyper-V, WSL2, Docker Desktop) dynamically reserves port ranges at boot. The reservations rotate per-boot and are NOT visible in \`netsh int ipv4 show excludedportrange\` (which only shows static admin reservations). When port 22 randomly falls inside an HNS-held range, sshd's bind() returns EPERM at OS level, regardless of admin status. Sources: - https://keasigmadelta.com/blog/how-to-solve-cannot-bind-to-port-due-to-permission-denied-on-windows/ - https://github.com/docker/for-win/issues/3171 - https://gist.github.com/strayge/481a77d31a94e133a76662877b1a90ca ## Persistent fix (this PR) Two-step persistent workaround applied during admin-elevated sshd install. Both ops idempotent — re-run of install on a healthy box doesn't re-prompt or duplicate state. 1. \`reg add HKLM\\SYSTEM\\CurrentControlSet\\Services\\hns\\State /v EnableExcludedPortRange /d 0 /f\` Disables HNS auto-exclusion. Survives reboots. 2. \`netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1\` Explicitly reserves port 22 in the static excluded-port-range so HNS can't grab it on subsequent boots. Plus a New-NetFirewallRule for the OpenSSH-Server-In-TCP rule (the capability install usually creates it but it can be missing/disabled on some systems — idempotent check before creating). ## Files changed - \`install.ps1\` — \`Set-HnsPortFreedomFor22\` helper + wired into \`Install-OpenSSHServer\`. Native Windows installer path. - \`install.sh\` — Windows-bash branch's \`_ensure_sshd_running\` now emits a single elevated PowerShell payload that runs ALL the steps (capability install + HNS workaround + firewall rule + start + persist) so Joel/users click UAC ONCE for the whole sshd setup. - \`airc doctor\` — \`[MISSING] sshd\` Windows hint now includes the reg+netsh lines and explains why (HNS quirk). User can run all five commands as a contiguous block to remediate manually. ## Why this matters Pre-fix, even after the user ran the Add-WindowsCapability + Start- Service incantation from PR #156's hint or PR #157's auto-install, they could STILL hit the bind-EPERM if HNS happened to claim port 22 on their boot. Random failure, no diagnostic, looks like a permission bug. Continuum-b69f's diagnosis turns this from an unsolvable random into a one-time install action. ## Test posture (Mac regression) Mac side unchanged behavior; HNS branch only fires on MINGW/MSYS/CYGWIN. - part_persists: 8/8 - list: 4/4 - general_sidecar_default: 12/12 - platform_adapters: 11/11 ## Out of scope Cross-machine substrate end-to-end test once continuum's Windows host binds port 22 successfully. Parallel work; not blocked on this PR.
There was a problem hiding this comment.
Pull request overview
Adds a Windows-specific remediation for the intermittent “sshd can’t bind to port 22 (EPERM)” failure caused by HNS dynamically reserving port ranges, and surfaces the workaround in install flows and airc doctor.
Changes:
- Add idempotent HNS port-22 mitigation (registry + excluded port range) to the Windows OpenSSH Server install/start path.
- Consolidate Windows Git-Bash sshd setup into a single UAC-elevated PowerShell payload (capability install + HNS workaround + firewall + service start/persist).
- Expand
airc doctorWindows instructions to include the HNS workaround commands.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| install.sh | Windows Git Bash: single elevated PowerShell payload now includes HNS + firewall remediation alongside OpenSSH install/start. |
| install.ps1 | Adds Set-HnsPortFreedomFor22 and wires it into Install-OpenSSHServer, plus prints manual remediation steps. |
| airc | Updates Windows doctor output to include HNS workaround commands for missing sshd. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| local _elevated_payload=' | ||
| $ErrorActionPreference = "Stop"; | ||
| try { | ||
| $cap = Get-WindowsCapability -Online -Name "OpenSSH.Server*"; | ||
| if ($cap.State -ne "Installed") { Add-WindowsCapability -Online -Name $cap.Name | Out-Null } | ||
| $reg = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\hns\State" -Name "EnableExcludedPortRange" -ErrorAction SilentlyContinue).EnableExcludedPortRange; | ||
| if ($reg -ne 0) { reg add "HKLM\SYSTEM\CurrentControlSet\Services\hns\State" /v "EnableExcludedPortRange" /d 0 /f | Out-Null } | ||
| $excl = netsh int ipv4 show excludedportrange protocol=tcp | Out-String; | ||
| if ($excl -notmatch "(?m)^\s*22\s+22\b") { netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 | Out-Null } | ||
| if (-not (Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) { | ||
| New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null | ||
| } | ||
| Start-Service sshd; | ||
| Set-Service -Name sshd -StartupType Automatic; | ||
| Write-Host "airc: sshd ready (capability + HNS + firewall + service auto-start)"; | ||
| } catch { Write-Host "airc-elevated-error: $_" } |
There was a problem hiding this comment.
The elevated PowerShell payload is injected into -Command "...", but the payload itself contains many unescaped double quotes (e.g., "Stop", "OpenSSH.Server*", registry path strings). This will break the quoting of the inner -Command argument and can cause the elevated run to fail or run truncated/malformed commands. Consider passing the script via -EncodedCommand, writing it to a temp .ps1 and invoking -File, or rewriting the payload to avoid double quotes (use single-quoted PS strings) and/or escape any embedded quotes reliably.
| $reg = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\hns\State" -Name "EnableExcludedPortRange" -ErrorAction SilentlyContinue).EnableExcludedPortRange; | ||
| if ($reg -ne 0) { reg add "HKLM\SYSTEM\CurrentControlSet\Services\hns\State" /v "EnableExcludedPortRange" /d 0 /f | Out-Null } | ||
| $excl = netsh int ipv4 show excludedportrange protocol=tcp | Out-String; | ||
| if ($excl -notmatch "(?m)^\s*22\s+22\b") { netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 | Out-Null } | ||
| if (-not (Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) { | ||
| New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null | ||
| } | ||
| Start-Service sshd; | ||
| Set-Service -Name sshd -StartupType Automatic; | ||
| Write-Host "airc: sshd ready (capability + HNS + firewall + service auto-start)"; | ||
| } catch { Write-Host "airc-elevated-error: $_" } |
There was a problem hiding this comment.
In Windows PowerShell (5.1), failures from native executables like reg.exe/netsh.exe do not throw, so the current try/catch and $ErrorActionPreference = 'Stop' won’t catch or stop on those failures. Since the script discards output (Out-Null), this can silently fail to apply the HNS workaround. Check $LASTEXITCODE after each native command and throw/exit 1 on non-zero (or use PowerShell-native registry cmdlets instead of reg).
| $reg = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\hns\State" -Name "EnableExcludedPortRange" -ErrorAction SilentlyContinue).EnableExcludedPortRange; | |
| if ($reg -ne 0) { reg add "HKLM\SYSTEM\CurrentControlSet\Services\hns\State" /v "EnableExcludedPortRange" /d 0 /f | Out-Null } | |
| $excl = netsh int ipv4 show excludedportrange protocol=tcp | Out-String; | |
| if ($excl -notmatch "(?m)^\s*22\s+22\b") { netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 | Out-Null } | |
| if (-not (Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) { | |
| New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null | |
| } | |
| Start-Service sshd; | |
| Set-Service -Name sshd -StartupType Automatic; | |
| Write-Host "airc: sshd ready (capability + HNS + firewall + service auto-start)"; | |
| } catch { Write-Host "airc-elevated-error: $_" } | |
| $regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\hns\State"; | |
| $reg = (Get-ItemProperty -Path $regPath -Name "EnableExcludedPortRange" -ErrorAction SilentlyContinue).EnableExcludedPortRange; | |
| if ($reg -ne 0) { Set-ItemProperty -Path $regPath -Name "EnableExcludedPortRange" -Value 0 -Type DWord } | |
| $excl = netsh int ipv4 show excludedportrange protocol=tcp | Out-String; | |
| if ($LASTEXITCODE -ne 0) { throw "netsh show excludedportrange failed with exit code $LASTEXITCODE" } | |
| if ($excl -notmatch "(?m)^\s*22\s+22\b") { | |
| netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 | Out-Null | |
| if ($LASTEXITCODE -ne 0) { throw "netsh add excludedportrange failed with exit code $LASTEXITCODE" } | |
| } | |
| if (-not (Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) { | |
| New-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -DisplayName "OpenSSH Server (sshd)" -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 | Out-Null | |
| } | |
| Start-Service sshd; | |
| Set-Service -Name sshd -StartupType Automatic; | |
| Write-Host "airc: sshd ready (capability + HNS + firewall + service auto-start)"; | |
| } catch { | |
| Write-Host "airc-elevated-error: $_" | |
| exit 1 | |
| } |
| & reg add 'HKLM\SYSTEM\CurrentControlSet\Services\hns\State' /v 'EnableExcludedPortRange' /d 0 /f 2>$null | Out-Null | ||
| } | ||
|
|
||
| # Check if port 22 is already in the static excluded-port-range. | ||
| $existing = & netsh int ipv4 show excludedportrange protocol=tcp 2>$null | Out-String | ||
| if ($existing -match '(?m)^\s*22\s+22\b') { | ||
| # Already reserved. | ||
| return | ||
| } | ||
| Write-Host ' Reserving port 22 in static excluded-port-range (netsh) ...' | ||
| & netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 2>$null | Out-Null |
There was a problem hiding this comment.
In Windows PowerShell, non-zero exit codes from native commands (reg, netsh) won’t trigger catch. This function currently suppresses stderr and doesn’t check $LASTEXITCODE, so the HNS workaround can fail silently (and the caller will continue as if it succeeded). After each reg/netsh call, check $LASTEXITCODE and throw/emit a warning on failure so the install can report an actionable error.
| & reg add 'HKLM\SYSTEM\CurrentControlSet\Services\hns\State' /v 'EnableExcludedPortRange' /d 0 /f 2>$null | Out-Null | |
| } | |
| # Check if port 22 is already in the static excluded-port-range. | |
| $existing = & netsh int ipv4 show excludedportrange protocol=tcp 2>$null | Out-String | |
| if ($existing -match '(?m)^\s*22\s+22\b') { | |
| # Already reserved. | |
| return | |
| } | |
| Write-Host ' Reserving port 22 in static excluded-port-range (netsh) ...' | |
| & netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 2>$null | Out-Null | |
| & reg add 'HKLM\SYSTEM\CurrentControlSet\Services\hns\State' /v 'EnableExcludedPortRange' /d 0 /f 2>$null | Out-Null | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "Failed to disable HNS auto-exclusion via reg.exe (exit code $LASTEXITCODE)." | |
| } | |
| } | |
| # Check if port 22 is already in the static excluded-port-range. | |
| $existing = & netsh int ipv4 show excludedportrange protocol=tcp 2>$null | Out-String | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "Failed to query excluded port ranges via netsh.exe (exit code $LASTEXITCODE)." | |
| } | |
| if ($existing -match '(?m)^\s*22\s+22\b') { | |
| # Already reserved. | |
| return | |
| } | |
| Write-Host ' Reserving port 22 in static excluded-port-range (netsh) ...' | |
| & netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1 2>$null | Out-Null | |
| if ($LASTEXITCODE -ne 0) { | |
| throw "Failed to reserve port 22 via netsh.exe (exit code $LASTEXITCODE)." | |
| } |
| @@ -163,22 +204,37 @@ function Install-OpenSSHServer { | |||
| } | |||
| Write-Step 'Installing + starting OpenSSH Server (admin required) ...' | |||
| try { | |||
There was a problem hiding this comment.
Install-OpenSSHServer returns early when sshd is already running, which skips the new “persistent” steps (HNS registry/netsh reservation + firewall rule). Since the HNS issue is about future boots, it’s worth running Set-HnsPortFreedomFor22 (idempotent) and the firewall-rule check even when sshd is currently Running, while still skipping the capability install/start.
| printf " Fix (admin PowerShell):\n" | ||
| printf " Fix (admin PowerShell — five lines, run all together):\n" | ||
| printf " Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0\n" | ||
| printf " reg add HKLM\\\\SYSTEM\\\\CurrentControlSet\\\\Services\\\\hns\\\\State /v EnableExcludedPortRange /d 0 /f\n" |
There was a problem hiding this comment.
This printf line is over-escaped: in bash, \\ in a double-quoted string prints \, so users will see HKLM\\SYSTEM... with double backslashes. For reg add the conventional path uses single backslashes (HKLM\SYSTEM...). Reduce escaping so the printed command is exactly what the user should paste.
| printf " reg add HKLM\\\\SYSTEM\\\\CurrentControlSet\\\\Services\\\\hns\\\\State /v EnableExcludedPortRange /d 0 /f\n" | |
| printf " reg add HKLM\\SYSTEM\\CurrentControlSet\\Services\\hns\\State /v EnableExcludedPortRange /d 0 /f\n" |
| printf " Fix (admin PowerShell — five lines, run all together):\n" | ||
| printf " Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0\n" | ||
| printf " reg add HKLM\\\\SYSTEM\\\\CurrentControlSet\\\\Services\\\\hns\\\\State /v EnableExcludedPortRange /d 0 /f\n" | ||
| printf " netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1\n" | ||
| printf " Start-Service sshd\n" | ||
| printf " Set-Service -Name sshd -StartupType Automatic\n" | ||
| printf " (The reg+netsh lines work around Windows HNS holding port 22 randomly per boot —\n" | ||
| printf " continuum-b69f's diagnosis 2026-04-27. Without them, sshd bind returns EPERM.)\n" | ||
| return 1 |
There was a problem hiding this comment.
The HNS port-22 issue can manifest as “sshd installed but not running” (service fails to start/bind), not only as the missing-capability case. The doctor hint was expanded only for the "" (missing) branch; consider also updating the Stopped|StopPending|... branch above to include the same reg+netsh remediation (or at least mention it) so airc doctor is actionable when the service exists but won’t start due to EPERM.
| info "Configuring OpenSSH.Server + HNS port-22 reservation (UAC prompt incoming)." | ||
| info " airc joiners need this to ssh-tail your messages.jsonl when you host." | ||
| powershell.exe -NoProfile -Command "Start-Process powershell -Verb RunAs -Wait -ArgumentList '-NoProfile -Command \"$_elevated_payload\"'" 2>&1 \ | ||
| && ok "OpenSSH.Server installed + started + HNS port-22 reserved + auto-start." \ | ||
| || warn "Self-elevation failed. Run in admin PowerShell: |
There was a problem hiding this comment.
Start-Process ... -Wait does not propagate the elevated process exit code back to the caller. Also, the payload’s catch { Write-Host ... } swallows errors and will still exit 0. As a result, this function can print the success ok message even when the elevated steps (capability install / reg / netsh / service start) failed. Use -PassThru to capture the process, check $p.ExitCode, and ensure failures exit non-zero (and avoid swallowing errors in the payload).
| $cap = Get-WindowsCapability -Online -Name "OpenSSH.Server*"; | ||
| if ($cap.State -ne "Installed") { Add-WindowsCapability -Online -Name $cap.Name | Out-Null } | ||
| $reg = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\hns\State" -Name "EnableExcludedPortRange" -ErrorAction SilentlyContinue).EnableExcludedPortRange; | ||
| if ($reg -ne 0) { reg add "HKLM\SYSTEM\CurrentControlSet\Services\hns\State" /v "EnableExcludedPortRange" /d 0 /f | Out-Null } |
There was a problem hiding this comment.
The reg add ... /d 0 call omits /t REG_DWORD. If EnableExcludedPortRange is missing, reg add will create a REG_SZ by default, which may not be honored by HNS. Prefer Set-ItemProperty/New-ItemProperty -PropertyType DWord or specify /t REG_DWORD explicitly to ensure the value type is correct.
| if ($reg -ne 0) { reg add "HKLM\SYSTEM\CurrentControlSet\Services\hns\State" /v "EnableExcludedPortRange" /d 0 /f | Out-Null } | |
| if ($reg -ne 0) { reg add "HKLM\SYSTEM\CurrentControlSet\Services\hns\State" /v "EnableExcludedPortRange" /t REG_DWORD /d 0 /f | Out-Null } |
| } catch { } | ||
| if ($needRegWrite) { | ||
| Write-Host ' Disabling HNS auto-exclusion (HKLM\...\hns\State EnableExcludedPortRange = 0) ...' | ||
| & reg add 'HKLM\SYSTEM\CurrentControlSet\Services\hns\State' /v 'EnableExcludedPortRange' /d 0 /f 2>$null | Out-Null |
There was a problem hiding this comment.
Set-HnsPortFreedomFor22 writes EnableExcludedPortRange via reg add ... /d 0 without specifying the value type. If the value doesn’t exist, reg add defaults to REG_SZ, but this setting is expected to be a DWORD. Use Set-ItemProperty/New-ItemProperty -PropertyType DWord or add /t REG_DWORD to ensure the registry value is created/updated with the correct type.
| & reg add 'HKLM\SYSTEM\CurrentControlSet\Services\hns\State' /v 'EnableExcludedPortRange' /d 0 /f 2>$null | Out-Null | |
| & reg add 'HKLM\SYSTEM\CurrentControlSet\Services\hns\State' /v 'EnableExcludedPortRange' /t REG_DWORD /d 0 /f 2>$null | Out-Null |
…nd-to-end (#177) fix(msys): export MSYS2_ARG_CONV_EXCL at airc startup — last layer of cross-machine fix (continuum's catch + verify) continuum-b69f's diagnosis 2026-04-27: even with PR #174 + #176's argparse `--flags`, MSYS Git Bash on Windows translates argv VALUES that look like Unix-rooted paths when bash invokes a Windows-native binary. So `--host-airc-home /Users/joelteply/.airc` arrived at python.exe as `--host-airc-home C:/Program Files/Git/Users/joelteply/.airc`, the joiner cached the corrupted path, the SSH command later sent it to a real Unix host that had no such file. Silent broadcast failure. ## Fix ```bash export MSYS2_ARG_CONV_EXCL="${MSYS2_ARG_CONV_EXCL:-/Users/;/home/;/root/}" ``` Set once at airc startup, exported, every airc_core invocation inherits the same translation policy. Targeted prefix list covers macOS / Linux / root home prefixes without breaking `/tmp/` or `/c/` paths (which DO need translation for `--config "$CONFIG"` where $CONFIG is on the local Windows filesystem). Honors a user override via the `${...:-...}` default-fallback. ## End-to-end verification continuum-b69f shipped a test broadcast from Windows after their local patch: > WORKING TEST: Windows-Mac airc msg via continuum-msyspatch with > targeted MSYS exclude. should land! Verified live in MY host's messages.jsonl on Mac: ``` {"from":"continuum-msyspatch","to":"all","ts":"2026-04-27T23:30:13Z", "msg":"WORKING TEST: Windows-Mac airc msg via continuum-msyspatch with targeted MSYS exclude. should land!","sig":"..."} ``` **Cross-machine Mac↔Windows airc end-to-end working.** This was the last bug in the chain that started at PR #153 (Microsoft Store python3 stub). ## Test posture (Mac, where the env var is a no-op) - tabs 19/19, identity 19/19, whois 5/5, part_persists 8/8, list 4/4, general_sidecar_default 12/12, kick 12/12, events 5/5, platform_adapters 11/11, whois_cross_scope 6/6 ## Today's full chain of cross-machine fixes #153 → #154 → #155 → #156 → #157 → #158 → #159 → #160 → #162 → #164 → #165 → #166 → ... → #176 → this. 27+ PRs to ship a working cross-machine airc on Windows. Every step revealed a new layer.
Bug (continuum-b69f via cross-Mac/Windows coord gist 2026-04-27)
`Start-Service sshd` on Windows: "Cannot bind any address" / permission denied even WITH admin. Root cause: Windows HNS (Host Network Service — backs Hyper-V/WSL2/Docker) dynamically reserves port ranges at boot. Per-boot rotation. NOT visible in `netsh int ipv4 show excludedportrange` (which shows static admin reservations only). When port 22 randomly lands inside an HNS-held range, sshd bind returns EPERM at the OS layer.
References continuum cited:
Persistent fix
Two-step workaround inside the admin-elevated install payload, both idempotent:
```powershell
reg add HKLM\SYSTEM\CurrentControlSet\Services\hns\State /v EnableExcludedPortRange /d 0 /f
netsh int ipv4 add excludedportrange protocol=tcp startport=22 numberofports=1
```
Plus a New-NetFirewallRule for OpenSSH-Server-In-TCP (capability install creates it, but defensive idempotent check).
Files
Why this matters
Pre-fix, install or doctor's hint could leave a user partially fixed: capability installed, service set automatic — but `Start-Service sshd` still EPERM'd because HNS owned port 22. Looked like a permission bug. Continuum's diagnosis converts this from "random Windows mystery" to "one-time install action."
Mac regression
HNS branch only fires on MINGW/MSYS/CYGWIN. Mac unchanged.