24

I have a remote access to remote server from my university and I'm accessing it through my local machine! However, my local machine has not enough memory to run multiple jupyter notebooks. Is there any way to run them through the remote server, which probably speed up tasks!! I'm not quite sure though!

I access the server from the terminal in macOS. Thanks!!

Mahesh
  • 556
  • 1
  • 3
  • 16

6 Answers6

61

There's quite a good tutorial here

Essentially you just run the notebook on the remote in no browser mode.

jupyter notebook --no-browser --port=8080

Then setup up an ssh tunnel from the local machine:

ssh -L 8080:localhost:8080 <REMOTE_USER>@<REMOTE_HOST>

Then in your local browser go to: http://localhost:8080/

EDIT:

Running on a specific port is not necessary. The --no-browser tag is.

nihilok
  • 1,325
  • 8
  • 12
  • This http://localhost:8080/ does not open for me. – Parisa Khateri Feb 24 '22 at 14:37
  • I'll add a few links with some references therein since the two answers so far seem to be making good suggestions; however, sometimes it is easier with a little more explanation available and a sense of what parts can be varied. See [here](https://discourse.jupyter.org/t/running-notebooks-remotely/6616/2?u=fomightez) and [here](https://discourse.jupyter.org/t/jupyter-notebook-do-not-open-after-connecting-to-an-ec2-instance/3018/2?u=fomightez) and [here](https://discourse.jupyter.org/t/issue-in-terminal-with-redirecting-to-jupyter-notebook/8120/10?u=fomightez). – Wayne Apr 27 '22 at 15:29
  • 2
    It would be useful to mention that the second step is on the local machine, thanks! – Matifou Aug 03 '22 at 12:13
  • Thanks for this answer. I had a follow-up question. This Jupyter notebook running on the remote server, will it have access to my local virtual environments? Or will it need a virtual environment of its own on the remote server? – CodeAllDay Oct 20 '22 at 00:45
  • The only connection you have to the notebook is via your browser via the tunnel you set up on port 8080, so yeah you'd need to be managing your environments on the remote machine too. – nihilok Oct 20 '22 at 11:05
12

I think you might be looking for port-forwarding.

e.g. when you're logged into your remote via ssh you can:

  1. On the remote machine, start jupyter notebook from your current directory and specify the port:

    jupyter notebook --no-browser --port=9999
    
  2. On the local machine, catch the forwarded port:

    ssh -NfL localhost:9999:localhost:9999 your_user_name@remote_ip_address
    
  3. Go to http://localhost:9999. You should be able to select your notebook and you'll be good to go.

double-beep
  • 5,031
  • 17
  • 33
  • 41
7

you can run jupyter notebook --no-browser --ip="<remote server ip>" on your remote machine terminal. And access notebooks using http://:8888/?token=<> from your browser on local machine.

Janhavi D
  • 155
  • 1
  • 3
  • 8
7

Let's assume that the local user localuser and host as localhost, the remote user and remote host as remoteuser and remotehost. To run a Jupyter notebook on remote server from your local machine you can follow the steps below.

  1. On remote machine run Jupyter notebook with --no-browser with specifying a port

    jupyter notebook --no-browser --port=XXXX
    
  2. Forward it to your local machine via SSH

    ssh -N -f -L localhost:YYYY:localhost:XXXX remoteuser@remotehost
    
  3. Open a browser and go to http://localhost:YYYY

You may need to close the connection

  • Stop your running notebook with CTRL+C then y

  • Kill the process running on port YYYY, netstat returns you the process ID (PID)

     sudo netstat -lpn | grep :YYYY -> for LINUX
     sudo netstat -anv | grep YYYY -> for MacOS
    
     kill PROCESS_ID
    
abdullahselek
  • 7,893
  • 3
  • 50
  • 40
1

To access the remote machine with a browser the notebook must listen on an external facing port (not localhost). You will need the same invocation if want to run the Jupyter notebook on a container. In that case it is something like this:

jupyter notebook --no-browser --port=8080 --ip=0.0.0.0

To listen only in localhost then you can omit the IP

jupyter notebook --no-browser --port=8080

To access remotely the ssh tunnel option in other answers works. You will need an SSH tunnel per notebook. If you need to run multiple notebooks, another option is using the sshuttle tool to simulate a VPN over SSH so you don't have to create multiple tunnels. Run sshuttle with the option to have full reachability to the network of your remote machine (e.g. 10.250.100.40/24) or you can run it to forward all the traffic through the remote machine (like a traditional VPN).

# forward all traffic to network of remote server over the SSH VPN
sshuttle --dns -NHr username@sshserver 10.250.100.40/24

# forward all traffic through the remote server over the SSH VPN
sshuttle --dns -NHr username@sshserver 0/0

Note: The DNS flag is important to be able to use the remote machine DNS for name resolution.

Then execute the notebooks to listen on external ports. For example, running 3 notebooks.

jupyter notebook --no-browser --port=8080 --ip=0.0.0.0
jupyter notebook --no-browser --port=8081 --ip=0.0.0.0
jupyter notebook --no-browser --port=8082 --ip=0.0.0.0

You will use the Token on the output when running the notebooks for the authentication. It will display the output like this

http://server.example.com:8080/tree?token=dd9024f1fb68434645d3902d161f41720650644dc5832f16

Assuming the remote server is not blocking traffic to those port, then on you local machine it will be as simple as opening a browser to

http://<name-of-remote-machine>:8080/tree?token=<your-token>
http://<name-of-remote-machine>:8081/tree?token=<your-token>
http://<name-of-remote-machine>:8082/tree?token=<your-token>
0

Run the jupyter notebook in the no browser mode with your custom port number

jupyter notebook --no-browser --port=8888

Then setup up an ssh tunnel from the local machine:

ssh -L 8888:localhost:8888 <REMOTE_USER>@<REMOTE_HOST>