将 ssh 密钥从 Windows 机器复制到 Windows Server 2019

问题描述

我一直在尝试通过 OpenSSH 协议无需密码即可访问 Windows Server 2019。 所以我创建了新的密钥,我需要将它复制到 Windows Server,我试过这个:

ssh-copy-id -i ~/.ssh/id_rsa user@server

但是我在输入正确的密码后得到这个:

'exec' is not recognized as an internal or external command,operable program or batch file.
The system cannot find the path specified.
The system cannot find the path specified.

我的问题是如何从一台 Windows 机器传输密钥(使用 gitbash、WSL、powershell 或其他) 如果我没记错的话,请转到授权密钥的 Windows Server 2019 位置。

我非常渴望手动完成,但这些密钥的位置对我来说很神秘,我是否需要先在 Windows Server 上设置一些东西,以便它可以接受用于身份验证的密钥?

ssh-copy-id 从 Windows 机器到 Windows Server 2019 的替代方法是什么?

找到解决方案: 按照这个有用的 youtube 指南,道具到 https://www.youtube.com/watch?v=Cs3wBl_mMH0&ab_channel=IT%2FOpsTalk-Deprecated-SeeChannelDescription

此外,安装 OpenSSHUtils 可以使用:

Install-Module -Name OpenSSHUtils -requiredVersion 0.0.2.0 -Scope AllUsers

本指南也有帮助:

https://www.cloudsma.com/2018/03/installing-powershell-modules-on/

我的服务器没有访问权限,所以我手动复制了以下文件: C:\Program Files\WindowsPowerShell\Modules 到服务器的: 服务器:\Program Files\WindowsPowerShell\Modules

解决方法

首先,此错误信息后跟 microsoft/vscode-remote-release issue 25

当前的解决方法(上下文是 VSCode,但也应适用于常规 SSH 连接):

此外,对于喜欢在 Windows 上使用 bash 但仍希望能够远程使用 VSCode 的其他任何人,我目前设置的解决方法是使用部署在服务器上的 autorun.cmd 来检测 SSH连接正在进入并分配了一个终端:

@echo off

if defined SSH_CLIENT (
    :: check if we've got a terminal hooked up; if not,don't run bash.exe
    C:\cygwin\bin\bash.exe -c "if [ -t 1 ]; then exit 1; fi"
    if errorlevel 1 (
        C:\cygwin\bin\bash.exe --login
        exit
    )
)

众所周知,这适用于 Cygwin bash,不确定 Windows 附带的 bash;我想它对 TTY 代码在内部的工作方式非常敏感。
这样,启动 cmd.exe 可以正常工作,使用 VSCode(因为它没有分配 PTY)可以正常工作,但是 SSH 进入机器会启动 bash.exe。

我怀疑它也可以使用 Git for Windows 附带的 bash.exe,如果它安装在目标服务器上。

目标文件应该在服务器上:

%USERPROFILE%\.ssh\authorized_keys

如果您可以手动完成,只需尝试scp而不是使用ssh-copy-id

scp user@server:C:/Users/<user>/.ssh/authorized_key authorized_key 
# manual and local edit to add the public key
scp authorized_key  user@server:C:/Users/<user>/.ssh/authorized_key

(同样,我会使用 Git For Windows 附带的 scp.exe,这次是在本地安装的)

,

找到解决方案: 按照这个有用的 youtube 指南,道具到 https://www.youtube.com/watch?v=Cs3wBl_mMH0&ab_channel=IT%2FOpsTalk-Deprecated-SeeChannelDescription

此外,安装 OpenSSHUtils 可以使用:

Install-Module -Name OpenSSHUtils -RequiredVersion 0.0.2.0 -Scope AllUsers

本指南也有帮助:

https://www.cloudsma.com/2018/03/installing-powershell-modules-on/

我的服务器没有访问权限,所以我手动复制了以下文件: C:\Program Files\WindowsPowerShell\Modules 到服务器的: 服务器:\Program Files\WindowsPowerShell\Modules