10

Is there a way to configure OpenSSH on Windows 7 in to initialize another shell other than the default Windows command shell?

me@linuxhost:~
$ ssh me@windowshost
me@windowshost's password:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

me@windowshost C:\Users\me> exit
Connection to windowshost closed.
me@linuxhost:~
$

Alternately, installing Cygwin and including OpenSSH in the additional Net packages results in a default Cygwin shell, so the same question stands: is there a way to configure the shell initialized by OpenSSH after installation?

EDIT:

Thank you @simlev for your suggestion to use Cygwin I have reworded my question to more clearly represent my problem.

1
  • You can do this easier with a dedicated tool. See for example if you like Gitblit.
    – harrymc
    Feb 27, 2017 at 20:26

4 Answers 4

17

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 executable as string value.

For Powershell,

Set-ItemProperty -verbose -path "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH" -Name "DefaultShell" -Value $(Get-Command powershell.exe).Source

For CygWin Bash

Set-ItemProperty -verbose -path "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH" -Name "DefaultShell" -Value "C:\Cygwin64\bin\bash.exe"

Setting the registry key to an empty string will revert to the default behavior of using cmd.exe as the shell.


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.
2
6
+50

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.

1
  • This does not make use of the tools you mention (OpenSSH and Git Bash) but allows you to reach your goals (scp, Bash).
    – simlev
    Feb 27, 2017 at 19:35
1

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.

1
  • With Powershell being under heavy development, things have changed: see Brethlosze's recent answer.
    – simlev
    Jan 23, 2019 at 18:03
1

FWIW, I put together a packaging of the Cygwin port of OpenSSH that has an easy to use installer and also somewhat detailed documentation in PDF and CHM format. It's on Github:

https://github.com/Bill-Stewart/CygSSH

It uses Cygwin v3 so it doesn't require a dedicated service account (the service runs as SYSTEM).

My intent was to build a convenient packaging of Cygwin and rsync that would be (relatively) convenient to install and use on Windows machines, so it has some defaults and capabilities that are more Windows-centric:

  • Installer supports silent installation and adding to the system path
  • Windows PowerShell is the remote interactive shell
  • It creates an SSH Users local group for authentication and sets that by default in sshd_config; nested groups are supported (e.g., if you want to manage the membership via GPO)
  • It supports granting sftp-only access to accounts
  • It includes rsync

It requires Windows 8.1 64-bit or Windows Server 2012 R2 or later.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .