Configure default shell initialized by OpenSSH on Windows 7

According to this you can configure the DefaultShell for OpenSSH in Windows, to be PowerShell or any other executable.

It requires to add the String Registry Key Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH\DefaultShell with the path of the Shell Executable as string value, i.e.:

  • Powershell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

  • CygWin Bash: C:\Cygwin64\bin\bash.exe

I tested it and works as expected.


From the Win32-OpenSSH wiki:

On the server side, configure the default ssh shell in the windows registry.

  • Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH\DefaultShell - full path of the shell executable
  • Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH\DefaultShellCommandOption (optional) - switch that the configured default shell requires to execute a command, immediately exit and return to the calling process. By default this is -c.
  • Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH\DefaultShellEscapeArguments (optional) - flag that allow you to skip escaping the arguments of default shell. By default this is 0. This option is only applicable to shells other than powershell, powershell, bash, cygwin, cmd, and ssh-shellhost.exe.

My warm recommendation is to use Cygwin to accept ssh connections on your Windows machine. This would allow you to scp to and from it, as well as login from a remote system via ssh to a Bash shell and command-line git.

user@linuxhost$ ssh 192.168.x.x
Last login: Sun Feb 12 08:20:07 2017 from 10.x.x.x
user@windowshost$ echo $0 && git --version
-bash
git version 2.8.3

The shell can of course be customized: ash, bash, dash and sh are included by default, but just run the Cygwin installer and you can add your choice of zsh, mksh, tcsh or posh. Then add the following line to /etc/nsswitch.conf:

db_shell: /bin/sh

possibly substituting /usr/bin/sh with the path to your preferred shell. All Cygwin processes (terminal windows and sshd service) must be restarted for the setting to take effect.

There is even a way to get a cmd or powershell prompt upon login, which I recently found out about on Stackoverflow.

  1. Download winpty for Cygwin and extract winpty.exe, winpty.dll and winpty-agent.exe to /bin. If you do this from outside of a Cygwin terminal, look for a bin subdirectory of the Cygwin installation folder.
  2. Create two batch files in /binand make sure they have execute permissions. Let's name them winpty-cmd.bat and winpty-powershell.bat and fill them with the following contents, where of course <cygwin path> is a placeholder for the path you installed Cygwin to (by default it's C:\cygwin):

    @ECHO OFF
    <cygwin path>\bin\winpty.exe cmd
    

    and

    @ECHO OFF
    <cygwin path>\bin\winpty.exe powershell
    
  3. Put one of these lines into /etc/nsswitch.conf:

    db_shell: /bin/winpty-cmd.bat
    

    or

    db_shell: /bin/winpty-powershell.bat
    
  4. Restart the sshd service.

Since the title has been "reworded" and my post might now seem sligthly out of place, allow me to directly answer the original question:

"Use git bash instead of cmd when sshing from Linux"

In addition to using Cygwin to accept ssh connections on the Windows machine:

  1. Follow step 1. above.
  2. Create a file called <cygwin path>\bin\winpty-gitbash.bat with these contents, where <cygwin path> is a placeholder for the path you installed git-for-windows to (by default it's C:\Program Files\Git):

    @ECHO OFF
    SET PATH="/bin"
    <cygwin path>\bin\winpty.exe "<git path>\bin\bash.exe"
    
  3. Put this line into /etc/nsswitch.conf:

    db_shell: /bin/winpty-gitbash.bat
    
  4. Restart the sshd service.

Update - September 2018:

Things change, and Win32-OpenSSH now allows choosing the default shell (any shell actually) by setting its path in a registry key. See Brethlosze's recent answer.


Win32-OpenSSH hardcodes cmd.exe as the default shell in the source: see lines 978-984 and 1081-1086 of shell-host.c. With that said, it appears the only way to change the default shell is to change it in those locations and recompile OpenSSH.

Tags:

Ssh

Openssh

Sshd