SSH tunneling allows to send and receive traffic using a dedicated port. Using an ssh traffic can have many reasons one most common usage reason is to protect the traffic from a host to a remote server or to access port numbers which are by other means blocked by firewall, e.g.: (get around firewall filtering)
SSH tunneling works only with TCP traffic. The way to make ssh tunnel is with cmds:
host:/root# ssh -L localhost:deshost:destport username@remote-server.net
host:/root# ssh -R restport:desthost:localport username@remote-server.net
host:/root# ssh -X username@remote-server.net
This command will make ssh to bind a port on localhost of the host host:/root# machine to the host desthost:destport (destination host : destinationport). Important to say deshost is the host destination visible from the remote-server.net therefore if the connection is originating from remote-server.net this means desthost will be localhost.
Mutiple ssh tunnels to multiple ports using the above example commands is possible. Here is one example of ssh tunneling
Let’s say its necessery to access an FTP port (21) and an http port (80), listening on remote-server.net In that case desthost will be localhost , we can use locally the port (8080) insetad of 80, so it will be no necessery to make the ssh tunnel with root (admin privileges). After the ssh session gets opened both services will be accessible on the local ports.
host:/home/user$ ssh -L 21:localhost:21 -L 8080:localhost:80 user@remote-server.net
That’s all enjoy 😉
More helpful Articles
Tags: command, connection, deshost, destination host, destport, example, firewall, Ftp, ftp port, home, host, host host, host root, How to, Important, Linux, localhost, machine, make, Mutiple, necessery, net, nethost, netThat, port, port 8080, port numbers, ports, Privileges, reason, remote server, restport, root, root admin, server, ssh, ssh session, ssh tunneling, traffic, tunnels, username, way
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
A useful tunneling is to create SSH tunnel to MySQL server on localhost so you can access it via mysql cli using some port lets say (3308):
Then access with mysql cli (assuming mysql cli is installed on localhost):
$ mysql -P 3308 -u USERNAME -pPASSWORD DATABASE
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
In corporate world it is also very useful to create and use SSH tunnel to Oracle Database. The same logic is in place:
C:\Users\georgi>sqlplus mdinh/mdinh@127.0.0.1:1521/lax_db01 SQL*Plus: Release 11.2.0.1.0 Production on Mon Mar 11 00:00:14 2013 Copyright (c) 1982, 2010, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> select instance_name from v$instance; INSTANCE_NAME ---------------- db01 SQL> select db_unique_name from v$database; DB_UNIQUE_NAME ------------------------------ lax_db01 SQL> exit
View CommentView CommentMozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Another useful scenario is whether it is necessery to make ssh tunnel via multiple (server) hops:
There are 3 scenarios to tunnel ssh traffic via multiple servers:
Tunnel from
localhost
tohost1
:As noted above, the connection from
host1
tohost2
will not be secured.Tunnel from
localhost
tohost1
and fromhost1
tohost2
:This will open a tunnel from
localhost
tohost1
and another tunnel fromhost1
tohost2
. However the port9999
tohost2:1234
can be used by anyone onhost1
. This may or may not be a problem.Tunnel from
localhost
tohost1
and fromlocalhost
tohost2
:This will open a tunnel from
localhost
tohost1
through which the SSH service onhost2
can be used. Then a second tunnel is opened fromlocalhost
tohost2
through the first tunnel.Normally, I'd go with option 1.
If the connection from
host1
tohost2
needs to be secured, go with option 2.Option 3 is mainly useful to access a service on
View CommentView Commenthost2
that is only reachable fromhost2
itself.