Copying ssh key from windows machine to windows server 2019

Viewed 1194

I've been trying to get access to Windows Server 2019 without password through OpenSSH protocol. So I've created new key which I need it to be copied to the Windows Server, I've tried this:

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

But I get this after entering correct password:

'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.

My issue is how to transfer key from one windows machine(using gitbash, WSL, powershell or whatever) to Windows Server 2019 location of authorized keys if I am not mistaken.

I am desperate enough to do it manually but location of those keys is mystery to me, do I need to set something on Windows Server first so that it can accept keys for authentication ?

What is the alternative on ssh-copy-id from Windows machine to Windows Server 2019 ?

Found solution: Followed this helpful youtube guide, props to the https://www.youtube.com/watch?v=Cs3wBl_mMH0&ab_channel=IT%2FOpsTalk-Deprecated-SeeChannelDescription

Also, installing OpenSSHUtils worked with:

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

Also this guide helped:

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

My server didn't have access so I manually copied file from: C:\Program Files\WindowsPowerShell\Modules to the server's: Server:\Program Files\WindowsPowerShell\Modules

2 Answers

First, this error message is followed by microsoft/vscode-remote-release issue 25

Current workaround (the context is VSCode, but should apply also for regular SSH connection):

Also, for anyone else here that loves their bash on windows but still wants to be able to use VSCode remote, the workaround I have currently setup is to use an autorun.cmd deployed on the servers that detects when an SSH connection is coming in and has a terminal allocated:

@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
    )
)

This is known to work with Cygwin bash, unsure about bash that ships with windows; I imagine it's very sensitive to how the TTY code works internally.
This way, launching cmd.exe works normally, using VSCode (because it does not allocate a PTY) works normally, but SSH'ing into the machine launches bash.exe.

I suspect it would also work using the bash.exe which comes with Git for Windows, should it be installed on the target server.

The destination file should be on the server:

%USERPROFILE%\.ssh\authorized_keys

If you can do it manually, simply try and scp it instead of using 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

(again, I would use the scp.exe coming with Git For Windows, installed this time locally)

Found solution: Followed this helpful youtube guide, props to the https://www.youtube.com/watch?v=Cs3wBl_mMH0&ab_channel=IT%2FOpsTalk-Deprecated-SeeChannelDescription

Also, installing OpenSSHUtils worked with:

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

Also this guide helped:

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

My server didn't have access so I manually copied file from: C:\Program Files\WindowsPowerShell\Modules to the server's: Server:\Program Files\WindowsPowerShell\Modules

Related