SSH Tunneling Port Forwarding - vicgalle/wiki-example GitHub Wiki

SSH Tunneling / Port Forwarding

Example:

$ ssh -p 5555 -N -f -L localhost:8888:localhost:8889 [email protected]

Note: To create a ssh tunnel to carlitos you have to specify the port, 5555.

In case you have the remote machine configure in the ~/.ssh/config file you only need:

$ ssh -N -f -L [local port]:[remote port] [host]

Example:

$ ssh -N -f -L 8888:localhost:8900 carlitos

Flags

This type of port forwarding lets you connect from your local computer to a remote server. Assuming you are behind a restrictive firewall, or blocked by an outgoing firewall from accessing an application running on port 3000 on your remote server.

You can forward a local port (e.g 8080) which you can then use to access the application locally as follows. The -L flag defines the port forwarded to the remote host and remote port.

$ ssh [email protected] -L 8080: server1.example.com:3000

Adding the -N flag means do not execute a remote command, you will not get a shell in this case.

$ ssh -N [email protected] -L 8080: server1.example.com:3000

The -f switch instructs ssh to run in the background.

$ ssh -f -N [email protected] -L 8080: server1.example.com:3000

Now, on your local machine, open a browser, instead of accessing the remote application using the address server1.example.com:3000, you can simply use localhost:8080 or with your local IP, e.g., 192.168.43.31:8080.