The main idea is that you will run a “headless” notebook from your server, but have the graphical interface (GUI) show up in your local machine’s web browser. The libraries, hardware, and all backend-related stuff depends on your remote machine, but the GUI is seen from your laptop.

Set-up: Here, let’s define the local user and host as localuser and localhost respectively. Similarly, let’s define the remote user and remote host as remoteuser and remotehost. Needless to say, make sure that Jupyter notebook and all its dependencies are installed in both machines. Here’s a quick diagram of the whole process, I will discuss them one-by-one in the next section:

Step 1: Run Jupyter Notebook from remote machine

Log-in to your remote machine the usual way you do. In most cases, this is simply done via an ssh command. Once the console shows, type the following:

  • jupyter notebook: simply fires up your notebook
  • --no-browser: this starts the notebook without opening a browser
  • --port=XXXX: this sets the port for starting your notebook where the default is 8888. When it’s occupied, it finds the next available port.

Step 2: Forward port XXXX to YYYY and listen to it

In your remote, the notebook is now running at the port XXXX that you specified. What you’ll do next is forward this to port YYYY of your machine so that you can listen and run it from your browser. To achieve this, we write the following command:

  • ssh: your handy ssh command. See man page for more info
  • -N: suppresses the execution of a remote command. Pretty much used in port forwarding.
  • -f: this requests the ssh command to go to background before execution.
  • -L: this argument requires an input in the form of local_socket:remote_socket. Here, we’re specifying our port as YYYY which will be binded to the port XXXX from your remote connection.

Step 3: Fire-up Jupyter Notebook

To open up the Jupyter notebook from your remote machine, simply start your browser and type the following in your address bar:

Again, the reason why we’re opening it at YYYY and not at XXXX is because the latter is already being forwarded to the former. XXXX and YYYY can be the “same” number (not the same port, technically) because they are from different machines.

If you’re successful, you should see the typical Jupyter Notebook home screen in the directory where you ran the command in Step 1. At the same time, if you look in your remote terminal, you should see some log actions happening as you perform some tasks.

In your first connection, you may be prompted to enter an Access Token as typical to most Jupyter notebooks. Normally, I’d just copy-paste it from my terminal, but to make things easier for you, you can set-up your own notebook password.

Closing all connections

To close connections, I usually stop my notebook from remote via CTRL + C then Y, and kill the process on YYYY via:

 

Making life easy

Because I often perform these commands in my daily work, I simply created some functions where I only need to supply the port during connection. In my configuration file (this can be your .bashrc), I have the following commands:

And thus I only need to write:

And so:

For me, this reads as “jupyter tunnel”, and the whole command is like an action “tunnel 8889 (the port from my remote) to 8888 (port from my local machine)”.

The text comes from Lj Miranda



12-22 23:36
查看更多