This is a mirror of official site: http://jasper-net.blogspot.com/

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

ssh_tunnel1.png

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

$ ssh -L 5432:192.168.0.2: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:192.168.0.2:5432 <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

db_bame=#

Read more: easytech blog
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://blog.easytech.com.ar/2011/07/11/ssh-tips/

Posted via email from Jasper-net

0 comments: