Creating persistent SSH tunnels in Windows using autossh

  1. Download Cygwin (http://www.cygwin.com/)
  2. Install Cygwin, selecting the autossh package.
  3. Start the Cygwin shell (Start -> Programs -> Cygwin).
  4. Generate a public/private key pair.
    1. At the command line, run: ssh-keygen
    2. Accept the default file locations
    3. Use an empty passphrase
  5. Copy your newly-created public key to the SSH server.
    1. scp .ssh/id_rsa.pub user@ssh.host.name:id_rsa.pub
  6. Add your public key to your list of authorized keys on the server.
    1. Login to your SSH server.
    2. mkdir .ssh
    3. cat id_rsa.pub >> .ssh/authorized_keys
  7. Test your key.
    1. Logout of your SSH sever.
    2. Login to your SSH server again. This time, your key will be used for authentication and you won’t be challenged for your login credentials. If you are not logged in automatically, review the previous steps. Or contact your server administrator.
    3. Logout of your SSH server.
    4. Exit of the Cygwin shell.
  8. Install autossh as a Windows service.
    1. Now back in Windows, open a new command Window (Start -> Run -> cmd).
    2. cd C:\cygwin\bin
    3. cygrunsrv -I AutoSSH -p /usr/bin/autossh -a “-M 20000 -L localaddress:port:serveraddress:port user@ssh.host.name” -e AUTOSSH_NTSERVICE=yes
  9. Tweak Windows service settings.
    1. Open the Services management console (Administrative Tools -> Services).
    2. Edit the properties of the AutoSSH service.
    3. In the “Log On” tab, select the “This account” radio button and set the service to run as your current user.
    4. Start the service.
  10. Test your tunnels.
  11. Consider making a scheduled task to start the service every hour or so, in case autossh goes boom.

16 Comments

  • sysadmin says:

    Nice and useful manual, thank you. Especially for those who often use many proxies.

  • g balaji says:

    cygwinsrv not present in bin folder. Do we need to add something

    • g balaji says:

      sorry cygrunsrv not present in bin folder

      • bolt says:

        It’s included in the base install of cygwin, unless you removed something. It’s called cygrunsrv.exe, and should be in the “bin” folder under your Cygwin installation on Windows.

  • Basil Kruglov says:

    Just found a new tool for keeping SSH tunnels connected under Windows. Works as Windows service and restart tunnels if needed:
    https://persistentssh.com

    • Scott says:

      Sorry for pulling this thread out of the tomb, but i wanted to thank you for posting this. It is exactly what i was looking for! It is a very lightweight program that does it’s job well!

    • Pablo Destefanis says:

      This thing rocks. Thank you for posting this.

  • alex says:

    step 8.3 not sure why system is not taken the command
    cygrunsrv -I AutoSSH -p /usr/bin/autossh -a “-M 20000 -L localaddress:port:serveraddress:port user@ssh.host.name” -e AUTOSSH_NTSERVICE=yes

    i get error exactly one of –install –update

    thanks

  • alex says:

    bolt: i was able to get by the error by removing the ” and replaceing with ‘ but now i am able able to ssh into remote server with no password witch is good ( manualy ) but its does not go automaticly like i want it to via the windows service ( my code example )

    cygrunsrv -I AutoSSH -p /usr/bin/autossh -a ‘-M 20000 -L localhost:2222:206.xx.xx.xx:22 root@206.xx.xx.xx‘ -e AUTOSSH_NTSERVICE=yes

    thanks

  • alex says:

    good day

    any idea why my code does not stay connected via the windows service

    cygrunsrv -I AutoSSH -p /usr/bin/autossh -a ‘-M 20000 -L localhost:2222:206.xx.xx.xx:22 root@206.xx.xx.xx‘ -e AUTOSSH_NTSERVICE=yes

    thanks for your help in advance

    • bolt says:

      Have you completed steps 9 through 12? Is your service running? Is autossh running? Check your services.msc and Task Manager

  • alex says:

    yes the service is running and i have completed all steps but the session is not connected via the window service meaning the service is not connected to the ssh server

    if i do a manual ssh connect it works but that would be a manual session not a automatic via the window service

    is my code right on the a above repl

    thanks again for your time

    • bolt says:

      If the service is running, you should see autossh as a process in Task Manager. You should also see an ssh process.
      Make sure whatever port you chose to forward is actually available, and make sure both autossh and the ssh process are running as your user.
      If all of that fails, see the sections about log output for cygrunsrv at http://web.mit.edu/cygwin/cygwin_v1.3.2/usr/doc/Cygwin/cygrunsrv.README
      The logs should tell you why autossh fails to connect.
      I just tested this on a Windows 10 machine, and it performed as expected.

  • Bolt says:

    what does -M signify ? Please tell

    • bolt says:

      It’s the monitor port, used to open a port and send pings to verify the connection is up. It’s in the manual.

  • Must says:

    When I try to autossh from cigwin, it works
    but when I start the service in Windows, it terminates with error message
    “The AutoSSH service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs”
    Any ideas?

Leave a Reply to sysadmin Cancel reply

Your email address will not be published. Required fields are marked *