- Download Cygwin (http://www.cygwin.com/)
- Install Cygwin, selecting the autossh package.
- Start the Cygwin shell (Start -> Programs -> Cygwin).
- Generate a public/private key pair.
- At the command line, run: ssh-keygen
- Accept the default file locations
- Use an empty passphrase
- Copy your newly-created public key to the SSH server.
- scp .ssh/id_rsa.pub user@ssh.host.name:id_rsa.pub
- Add your public key to your list of authorized keys on the server.
- Login to your SSH server.
- mkdir .ssh
- cat id_rsa.pub >> .ssh/authorized_keys
- Test your key.
- Logout of your SSH sever.
- 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.
- Logout of your SSH server.
- Exit of the Cygwin shell.
- Install autossh as a Windows service.
- Now back in Windows, open a new command Window (Start -> Run -> cmd).
- cd C:\cygwin\bin
- cygrunsrv -I AutoSSH -p /usr/bin/autossh -a “-M 20000 -L localaddress:port:serveraddress:port user@ssh.host.name” -e AUTOSSH_NTSERVICE=yes
- Tweak Windows service settings.
- Open the Services management console (Administrative Tools -> Services).
- Edit the properties of the AutoSSH service.
- In the “Log On” tab, select the “This account” radio button and set the service to run as your current user.
- Start the service.
- Test your tunnels.
- Consider making a scheduled task to start the service every hour or so, in case autossh goes boom.
16 Comments
Nice and useful manual, thank you. Especially for those who often use many proxies.
cygwinsrv not present in bin folder. Do we need to add something
sorry cygrunsrv not present in bin folder
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.
Just found a new tool for keeping SSH tunnels connected under Windows. Works as Windows service and restart tunnels if needed:
https://persistentssh.com
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!
This thing rocks. Thank you for posting this.
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
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
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
Have you completed steps 9 through 12? Is your service running? Is autossh running? Check your services.msc and Task Manager
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
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.
what does -M signify ? Please tell
It’s the monitor port, used to open a port and send pings to verify the connection is up. It’s in the manual.
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?