PowerShell 7无法使用SSH的问题排查与解决求助
解决PowerShell 7中无法识别ssh命令的问题
问题重现
在PowerShell 7中执行ssh命令时出现如下错误:
ssh: The term 'ssh' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
该命令在CMD和Windows PowerShell中可正常运行,但无法直接切换到C:\Windows\System32\OpenSSH目录。
解决方案
1. 检查PowerShell 7的进程位数
这是最常见的原因:Windows的OpenSSH默认安装在C:\Windows\System32\OpenSSH,但32位版本的PowerShell 7会被系统重定向,无法直接访问System32目录(实际访问的是C:\Windows\SysWOW64)。
- 在PowerShell 7中执行:
[Environment]::Is64BitProcess - 如果返回
False,说明你使用的是32位PowerShell 7,卸载并重新安装64位版本即可解决问题。
2. 绕过目录重定向访问OpenSSH(临时方案)
如果暂时无法切换到64位PowerShell,可通过Sysnative路径绕过重定向:
- 执行以下命令进入OpenSSH目录:
cd C:\Windows\Sysnative\OpenSSH - 直接运行完整路径的ssh命令测试:
C:\Windows\Sysnative\OpenSSH\ssh.exe
3. 刷新环境变量
如果已经确认是64位PowerShell 7,但仍无法识别ssh,可能是环境变量未生效:
- 执行以下命令强制刷新系统Path到当前会话:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") - 之后再尝试执行
ssh命令。
4. 重新安装OpenSSH客户端
确保OpenSSH客户端安装正确:
- 打开Windows设置 → 应用 → 可选功能
- 找到「OpenSSH 客户端」,点击卸载
- 卸载完成后,点击「添加功能」,重新搜索并安装「OpenSSH 客户端」
- 安装完成后重启PowerShell 7测试
内容的提问来源于stack exchange,提问作者Adnan Bin Zahir
相关产品推荐
相关产品推荐

