A SSH tunnel consists of an encrypted tunnel created through a SSH protocol
connection. A SSH tunnel can be used to transfer unencrypted traffic over a
network through an encrypted channel. For example we can use a ssh tunnel to
securely transfer files between a FTP server and a client even though the FTP
protocol itself is not encrypted. SSH tunnels also provide a means to bypass firewalls that prohibits or filter certain
internet services. For example an organization will block certain sites using their proxy filter. But users may not wish
to have their web traffic monitored or blocked by the organization proxy filter. If users can connect to
an external SSH server, they can create a SSH tunnel to forward a given port on
their local machine to port 80 on remote web-server via the external SSH
server. I will describe this scenario in detail in a little while.
To set up a SSH tunnel a given port of one machine needs to be forwarded (of
which I am going to talk about in a little while) to a port in the other
machine which will be the other end of the tunnel. Once the SSH tunnel has been
established, the user can connect to earlier specified port at first machine to
access the network service.
Firs install these packages:
apt install openssh openssh-server
ssh -L 9001:yahoo.com:80 home -N
The ‘L’ switch indicates that a local port forward is need to be created. The switch syntax is as follows.
-L < local-port-to-listen >:< remote-host >: < remote-port> -N
Now the SSH client at ‘work’ will connect to SSH server running at ‘home’ (usually running at port 22) binding port 9001 of
‘work’ to listen for local requests thus creating a SSH tunnel between ‘home’ and ’work’. At the ‘home’ end it will create a
connection to ‘yahoo.com’ at port 80. So ‘work’ doesn’t need to know how to connect to yahoo.com. Only ‘home’ needs to worry
about that. The channel between ‘work’ and ‘home’ will be encrypted while the connection between ‘home’ and ‘yahoo.com’ will
be unencrypted.
ssh -L <local-port-to-listen>:<remote-host>:<remote-port> <gateway> -N
ssh -L <local-port-to-listen>:<block-host>:<block-port> <Remote-shell-server> -N
ssh -L 9001:yahoo.com:80 user@userme -N
is also possible to specify a port in the ‘home’ computer itself instead of
connecting to an external host. This is useful if I were to set up a VNC session
between ‘work’ and ‘home’. Then the command line would be as follows.
ssh -L 5900:localhost:5900 home -N (Executed from 'work')
The localhost in the above example is not the 'work' but the 'home' ,so this will make a connection
to port 5900 of the ‘home’ computer where the VNC client would be listening in.
ssh -L 9001:banned:22 home -N
Now we need to create a SSH session to local port 9001 from where the session
will get tunneled to ‘banned’ via ‘home’ computer.
ssh -p 9001 localhost -N
ssh -R 9001:intra-site.com:80 home -N(Executed from 'work')
Once executed the SSH client at ‘work’ will connect to SSH server running at home creating a SSH channel. Then the server
will bind port 9001 on ‘home’ machine to listen for incoming requests which would subsequently be routed through the created
SSH channel between ‘home’ and ‘work’. Now it’s possible to browse the internal site by visiting
ssh -g -ND 9001 home (Executed from 'work')
Here SSH will create a SOCKS proxy listening in for connections at local port
9001 and upon receiving a request would route the traffic via SSH channel
created between ‘work’ and ‘home’. For this it is required to configure the
browser to point to the SOCKS proxy (Not the http proxy) at port 9001 at localhost.
ssh -o ProxyCommand="nc -X 5 -x localhost:9150 %h %p" server.example.org
Reference of this article
http://chamibuddhika.wordpress.com/2012/03/21/ssh-tunnelling-explained/.