Configure default shell initialized by OpenSSH on Windows 7

4

4

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.

mlegge

Posted 2017-02-24T21:35:13.127

Reputation: 93

You can do this easier with a dedicated tool. See for example if you like Gitblit.

– harrymc – 2017-02-27T20:26:18.963

Answers

2

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.

Joseph Sible-Reinstate Monica

Posted 2017-02-24T21:35:13.127

Reputation: 1 420

With Powershell being under heavy development, things have changed: see Brethlosze's recent answer.

– simlev – 2019-01-23T18:03:22.197

6

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.

simlev

Posted 2017-02-24T21:35:13.127

Reputation: 3 184

This does not make use of the tools you mention (OpenSSH and Git Bash) but allows you to reach your goals (scp, Bash). – simlev – 2017-02-27T19:35:58.237

2

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.

Brethlosze

Posted 2017-02-24T21:35:13.127

Reputation: 197

1Thank you, I like this! The page you link to was first created on 13 Sep 2018, it was about time! – simlev – 2019-01-23T17:57:02.253

0

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/Cygwin-OpenSSH

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 Vista/Server 2008 or newer and Windows PowerShell 2.0 or later.

Bill_Stewart

Posted 2017-02-24T21:35:13.127

Reputation: 862