You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11下PowerShell无法识别已安装且在PATH中的SSH命令

问题:Windows 11 PowerShell中SSH命令失效

问题现象

执行ssh命令时收到错误:

C:\Users\ivan> ssh
ssh : The term 'ssh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ ssh
+ ~~~
    + CategoryInfo          : ObjectNotFound: (ssh:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

环境检查

  • 环境变量$env:path包含%SYSTEMROOT%\System32\OpenSSH\:
    PS C:\Users\ivan> $env:path
    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\usbipd-win\;C:\Program Files\dotnet\;
    
  • $env:systemroot值为C:\WINDOWS:
    PS C:\Users\ivan> $env:systemroot
    C:\WINDOWS
    
  • 手动进入C:\windows\system32\OpenSSH\目录执行./ssh可正常运行:
    PS C:\Users\ivan> cd C:\windows\system32\OpenSSH\
    PS C:\windows\system32\OpenSSH> ./ssh
    usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
               [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
               [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
               [-i identity_file] [-J [user@]host[:port]] [-L address]
               [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
               [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
               [-w local_tun[:remote_tun]] destination [command]
    

解决方法

1. 强制刷新环境变量

PowerShell有时不会实时更新环境变量,执行以下命令重新加载系统和用户级PATH:

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

执行后直接输入ssh测试。

2. 替换为绝对路径或统一PATH大小写

将PATH中的%SYSTEMROOT%\System32\OpenSSH\替换为绝对路径C:\WINDOWS\system32\OpenSSH\,或者统一为%SystemRoot%\system32\OpenSSH\(匹配实际路径的小写格式)。操作步骤:

  • 右键「此电脑」→「属性」→「高级系统设置」→「环境变量」
  • 在「系统变量」中找到Path,点击「编辑」
  • 修改对应路径后点击「确定」,重启PowerShell生效。

3. 重新安装OpenSSH客户端

  • 打开「设置」→「应用」→「可选功能」
  • 找到「OpenSSH 客户端」,点击「卸载」
  • 卸载完成后,点击「添加可选功能」,搜索并重新安装「OpenSSH 客户端」
  • 重启PowerShell测试。

4. 检查并调整PowerShell执行策略

执行以下命令查看当前执行策略:

Get-ExecutionPolicy

如果策略为Restricted,执行以下命令修改为RemoteSigned(仅针对当前用户):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

重启PowerShell后测试SSH命令。


内容的提问来源于stack exchange,提问作者Ivan Kvolik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:13:09