JhGU's personal site

SSH Tunneling

Forward Tunneling

Suppose we have a machine with public IP and want to use it as a bridge to another machine (either with or without public IP), we can run the following command on the local machine:

ssh -p<port> -L <local port>:<destination address>:<remote port> -N <user>@<public address>

where

  • <port> is the ssh port of the bridge machine, i.e., the <public address>
  • <address> is the address of the bridge machine, usually a VPS
  • <local port> is the port opened on a local machine through which we can reach <destination address>:<remote port>
  • <destination address> is the address that we want to reach

Reverse Tunneling

Suppose we have a machine that is behind a firewall and wants to access it from outside. We will need a publically accessible machine. Then we can run the following command on the machine that is behind the firewall:

ssh -p<port> -N -R <port to open>:localhost:<local port> <user>@<public address>

where

  • is the ssh port of the <public address>
  • <public address> is the address of the publically accessible machine
  • <port to open> is the port to be opened on the public machine, which is then used to reach the machine that is behind the firewall
  • <local port> is the port (open ssh port) opened on the machine that is behind the firewall.

Note that GatewayPort yes should be set in /etc/ssh/sshd_config