How to run Jupyter Notebook in server which is at multi hop distance?
This post is in continuation of my previous post “How to set up data science work-space in Ubuntu and Python?”. Today all the budding data scientists are using Jupyter Notebook more frequently. But very often the situation comes where you login to your work-machine
from your local machine
. Generally, the local machine
is your personal computer or laptop. Now all you want to run your notebook in work-machine
and access it via the browser in your local machine
. We will use ssh tunnelling
(link1, link2) in this case.
1-Hop SSH Tunnelling
In the image Host-A
is your local machine and Host-B
is the work machine. We will do ssh tunneling with port forwarding
here. It means while doing ssh
we will bind a port, e.g. port-A
, of Host-A
to a port, e.g. port-B
of Host-B
. Thus we create a ssh tunnel
between these two hosts. So any application running in Host-B:Port-B
can be read by an application running at Host-A:Port-A
. Next we will run the Jupyter Notebook in Host B
at Port-B
. Finally in Host A
start your browser and go to localhost:port-A
. Due to the ssh tunneling with port forwarding
the browser in Host-A
will be able to read the Jupyter Notebook running in the Host-B
. Here are the steps:
Open a terminal in Host-A
and run the following:
ssh -L<port_A>:localhost:<port_B> user@Host-B
Next login to Host-B
and start the Jupyter Notebook as follows:
jupyter notebook --no-browser --port=<port-B>
Remember, give port number without the bracket<>
. Next start the browser in Host-A
2-Hop SSH Tunnelling
Here the scenario is a little complicated than the previous case. Some time we login to our work machine
from our local machine
via a login host
machine. Therefore, we need to create tunnel such that we can connect these three machine. In the above image Host-A
is your local machine and now Host-C
is the work machine. Where as Host-B
is the login host machine. Like the earlier method, we will do ssh tunneling with port forwarding
here also with a tweak. Finally we will run the Jupyter Notebook in Host-C
at Port-C
. And then in Host A
start your browser and go to localhost:port-A
. Due to the ssh tunneling with port forwarding
the browser in Host-A
will be able to read the Jupyter Notebook running in the Host-C
. Here are the steps.
Simple Steps:
1. First open terminal in Host-A and do ssh
from Host-A
to Host-B
ssh -f user@Host-B -L <Port-A>:localhost:<Port-B> -N
2. Now do normal ssh
to Host-B
and then from there create ssh tunnel to Host-C
similarly
ssh -f user@Host-C -L <Port-B>:localhost:<Port-C> -N
3. Now do normal ssh
to Host-C
and start jupyter notebook in Port-C
4. In Host-A
open browser and go to localhost:<Port-A>
and then put the jupyter notebook token
from Host-C
.
Compact Steps
You can combine the steps 1 and 2 as follows:
ssh -L<Port-A>:localhost:<Port-B> user@Host-B -t ssh -L<Port-B>:localhost:<Port-C> user@Host-C
And then follow steps 3 and 4.
For further understanding you can read these posts: link1, link2.
Happy Coding :)