Secure SSH Tunneling – at no extra cost

Assuming you have a Windows machine and you interact with Linux boxen at any stage, chances are high that you have used and interacted with PuTTY at one stage or another. That beautiful, less than 2 sec, 444K download of a tool (currently at version 0.60 beta) allows you to SSH, COM-direct, RSH, Telnet etc from the desktop. Both examples below relate to MySQL port tunneling.

SSH Tunnels using the Bash command line

Running on a proper machine (or even a Windows with Cygwin or a Mac Terminal) allows you to quickly tunnel a session to a remote server:

ssh -N -L 3307:127.0.0.1:3306 userName@remoteHost &

This simple one-liner maps your remote server’s locally-bound MySQL server from 3306 on the remote server to your local machine on port 3307, so you can connect via your MySQL client on the local machine to 127.0.0.1 on port 3307 and hey presto, you’re interacting with the remote server.

PuTTY on Windows SSH Tunnels

SSH Tunnel via PuTTY

SSH Tunnel via PuTTY

In the WWW (Wonderful World of Windows?), PuTTY comes to the rescue.

All you need to do is to create a session in PuTTY. Select the Tunnels tab in the SSH section on the left-hand-side.

In the Source port text box enter the local port to connect to on the local machine – in this case 3307. This is the port PuTTY will listen on on your local machine (localhost – 127.0.0.1). In the Destination field immediately below the Source port enter 127.0.0.1:3306. This means, from the server, forward the connection to IP 127.0.0.1 port 3307.

Want the full run-down, including the command-line examples (and examples using plink), have a look at PuTTY’s documentation on this.

Therefore, if you’ve saved a session named RemoteSavedPuTTYSession, using PuTTY, you can do something like:

putty -L 3307:remoteHost:3306 -load RemoteSavedPuTTYSession

But you knew that already!

Simple, eh?

Already gave an example relating to this for RDP redirection via SSH tunnel some time ago.