SSH Tunnel
Introduction
Tunneling (from the English tunnelling - "laying a tunnel") in computer networks is a process during which a logical connection is created between two endpoints by encapsulating various protocols.
Tunneling is a method of building networks in which one network protocol is encapsulated in another.
Tunneling differs from conventional multi-layer network models (such as OSI or TCP/IP) in that the encapsulated protocol belongs to the same or a lower layer than the one used as the tunnel.
The essence of tunneling is to "pack" the transmitted portion of data, together with its service fields, into the payload area of a packet of the carrier protocol. Tunneling can be applied at the network and at the application layer. The combination of tunneling and encryption makes it possible to implement closed virtual private networks (VPN). Tunneling is usually used to reconcile transport protocols or to create a secure connection between network nodes.
Example
A tunnel can be created with the following command. Replace the addresses and ports with the ones you need. Details in the next paragraph .
ssh -L 9119:192.168.0.2:9200 username@192.168.0.2
Create an SSH tunnel
Tunnels are usually created to redirect traffic. An SSH tunnel is the same thing as SSH port forwarding.
Suppose you want to direct traffic from your localhost (127.0.0.1) to a
remote
host 192.168.0.2.
On the
remote
host you have a user named andrei and you know his password.
First you need to decide on the ports on the local host and on the remote one.
Suppose you chose 9119 for the local and
9200 for the
remote
hosts.
That is, you want everything that goes to localhost:9119 to be redirected to
192.168.0.2:9200
Run
ssh -L localhost:9119:192.168.0.2:9200 andrei@192.168.0.2
localhost: need not be specified explicitly. It is implied by default, but you can keep it to make it easier to remember.
ssh -L 9119:192.168.0.2:9200 andrei@192.168.0.2
The authenticity of host '192.168.0.2 (192.168.0.2)' can't be established.
ECDA …
…
andrei@192.168.0.2's password:
Last login: Sun Jan 31 13:23:00 2021
Check the IP by running
ip a
If you need a tunnel with graphical interface support X Window System use the -X flag
ssh -X -L 9119:192.168.0.2:9200 andrei@192.168.0.2
Demonstration
The tunnel is created, do not close the terminal. Open two new terminals or two new tabs in the old one.
You have two empty terminals, both on the local host. Let us call them 1 and 2.
From terminal 2 we will connect to the remote host 192.168.0.2 over ssh and listen on port 9200 with ncat
ssh andrei@192.168.0.2
ncat -l 9200
On terminal 1, with ncat, we will send a message to port 9119 of the local host. This is the port we configured for forwarding.
ncat localhost 9119
Let us enter any text
heihei.ru
Open terminal 2, the same text should appear there
heihei.ru
List of all open SSH tunnels
To get a list of the tunnels opened specifically by ssh, run
sudo lsof -i -n | egrep '\<ssh\>'
ssh 14695 andrei 3u IPv4 230080 0t0 TCP 192.168.0.1:46356->192.168.0.2:ssh (ESTABLISHED) ssh 14695 andrei 4u IPv6 230103 0t0 TCP [::1]:9119 (LISTEN) ssh 14695 andrei 5u IPv4 230104 0t0 TCP 127.0.0.1:9119 (LISTEN)
The output of this command is for some reason different under practically identical conditions.
Below is an example of the second variant. If you know why this happens - write in the comments.
ssh 15151 andrei 3u IPv4 239364 0t0 TCP 192.168.0.1:46464->192.168.0.2:ssh (ESTABLISHED) ssh 15151 andrei 4u IPv6 239380 0t0 TCP [::1]:mxit (LISTEN) ssh 15151 andrei 5u IPv4 239381 0t0 TCP 127.0.0.1:mxit (LISTEN) ssh 15151 andrei 9u IPv6 239428 0t0 TCP [::1]:mxit->[::1]:54306 (ESTABLISHED)
Close all SSH tunnels
To close everything related to ssh you can run
sudo killall ssh sshd
This method is good only if you work alone, for example on some virtual machine.
Article author: Andrei Olegovich