Home Port Forwarding
Post
Cancel

Port Forwarding

Port Forwarding

Normally we use ssh, with or without port:

1
ssh -p <port> <user_name>@<host_name>

If we only can access server A, and server A only has access to server B and so on. We can use port-forwarding to make laptop port xxxx to have access to target C port cccc.

Port Forwarding Port Forwarding

Starting from the left, for laptop:

1
ssh -N user_a@host_a -L xxxx:localhost:aaaa

If you need -p port when doing ssh, you will also need it when port-forwarding. Just add it right after ssh.

localhost means the server you ssh into, which is host_a and on port aaaa. If the server you ssh into is an HPC cluster, you can still use localhost idicating this node (the login node you accessed into).

For server A:

1
ssh -N user_b@host_b -L aaaa:localhost:bbbb

For server B:

1
ssh -N user_c@host_c -L bbbb:localhost:cccc

With this, we can now access target C port cccc on local laptop port xxxx.

Example – Connecting to Remote Jupyter Notebook

Let’s say we launch a Jupyter Notebook on a remote compute node on port 9999 and accepting all range of ip using:

1
jupyter lab --no-browser --port=9999 --ip="0.0.0.0"

It will print out these and bind to its local port 9999, which is the port in the remote compute node:

1
2
http://127.0.0.1:9999/lab?token=blablabla
http://<localhost_name>:9999/lab?token=blablabla

We can connect to this notebook on our laptop port 8888 by running this on our laptop (ignore port -p if we don’t have one when ssh):

1
ssh -p port -N user@host -L 8888:localhost:9999

We can now open the Jupyter Notebook in browser using:

1
http://127.0.0.1:8888/lab?token=blablabla
This post is licensed under CC BY 4.0 by the author.