Windows现在也自带OpenSSH了,这个策略应该是向macOS学的,macOS甚至集成了zsh,作为基本的shell。

安装OpenSSH

OpenSSH可以从Settings App中添加,参考这个How to Enable SSH Commands in Windows。 也可以从PowerShell添加,参考Get started with OpenSSH for Windows, 打开一个管理员权限的PowerShell:

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

查看OpenSSH的客户端和服务端是否安装,输出结果:

Name  : OpenSSH.Client~~~~0.0.1.0
State : NotPresent

Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

下面的命令用于安装所需的功能:

# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

如果遇到问题Add-WindowsCapability : Add-WindowsCapability failed. Error code = 0x800f0954的话,参考[Solved] Add-WindowsCapability failed. Error code = 0x800f0954 – RSAT Fix

  • 运行组策略编辑器gpedit.msc
  • 点击Computer Configuration -> Administrative Templates -> System
  • 打开Specify settings for optional component installation and component repair
    • 将其设置成Enabled
    • 然后选中Download repair content and optional features directly from Windows Updates instead of Windows Server Updates Services (WSUS)
  • 执行gpupdate /force更新组策略
  • 然后再执行Add-WindowsCapability

卸载的话执行:

# Uninstall the OpenSSH Client
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# Uninstall the OpenSSH Server
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

然后登录服务端的话,执行:ssh domain\username@servername

配置Git使用OpenSSH

参考Setting up SSH-Agent in Windows for Passwordless Git Authentication

安装ssh-agent服务:Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service

然后通过start-ssh-agent.cmd可以直接开启服务,而不同等重启。

接着配置Git:git config --global core.sshCommand C:/Windows/System32/OpenSSH/ssh.exe,让其使用windows自带的SSH。

但是Windows自带的SSH存在一个问题,如果是域用户,那么此SSH会把用户名弄成domain\user这种形式,而服务器可能只需要user部分。

其他参考