Setting Up an SSH Tunnel from Windows
Sunday, 20 July 2008 00:00
This is a quick guide on how to set up an SSH tunnel from Windows to a Linux server. I needed to run through these steps because I wanted to be able to connect to a MySQL Server running on an Ubuntu box. However, for security reasons, I didn't want to open up the MySQL port on my server. Luckily, setting up an SSH Tunnel was way easier to do than I thought it would be.

This tutorial assumes that:
  • You have an Ubuntu server already running with MySQL
  • You have downloaded the PuTTY SSH client to your local Windows machine
  • You have the MySQL tools installed on your local Windows machine

 

Step 1: Configure PuTTY
On the Windows machine, open up PuTTY and navigate to Connection... SSH... Tunnels. In the Source Port field, enter the port you'd like to use on your Windows desktop. (The SSH Tunnel works similar to how a proxy server would work.) For this example, I'll use 3306, which is the default MySQL port.

In the Destination field, add the IP address and port number to connect to your MySQL server. The trick is that the address should be written as if you were logged into your Linux server. So in my example, I would use 127.0.0.1:3306, since I am connecting directly to the server that is hosting MySQL.

Click the Add button to add this to the list of forwarded ports. If there are multiple ports you want to tunnel, you can do that. Be sure to save your PuTTY session to make it easy to reload this configuration.

Step 2: Connect to the SSH Server
Click on the Open button in PuTTY to launch the connection. Log into your SSH server as you normally would. Leave the terminal window open.

At this point, your tunnel is ready to go. In our example, we're using MySQL, but you can now open any software application and use the tunnel by connecting to the localhost with the port you specified in the Source Port.

For example, run the MySQL Query Browser. Connect to localhost using the port 3306. You'll notice that you're able to connect to the server and everything works as if you were connecting directly to the server port.