启用 Windows 的 ssh-agent 服务

OpenSSH agent 在 Windows 上默认是禁用的——三行 PowerShell 让密钥口令一劳永逸。

WindowsSSH 远程连接Git服务低风险
把 ssh-agent 服务设为自动启动并启动,然后用 ssh-add 加载你的密钥。

01命令

命令
$ Get-Service ssh-agent | Set-Service -StartupType Automatic ; Start-Service ssh-agent ; ssh-add $env:USERPROFILE\.ssh\id_ed25519

02作用说明

与 Linux 不同,Windows 出厂时 ssh-agent 服务的启动类型是"禁用",因此直接运行 ssh-add 会报"无法建立连接"。设为自动启动后,agent 从登录起就持有解密后的密钥,git push 和 ssh 不再反复询问口令。Windows 上的 agent 还可以通过可选桥接供 WSL 等工具使用,密钥存储在 Windows 密钥存储区而非套接字文件中。

03风险

风险低风险
使用条件Requires an elevated PowerShell to change the service startup type; OpenSSH client installed

04撤销

撤销
$ Set-Service ssh-agent -StartupType Disabled ; Stop-Service ssh-agent

05来源

  • SSH key management for OpenSSH on Windows
    Microsoft Learn · vendor-doc · 2026-09-17
    依据: Documents enabling the ssh-agent Windows service via Set-Service and loading keys with ssh-add, exactly as in this command.

最后验证: 2026-09-17 · 分类: 开发者