Using rsync between Linux and Windows

Linux Systems administrators have long enjoyed the use or rsync to keep files and directories synchronized on one a single system or between systems.  The ability to only transfer files that are new or have been updated since the last time rsync was run, make it a powerful tool. Administrators who are concerned with bandwidth and the time taken to synchronize the files have learned to appreciate rsync’s abilities.  Although there were many tools for backing the Windows system up, most must be run using the Windows Task Scheduler and do not take advantage of the Linux shell environment.

These instructions tell you how to install an ssh server and rsync on the system running Windows. This will give you encrypted connections between Windows and Linux and a Linux shell to run commands on the Windows system.  The procedure covers setting up ssh keys that so you can use a 2048 bit key instead of a password for authentication.  It also provides a script you can use to invoke rsync to automatically transfer/update files between the computers. The rsync command can be used to synchronize files on either system and be run from either the Linux or Windows system.

I searched for a long time and found all sorts of “Free for personal use” ssh servers for Windows that gave a connection to the Windows command prompt (cmd.exe) or bash shell.  However none of them were designed allow control from Linux to execute commands on the Windows system.  And although they included a shell in most cases none included rsync and many were tough to configure.

The only open source (truly free) application that would let me do this was Cygwin.

Overview of the setup

  • Download and install cygwin on the system running Windows.
  • Creating ssh keys for password-less authentication.
  • Enabling the openssh server in cygwin.

Installation of cygwin

On the system running Windows, go to the cygwin website and click on the link for the 32bit or 64 bit setup.exe program.

Run the program and select the following choices pressing the Next button after each choice.

  1. Install from Internet
  2. Accept the Root Directory of C:\cygwin (or C:\cygwin64 for 64 bit) and Install For All Users. Default Text File Type is Unix / binary.
  3. Accept the default Local Package Directory, which is the Windows desktop of the notebook PC. This will enable you to easily put a cygwin icon on the Windows desktop.
  4. Select Direct Connection if directly connected to the Internet or connected through a transparent router. Or put in your proxy server information.
  5. Pick one of the Available Download Sites. The sites change frequently but I normally pick one of the top three.   When you press next the script will download the list of current packages / programs, that are available.  It has selected for installation the default  set of packages. 
  6. Here you will see the categories available and you will need to add a couple of packages to the default Cygwin Setup.  Click on the plus sign next to the category Net.
  7. Scroll down to openssh, openssl and rsync select them to be installed.  
  8. Next click on Shells, and select bash if it is not already selected.
  9. Press the Next button at the lower right corner of the screen. The setup and installation of Cygwin will begin.
  10. Setup is complete. Check Create icon on Desktop, then press finish. The cygwin icon will appear on your desktop.

Generate an ssh keypair

Next you will create a pair of computer cryptographic “keys” that permit the Linux System to login and download the server’s files without entering a password.

Start cygwin by double clicking on the cygwin icon on the notebook PC’s desktop. A command window similar to the DOS command window will open.

You will now generate your ssh keypair using this command:

ssh-keygen -t rsa -b 2048

The -t rsa option specifies that the key type generated will be RSA protocol. As of version 7.0, OpenSSH no longer supports DSA keys by default, due to DSA keys being shown to be relatively weak compared to other options. It is recommended that you use rsa, ecdsa or ed25519 keys instead, which can be generated similarly to DSA keys. The -b 2048 option specifies a key length of 2048 bits, which is considered to be very secure. When generating the keys you will be prompted for a passphrase. Press Enter without typing in a pass phrase so that you won’t be prompted for the pass phrase every time you use the key (such as during the file downloading procedure).

To configure SSH and test your Cygwin setup, follow these steps:

After you install Cygwin, navigate using Windows Explorer to the C:\cygwin (C:\cygwin64) directory, open the Cygwin.bat file in edit mode using any editor, and add the following line before invoking the bash shell.

set CYGWIN=binmode ntsec

For example, here are the contents for the Cygwin.bat file after adding the above line:

@echo off 
C:\cygwin\binset CYGWIN=binmode ntsec 
bash --login -i

To verify if Cygwin (cygrunsrv) is installed properly, run C:\cygwin\Cygwin.bat, and execute the following command:

cygrunsrv -h

If Cygwin is installed properly, then all the Cygwin help options are displayed on the screen. However, if this command returns an error message, then you may have to reinstall Cygwin.

To configure the SSHD service, run C:\cygwin\Cygwin.bat, and execute the following command:


After running the command, you are prompted the following questions:

*** Query: Should privilege separation be used? <yes/no>: yes
*** Query: New local account 'sshd'? <yes/no>: yes
*** Query: Do you want to install sshd as a service?
*** Query: <Say "no" if it is already installed as a service> <yes/no>: yes
*** Query: Enter the value of CYGWIN for the deamon: [] binmode ntsec
*** Query: Do you want to use a different name? (yes/no) yes/no

At this point, if you want to use the same name, that is cyg_server, enter no. You are then prompted the following questions:

*** Query: Create new privileged user account 'cyg_server'? (yes/no) yes
*** Query: Please enter the password:
*** Query: Renter:

However, if you want to use a different name, enter yes. You are then prompted the following questions:

*** Query: Enter the new user name: cyg_server1
*** Query: Reenter: cyg_server1
*** Query: Create new privileged user account 'cyg_server1'? (yes/no) yes
*** Query: Please enter the password:
*** Query: Reenter:

If the configuration is successful, you will see the following message:

Host configuration finished. Have fun!

Open port 22 on your Windows system firewall.

I recommend rebooting the windows system and then test your ssh connection and rsync script.

Backup the c:\cygwin\etc\passwd file and then use any editor to open the file in edit mode. Remove only those entries of the user that you will use to connect to the host on which you want to install a Management Agent. Ask the user to make a backup of the c:\cygwin\etc\passwd file before editing.

  • If the user that you are employing to connect to the host on which you want to install the Management Agent is a local user, run C:\cygwin\Cygwin.bat and execute the following:

/bin/mkpasswd -l –u <USER> >> /etc/passwd (for example, /bin/mkpasswd -l -u pjohn >> /etc/passwd)

  • If the user you are employing to connect to the host on which you want to install the Management Agent running is a domain user, run C:\cygwin\Cygwin.bat and execute the following:

/bin/mkpasswd -d -u <USER> >> /etc/passwd (for example, /bin/mkpasswd -d -u pjohn >> /etc/passwd)mkdir -p /home/<USER>  (for example, mkdir -p /home/pjohn)chown <USER> /home/<USER> (for example, chown pjohn /home/pjohn)


Create a script to run rsync with ssh keys

if [ $# != 2 ]
echo "    Usage: [path to sync] [path to send to]"
exit 0
rsync -avzhe "ssh -i ~/.ssh/id_rsa" [Windows User]@[IP Address]:$1 $2

For testing add the -n flag to test but not sync.  For example:

rsync -avzhen “ssh -i ~/.ssh/id_rsa” Rodney@$1 $2