Uwe.Schneider
Uwe.Schneider

Reputation: 1415

Jupyter notebook SSH tunnel for two step ssh tunnel

I want to access a jupyter notebook via an SSH tunnel and follow this recipe

https://hsaghir.github.io/data_science/jupyter-notebook-on-a-remote-machine-linux/

To sumarize - : 1. Log in remote machine

user@local_host$ ssh user@remote_host

remote_user@remote_host$ jupyter notebook --no-browser --port=8889

2.In a new terminal:

user@local_host$ ssh -N -L localhost:8888:localhost:8889 remote_user@remote_host

3.Then go to a browser and go to

localhost:8888

Now here is my problem: I can access the remote machine only in two steps

ssh -X username@server

ssh -KX my_pc_name

and the jupyter notebook is only installed on my_pc_name.

What do I write for the second step, when I replace the first line of the first step by my longer log in procedure ?

When I plug in remote_user = username and remote_user = my_pc_name, I get an security error from the jupyter notebook asking for a token. The token that I get from step one, running the jupyter notebook will not work.


One solution could be to combine the two ssh log in steps to one.

Upvotes: 7

Views: 7878

Answers (2)

Vadim
Vadim

Reputation: 4529

Although we have a great answer from Kai, it might confuse somebody; at least I had some issues.

In order to establish a connection from your server to a local machine for the jupyter notebook/lab, we need to open three terminal windows on the local machine.

Please follow the order.

First terminal window:

ssh -f username1@gateway_server -L :7001:localhost:7001 -N -v -v

don't close the first terminal!

Second terminal window:

ssh username1@gateway_server
ssh -f username2@working_server -L 7001:localhost:7001 -N -K -v -v

don't close the second terminal!

Third terminal window:

ssh -X username1@gateway_server
ssh -KX username2@working_server
jupyter lab --no-browser --port=7001&

After on your local machine, go to a browser: http://localhost:7001/

Tested on Mac.

Upvotes: 6

Kai Aeberli
Kai Aeberli

Reputation: 1220

It seems 'server' is your gateway server, and that 'my_pc_name' is accessible only from there. Try establishing two connected ssh tunnels like so:

https://medium.com/@sankarshan7/how-to-run-jupyter-notebook-in-server-which-is-at-multi-hop-distance-a02bc8e78314

So I would do this. Open a terminal and run:

ssh -f username@server -L 8888:localhost:8889 -N

This connects your local machine to the jump server and does port forwarding.

Then open a new terminal and run:

ssh username@server
ssh -f my_pc_name -L 8889:localhost:8889 -N -K

This should connect you to the jump server and do port forwarding between jump server and my_pc_name.

Then open another terminal and run:

ssh -X username@server
ssh -KX my_pc_name
jupyter notebook --no-browser --port=8889

This should connect you to my_pc_name and run the jupyter notebook server there.

Finally go to your browser on your local machine and access: localhost:8888

You do have the -X option in your ssh connection string, which indicates X11 windowing (a type of remote desktop for linux). Try dropping it and see if it still works, else you may have to keep it. Also, -K indicates forwarding of Kerberos tickets, which you probably need to allow file access, so I kept it.

You may have to play with a combination of these on your machine to get it working.

Upvotes: 11

Related Questions