Execute Commands Through SSH on Remote Windows

bashshellshell-scriptsshwindows

My remote machine is a windows and I am doing ssh from ubuntu to windows.

I want to use git bash instead of command prompt, but running the ssh will bring me to the default command prompt, is there any way that I could invoke git bash through ssh?

I try the following command not working:

ssh Administrator@54.xxx.xxx.207 < "C:\Program Files\Git\bin\sh.exe"

I can run the following command to after ssh to start the git bash, but I am seeking a way to automate this process?

"C:\Program Files\Git\bin\sh.exe" --login

I am using Windows 2012 R2

Best Answer

Yes, you can if you have OpenSSH For Windows Server 2019 and Windows 10 installed.

From OpenSSH Server Configuration for Windows 10 1809 and Server 2019:

Configuring the default ssh shell is done in the Windows registry by adding the full path to the shell executable to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH in the string value DefaultShell.

As an example, the following Powershell command sets the default shell to be PowerShell.exe:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force

To set Git Bash as default shell, run this snippet as administrator in the PowerShell:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\Git\bin\sh.exe" -PropertyType String -Force

To get a login shell, you could create a batch file sh.bat in C:\Program Files\Git\bin with this content:

@echo off
"C:\Program Files\Git\bin\sh.exe" --login

and replace the DefaultShell value with C:\Program Files\Git\bin\sh.bat.

The DefaultShell registry key is a new key, you can delete it to restore the default command prompt.

Related Question