Port forwarding through SSH
While working on my undergraduate senior thesis a few years ago, I made a small web application to visualize certain patterns as my daily experiments went through. This application was running inside a remote server and was not getting exposed to the public internet, so it was a hassle to check its content.
After trying to figure out a way to make it easier to access said application, I stumbled upon the port forwarding functionality of ssh
. I'm writing this brief explanation many years later to prevent myself from forgetting it again.
As per ssh's man page, you can use the -L
flag to specify "that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side".
Practically speaking, if your remote server is located at example.com
and is running an application that listens for connections on port 8001
, you can run this command on your local machine:
ssh [email protected] -L 3000:localhost:8001
You can read the 3000:localhost:8001
part as "connections to (my local) port 3000
is to be forwarded to the remote server, to the localhost
host and the 8001
port". Adjust the ports and host to suit your use case.
While this ssh
session is running, you can access localhost:3000
on your local machine and you should be greeted by whatever is available on localhost:8001
on the remote machine. Nice!