让 git 使用 Windows 10 OpenSSH

问题

在 Windows 10 安装 git 的同时,开启系统自带的 OpenSSH,则系统里存在两套 ssh,git 会默认使用它自己的那套。

分析

由于 Windows 10 的 sshd、ssh-agent 做成服务,比较容易管理,而且微软改造的版本会更注重安全,所以 UMU 决定舍弃 git 带的那套。

解决

1
git config --global core.sshcommand "C:/Windows/System32/OpenSSH/ssh.exe"

以上方法是自定义私钥路径时惯用的(加 -i 参数),但这里我们只改变 ssh 本身路径。参考文档:

core.sshCommand
If this variable is set, git fetch and git push will use the specified command instead of ssh when they need to connect to a remote system. The command is in the same form as the GIT_SSH_COMMAND environment variable and is overridden when the environment variable is set.

另一种比较暴力的方式:把 git 那套 ssh 指向 Windows 10 OpenSSH。

用管理员权限运行 cmd,输入:

1
2
3
4
5
6
cd "%ProgramFiles%\git\usr\bin"
ren ssh.exe -ssh.exe
mklink ssh.exe %windir%\System32\OpenSSH\ssh.exe

ren scp.exe -scp.exe
mklink scp.exe %windir%\System32\OpenSSH\scp.exe

git ssh 链接到 OpenSSH