The Jupyter Notebook App is a server-client application that allows editing and running notebook documents via a web browser.
You’re starting to experiment an unknown library and you write a big chunk of code, there are too many errors, so you start debugging but in the end you give up hope. Sad story, eh?
But what if you can write small chunks, check if it’s running fine and proceed to the next? That’d be so great. This is what Notebooks achieve and why I love it.
Remote port forwarding / Reverse ssh tunneling for jupyter notebooks
My laptop’s too slow to even run a hello world program and I try to run it on my friend’s powerful machine. I want to run not just any code, but a jupyter notebook. This is how I do it.
I ssh into my friend’s machine first. Now, the
- Current machine is my friend’s powerful machine – let it be C,
- Remote machine is the local machine, my very slow laptop – R
Start a jupyter notebook on C in a new session, using
jupyter-notebook --no-browser --port 8080
In another session on C, do this
ssh -N -f -R <portR>:localhost:<portC> <user_name>@<local_machine_ip(R's ip)>
<portR> is R’s port we wish to use.
<portC> is C’s port that’s currently used, in this case it’s 8080
<user_name>@<local_machine_ip> is my slow laptop’s address.(R)
Now, I will be able to access the jupyter notebook on my laptop(R) and perform expensive operations using C’s resources.
Using bind address
Syntax for the argument
-R from the man pages of ssh
My friend now wants to access my jupyter notebook on my machine and when I send the address
<R's ip>:portR , my friend wouldn’t able to access it.
To allow nonlocal users to be able to connect
localhost:portC, follow these few steps.
R$ grep GatewayPorts /etc/ssh/sshd_config #GatewayPorts no
In the R’s
/etc/ssh/sshd_config file add
R$ sudo service sshd restart
and run in C,
ssh -N -f -R 0.0.0.0:<portR>:localhost:<portC> <user_name>@<local_machine_ip>
ssh -N -f -R \*:<portR>:localhost:<portC> <user_name>@<local_machine_ip>
ssh -N -f -R "[::]:<portR>:localhost:<portC>" <user_name>@<local_ machine_ip>
If you do this very often, set up a special host in
~/.ssh/config on C:
Host laptop HostName <R's ip> User <user_name> RemoteForward portR localhost:portC
-Nsays that you want an SSH connection, but you don’t actually want to run any remote commands. If all you’re creating is a tunnel, then including this option saves resources.
Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.
-fRequests ssh to go to background just before command execution