为 Windows SSH 添加密钥对

环境

官方文档

速查手册

使用 ECDSA 生成密钥对:

ssh-keygen -t ecdsa

将公钥写入服务器对应用户的 $env:USERPROFILE\.ssh\authorized_keys

$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ecdsa.pub
$remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"
ssh <your user name>@<server> $remotePowershell

注释 C:\ProgramData\ssh\sshd_config 的最后两行,阻止 sshd 修改具有管理员权限的用户使用的 authorized_keys