How to forward a TCP port over SSH

Encrypted cloud connection

For years SSH has prooven to be extremly useful and flexible protocol.

The forwarding option in SSH is another example for its versatility. Using it you can easily forward a local TCP port to another on the remote machine.

ssh -L [bind_address:]port:host:hostport

For example if your remote machine is running an MySQL server listening on the remote machines local:3306, you can use the SSH forwarding to access it remotely.

To connect your local port 33060 to the remote machines 3306

ssh -L 33060:localhost:3306 root@server.cloudbalkan.com

Another use case for example could be to forward a local port to an external target, going through the SSH connection.

This way the connection to the endpoint will bill opened from the remote server, and you’ll be accesing it through a port on your local machine.

In this example we’ll open a connection to cloudbalkan.com through a remote server:

ssh -L 4443:cloudbalkan.com:443 root@server.cloudbalkan.com

This way opening localhost:4443 will forward the connection, through the server.cloudbalkan.com and to the endpoint cloudbalkan.com.

Of course you might find more useful applications of the nice feature and we’ll be happy to hear from you in the comments below.