Ssh protocol secures remote connections which are 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 at 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
- LOCAL_PORT
- DESTINATION_HOST
- DESTINATION_PORT
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 poftut.com
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

X11 Forwarding via SSH Tunelling
Linux systems use for the GUI X11 server. One of the best features of SSH is a remote application with GUI can be run on local system. The application actually runs on a remote system but GUI or X11 protocol is forwarded to the local system and shown as a local application. To enable forwarding to provide -X parameter.
$ ssh -X 192.168.122.22

OR
Just with a single shot
$ ssh -X 192.168.122.22 firefox
