Linux Ssh Tunneling or Port Forwarding Local and Remote Ports with Examples?
Ssh protocol secure remote connections which is shell or terminal based to the remote systems. Ssh protocol and tool provides more than that. We can use
ssh to create tunnels over local and remote systems or connect remote system X server. In this tutorial we will look how to tunnel and port forward locally and remotely.
Local Port Forwarding – Forward or Tunnel Local System Port To Remote System Port
Local port forwarding or tunneling is used to forward given local port to the given remote system port. We need to specify three things
and the syntax is like below.
ssh -L LOCALPORT : DESTINATIONHOST : DESTINATIONPORT REMOTE_HOST
In this example we will connect to the
192.168.122.22 with ssh and forward our local
2222 port to the
2222 port like below.
$ ssh -L 2222:poftut.com:2222 192.168.122.22
Remote Port Forwarding – Forward or Tunnel Remote System Port To Local System Port
This is the same operation where the given remote port is connected to the given local port. We will connect remote system port
22 to our local system port
2222 . We will just change
-L local option to the
-R remote option.
$ ssh -R 2222:poftut.com:2222 192.168.122.22
Dynamic Port Forwarding
Dynamic port forwarding will use SOCKS which default port number is 1080. But another port number can be used. SOCKS generally used to proxy browsers like Chrome, Firefox, Opera. Proxy traffic will be forwarded to the remote system.
$ ssh -D 1080 192.168.122.22
Linux systems uses for GUI X11 server. One of the best feature of SSH is remote application with GUI can be run on local system. Application actually runs on remote system but GUI or X11 protocol is forwarded to the local system and shown like a local application. To enable forwarding provide -X parameter.
$ ssh -X 192.168.122.22
Just with a single shot
$ ssh -X 192.168.122.22 firefox