This is a mirror of official site:

SSH tunnel and a half

| Wednesday, July 13, 2011
As you all might know ssh is a very popular protocol used to connect to remote shells. I mostly use OpenSSH as a client (I think most of you to). SSH has a lot of cool options but in this post i’m going to write about how to use it to create a secure tunnel.

What is a tunnel?

A tunnel specifies a given port on the local (client) host that is to be forwarded to the given host and port on the remote side. How it works? This works by allocating a socket to listen to a port on the local side, optionally bound to the specified bind_address.  Whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to the port from the remote machine.

Making the tunnel

Before type the command line to create the ssh tunnel I’m assuming the following topology


If I would like to connect to db server and the database is a postgresql, then the default port is 5432 and the tunnel command line is:

$ ssh -L 5432: <user>@<web server>
     _____^______     ______^______
    / local port \   / remote port \

If you are running a postgresql in your own machine you can chose another port to listen in your loop back. I always have a pgsql running in my laptop because I use it in most of my developments and I use port 5435.

$ ssh -L 5435: <user>@<web server>

 from other terminal

$ psql -h localhost -p 5435 -U db_user -d db_name
Welcome to psql 7.3.4, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
 \h for help with SQL commands
 \? for help on internal slash commands
 \g or terminate with semicolon to execute query
 \q to quit


Read more: easytech blog

Posted via email from Jasper-net