Wednesday, February 3, 2010

Creating an ssh Tunnel

This is just what I've done to be able to access a remote database (postgresql in this case) via an ssh tunnel. For a database hosted on the machine I was logging into:

$ ssh -L 63333:localhost:5432 andrew@someserver.com

-L is the port forwarding option for the ssh command. The first port number, 63333, is the local port being bound. The next two values, localhost:5432, are the host and port being bound on the other side of the tunnel, relative to the machine your logging in to. 'andrew@someserver.com' is the normal ssh login username and host.

Now what if the database is hosted on a different machine than the one you're logging into? Just replace 'localhost:5432' with the address and port relative to the machine you've logged into. For example:

$ ssh -L 63333:192.168.1.40:5432 andrew@someserver.com

This command will create a tunnel from port 63333 on my local machine to 192.168.1.40:5432, relative to someserver.com.

After entering one of the above commands, you can now connect to the remote servers by connecting to port 63333 on your local machine and the connection will be protected by ssh.

No comments:

Post a Comment