How To Easily Create SSH Tunnels And Why You Should

What Is An SSH Tunnel

SSH tunneling is a method to create a secure connection between a local computer and a remote server over SSH. It is a kind of Port forwarding via SSH.

Since the traffic is encrypted it is quite safe to connect with a restricted server to manage a database or use any other services you need without opening a port on the firewall.

Read more details here https://www.ssh.com/ssh/tunneling/

Gnome SSH Tunnel Manager (gSTM)

gSTM (Gnome SSH Tunnel Manager) is a nice software with which you can easily create SSH tunnels.

gSTM (Gnome SSH Tunnel Manager) is not included in latest Ubuntu repos (available until Xenial 16.04).

However, according to Launchpad, there is an unsupported and untrusted PPA here.

SSH – The recommended way

Let’s try to create a tunnel with an example. It is easier than you think. We want to connect to standard PostgreSQL port 5432 on a remote server. This is not permitted by the firewall.

So we will create an SSH tunnel from the local 5433 port (or any other). We will then use our preferred software to connect to this local port.

Open the tunnel

To open a tunnel to port 5432 to remote_server from port 5433 in localhost:

ssh -fNg -L 5433:localhost:5432 username@remote_server_ip

Quick reference (man ssh or read here for details):

fRequests ssh to go to background just before command execution.
NDo not execute a remote command. This is useful for just forwarding ports (protocol version 2 only).
gAllows remote hosts to connect to local forwarded ports. If used on a multiplexed connection, then this option must be specified on the master process.
L[bind_address:] port:host:hostport
Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

To close the tunnel

ps ax |grep 5433
kill process_id

Windows machines

In Windows machines, you can create SSH tunnels with the excellent PuTTY software. Here is a nice tutorial.

Featured Videos