Secure Filesharing - Setting up SSH/SFTP on Cygwin+Windows

Windows users have a bit of a problem trying to remotely access files on their machines, and even worse if you actually do work on the command line or Cygwin but are stifled by the lack of *NIX-like remote accessibility. I have used IIS and other FTP servers in the past, and even an HTTP server, to expose files I needed remotely, but I've found the method to be too cumbersome, inflexible, and worst of all, insecure. FTP sends passwords in clear text over the Internet, and sharing large amounts of data via FTP is a hair-raising exercise in managing parallel principals and security settings.

Welcome to SSH/SFTP. SFTP is a "SSHed" version of the ubiquitous SFTP. SSH provides the equivalent of Telnet capabilities, only that it transmits everything over the network in an encrypted form. SFTP uses the encryption capabilities of SSH to allow secure file transfer also. Unfortunately no command-line SSH/SFTP client is included in Windows, but you can use SSH Secure Shell (GUI and command line) or Putty (command line). Linux comes with the clients. You'll need to install Cygwin, a *NIX emulator for Windows, and not only is the installation simple, IMHO you'll some day appreciate the flexibility of a hybrid Windows/*NIX environment.

This is not a Cygwin installation tutorial, so I'll keep that part short.

  • Stage 1: Cygwin Installation
    • Install Cygwin from www.cygwin.com
    • The following two packages must be installed:
      • cygrunsrv: allows cygwin to run services under Win32.
      • openssh: the ssh packages used for encryption.
    • Open the Cygwin command prompt.
  • Stage 2: User Setup
    • Windows workstations are likely to be on a domain, whereas home users are not. You can either your domain or your local userID to the list of users authorized to log into this SSH server.
    • For domain users:
      • Log in with as a domain user.
      • In the Cygwin window, type
        mkpasswd -d -u <username> >> /etc/passwd
    • For local users:
      • In the Cygwin window, type
        mkpasswd -l -u <username> >>/etc/passwd
    • mkpasswd retrieves the user information and password hash etc., d/l switches specifying whether locally or from a domain. The u switch picks the user you want to add (don't use domain\user syntax; just user is needed).
    • ">> /etc/passwd" adds the user info to the local passwd file, which acts as the authentication database.
  • Stage 3: Environment Setup
    • You must add an environment variable and edit another one.
    • Go to My Computer -> right click -> Properties -> Advanced (tab) -> Environment Variables
    • Use the buttons in the lower half of the screen, which deal with system variables (and not user variables).
    • Create a new variable named "CYGWIN". Give it the value "ntsec tty".
    • Edit the PATH variable. Add a semicolon to the end of the value string, and then add "C:\Cygwin\bin", replacing "C:\Cygwin" by the path where you installed Cygwin, if needed.
    • Close the dialog boxes by clicking on "Ok".
  • Stage 4: Installing and Starting SSH service
    • In the Cygwin window, type
      ssh-host-config -y
    • When asked for a terminal method, specify "ntsec tty".
    • Type
      cygrunsrv --install sshd
    • If you had failed in a previous attempt or are upgrading to a newer version of OpenSSH, you can try
      cygrunsrv --remove sshd
      and then repeat the install command. This worked for me when I had SSH working but then had to reinstall Cygwin; when I tried to reinstall, cygrunsrv gave me an error, saying that sshd already existed as a service. Removing and installing it worked.
    • Type
      cygrunsrv --start sshd
      to start the service.
    • Type
      ssh localhost
      to test the installation.
  • Miscellaneous
    • SFTP is authomatically installed with SSH.
    • To access different drives etc. in either SSH or SFTP, use the path /cygdrive/driveLetter/.
    • File permissions, whether you can upload or not, etc., works similarly to *NIX; if you have write permissions, you can upload.
    • I will investigate "anonymous" or guest usage issues and update this site.

[Home Page] [Portfolio Website] [Writings] [Family] [Photo Album] [Reading Material] [Resources] [My Blogs] [Mei Sententia] [Sites of Interest] [Search Site]

Last updated: 2348 hrs PST, Thursday, March 08, 2007