jozxyqk
jozxyqk

Reputation: 17266

git + vscode connect to windows with openssh (remote development)

Setup

I'm connecting to a remote windows machine using OpenSSH. I.e. windows is running the "OpenSSH SSH Server" service. The windows machine has a git repo, e.g. a working directory at C:\my_git_repo\* with files I want to edit. How can I get both git push/pull and vscode's remote development extension to work from my local machine?

I have set up OpenSSH on a windows machine (I'm connecting from linux but I think that's irrelevant): Setting up OpenSSH for Windows using public key authentication - the sshd_config edit was important.

Git

After this I can ssh my-window-hostname and I get a cmd.exe prompt. However git push/pull does not work:

'git-upload-pack' is not recognized as an internal or external command,
operable program or batch file.
fatal: Could not read from remote repository.

To fix that, I changed the OpenSSH shell to use the MINGW64 bash.exe that comes with gitforwindows as described here: How do I git clone from a Windows machine over ssh?

Vscode

The problem is now that vscode's remote development extension will not connect to my-window-hostname. In the logs I see a successful ssh connection, then the MINGW64 shell version and then Terminating local server:

[13:25:29.256] stderr> Authenticated to my-window-hostname ([192.168.0.123]:22) using "publickey".
[13:25:30.579] > ready: eaa158feaecd
[13:25:30.703] > MINGW64_NT-10.0-19042 3.3.4-341.x86_64 2022-02-23 17:44 UTC
[13:25:31.729] > local-server-1> Timed out
[13:25:31.737] Local server exit: 0
[13:25:45.414] Terminating local server

Someone else has had a similar problem before connecting from macos: https://github.com/microsoft/vscode-remote-release/issues/6359 Incidentally, I can ssh my-window-hostname powershell just fine. Maybe this is a vscode bug, making an assumption about the shell it gets?

Both?

It sounds like git needs the bash.exe shell while vscode expects cmd. I tried removing the bash.exe OpenSSH config and vscode started working again. For git I instead set the following in my ~/.ssh/config (vscode ignores RemoteCommand by default):

Host my-window-hostname
    RemoteCommand "C:\Program Files\Git\bin\bash.exe"
    RequestTTY yes

After this I see the MINGW64 bash.exe prompt after a regular ssh my-window-hostname however, git is still broken:

fatal: Could not read from remote repository.

This is probably related to git not expecting a TTY/interactive shell, or maybe it's ignoring RemoteCommand too.

What else can I do?

Upvotes: 1

Views: 611

Answers (1)

jozxyqk
jozxyqk

Reputation: 17266

Since asking here, enableRemoteCommand has been implemented. This allows you to use cmd.exe as the shell by adding RemoteCommand cmd.exe to .ssh/config. Better yet, a vscode-only config can be created:

Create ~/.ssh/config_vscode (e.g. copy from ~/.ssh/config) and add:

Host server-hostname
    RemoteCommand cmd.exe
    RequestTTY yes

Set the following User Settings in vscode:

{
    ...
    "remote.SSH.enableRemoteCommand": true,
    "remote.SSH.configFile": "/home/username/.ssh/config_vscode",
    "remote.SSH.useLocalServer": false
}

Finally I can now ssh from the command line, add the server as a git remote and run git fetch and connect to the server with vscode.

The useLocalServer option came from here. I blindly added it when I was still seeing a timeout.

I'm still hoping for a more straightforward or easier method. It'd be nice if vscode's remote ssh added a simple shell option.

Upvotes: 1

Related Questions