How to create SSH tunnel on Windows by using AutoSSH


How to create SSH tunnel on Windows by using AutoSSH

Step 1: download Cygwin
Step 2: Install Cygwin
Step 3: Open Cygwin terminal

Follow step 1,2,3 in this tutorial: How to install SSH daemon on Windows

Step 4: Generate a public/private key pair

Use this commandline:

ssh-keygen

Use empty passphrase:

ssh-keygen

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/Administrator/.ssh/id_rsa):
Created directory '/home/Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/Administrator/.ssh/id_rsa.
Your public key has been saved in /home/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YsEPYiYtiTkN+jbNkRvK6mc29EQVFD9Qbp89euZmpS9 Administrator@MRNC-N9NJM89QFO
The key's randomart image is:
+---[RSA 2048]----+
|.     .=+.       |
|.= o o .+        |
|= = O =  =       |
| + O * +. o o    |
|  * = o S  o o   |
| o o o .    . .. |
|. . o      . oo  |
|.  = .      +E   |
| .+ .       o.o. |
+----[SHA256]-----+

Step 5: Copy your newly-created public key to the SSH server

scp .ssh/id_rsa.pub user@ssh.host.name:id_rsa.pub

Example

scp .ssh/id_rsa.pub root@tutorialspots.com:id_rsa.pub

Result:

$ scp .ssh/id_rsa.pub root@tutorialspots.com:id_rsa.pub
root@tutorialspots.com's password:
id_rsa.pub                                    100%  411     0.4KB/s   00:00

Step 6: Login SSH server

ssh root@tutorialspots.com
mkdir .ssh
cat id_rsa.pub >> .ssh/authorized_keys

Result:

$ ssh root@tutorialspots.com
root@tutorialspots.com's password:
Last login: Sat May  7 08:05:24 2016 from xx.xx.xx.xx
[root@tutorialspots ~]# mkdir .ssh
[root@tutorialspots ~]# cat id_rsa.pub >> .ssh/authorized_keys

Step 7: Test key

In the previous step, we create a key in the SSH server, now we test it.

7.1 Use command line: exit to logout SSH server

7.2 Login again

ssh root@tutorialspots.com

If you see the result: (don’t require type password)

$ ssh root@tutorialspots.com
Last login: Sat May 21 07:32:29 2016 from xx.xx.xx.xx
[root@tutorialspots ~]#

It means these above steps are successful. Otherwise, they are false.

7.3 Do like step 7.1

Step 8: Install autossh as a Windows service

In the Cygwin terminal, use this commandline:

cygrunsrv -I AutoSSH -p /path/to/autossh -a "-M 20000 -L localaddress:port:serveraddress:port user@ssh.host.name" -e AUTOSSH_NTSERVICE=yes

Example:

cygrunsrv -I AutoSSH -p /bin/autossh -a "-M 20000 -L 127.0.0.1:1081:68.235.32.101:22 root@tutorialspots.com" -e AUTOSSH_NTSERVICE=yes

Step 9: Tweak Windows service settings

9.1: Run: services.msc
9.2: Properties

autossh service properties

9.3 Go to tab Log On, then choose your current user

autossh service properties user

9.4: Start this service

Step 10: test tunnel

Example: Read the next tutorial: How to connect Mysql server through SSH tunnel using AutoSSH

More:

Get status from windows server

cygrunsrv --query AutoSSH

Result:

$ cygrunsrv --query AutoSSH
Service             : AutoSSH
Current State       : Stopped
Command             : /bin/autossh -M 20000 -L 127.0.0.1:1081:68.235.32.101:22 root@tutorialspots.com

Remove windows service

cygrunsrv --remove AutoSSH

1 Comment

Leave a Reply