Setup rsync server over SSH on Windows Server 2012 easy way

  1. Configuring sshd from Windows server

There’re few options available such as Cygwin, WSL, MSYS2(Git for Windows) and Win32-OpenSSH. Personally I ‘thought’:

  • Cygwin: installing the Cygwin was relatively easy, but configuring the sshd wasn’t. Not providing uninstaller was also minus.
  • WSL: Looked promising, but it didn’t support background daemon.

Quote: https://blogs.windows.com/buildingapps/2017/08/08/windows-subsystem-linux-windows-server/

What this isn’t — WSL is not a Linux server

Just as with WSL on Windows Client, you can run daemons and jobs like MySQL, PostgreSQL, sshd, etc., via an interactive shell, but you cannot currently use WSL to run persistent Linux services, daemons, jobs, etc. as background tasks.

  • Win32-OpenSSH: It’s official way to use the sshd on Windows(It even recently started including OpenSSH from the OS additional features). Detailed instruction, (un)install Powershell scripts are also provided here https://github.com/PowerShell/Win32-OpenSSH/.

So I installed Win32-OpenSSH following by https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH page.

The server already installed the ‘Git for Windows(MSYS2)’ so that I made the bash.exe as a default shell of the sshd.

Name: DefaultShell

Value: C:\Program Files\Git\bin\bash.exe

Few more tweaks required,

  • I appended the client’s public key into the Windows Server(where rsync.exe server will be running)’s ~/.ssh/ authorized_keys
  • The server’s sshd_config file is located at ‘C:\ProgramData\ssh’ (unless you changed). ‘PubkeyAuthentication yes’ option is commented out by default. Let’s enable it.

So far, you should be able to ssh login to the Windows Server and see the MSYS2’s bash.exe shell as the result.

2. Installing rsync on the Server

I was struggling with this part at first because when I googled ‘rsync over ssh windows’ the results were outdated or insufficient. However, I found this and if you’re using MSYS2(Git for Windows), it’s extremely simple. Just grab the compiled binary from msys2 repo and paste it to the bin folder(C:\Program Files\Git\usr\bin).

Refs: https://serverfault.com/a/872557 & https://blog.tiger-workshop.com/add-rsync-to-git-bash-for-windows/

Then I was able to successfully ‘rsync’ing by:

rsync …… -e ssh -vvv /c/somedir/folderA user@pcname.xxx.xxx:/c/detination_folder

Last bonus:

3. Working it through Jenkins

Additional chores remained if you want it to work through Jenkins because Jenkins service process is running under the SYSTEM account (if you didn’t modify). For example, if someone tried to launch a job does Jenkins rsync over SSH, then they’ll see the following error:

[rsync test] $ C:\…\Git\bin\bash.exe -xe C:\Windows\TEMP\jenkins500000000000000003.sh

+ whoami

SYSTEM

+ rsync … -e ‘ssh -vvv -i /c/Users/xxxxx/.ssh/id_rsa’ /c/….

OpenSSH_7.5p1, OpenSSL 1.0.2k 16 Jan 2017

debug1: Reading configuration data /etc/ssh/ssh_config

: : :

: : :

debug3: hostkeys_foreach: reading file “/etc/ssh/ssh_known_hosts”

debug3: hostkeys_foreach: reading file “/etc/ssh/ssh_known_hosts”

debug1: read_passphrase: can’t open /dev/tty: No such device or address

Host key verification failed.

rsync: connection unexpectedly closed (0 bytes received so far) [sender]

rsync error: error in rsync protocol data stream (code 12) at io.c(226) [sender=3.1.3]

Build step ‘Execute shell’ marked build as failure

Finished: FAILURE

I believe this problem can be fixed by making the Jenkins to run under normal user account, but I approached bit differently by

  • Launching cmd.exe as the SYSTEM privilege using Sysinternals’ PsExec.exe

  • From that console, run bash.exe again and just login to the server once to create known_hosts for %SYSTEMPROFILE%.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s