How to access Jupyter Notebooks running in your local server with ngrok (and an intro to GNU Screen)

I wrote about how to create a SSH Tunnel with ngrok on Ubuntu Server (aka ssh access to your local server in local network with dynamic ip) in another article. If you haven’t installed ngrok yet, please read that guide first.

After I had access to my Ubuntu Server through SSH, the next logical step (at least for me) was to enable Jupyter Notebooks running inside TORUS (Docker) to be reachable from a remote browser. That way I’m able to run python code on the server while using my browser as if it was locally installed. To run ngrok for a http port forward is fast and easy, even remotely through SSH, but before that let’s talk about screen

What is screen?

GNU Screen is a terminal multiplexer, a software application that can be used to multiplex several virtual consoles, allowing a user to access multiple separate login sessions inside a single terminal window, or detach and reattach sessions from a terminal. -Wikipedia

It is one of those tools you will love once you know them. screen lets you “open multiple windows” in your terminal (SSH included). You can run multiple commands and, if connected through SSH, keep running and get back to them even if the connection gets down. To install screen just open a terminal and type

sudo apt-get install screen -y

ngrok and screen

Once you have screen installed lets use ngrok to open a tunnel to your Jupyter instance inside TORUS.

1. SSH into your server 2. Open screen and assign a name

screen -S ngrok_jupyter`

3. Identify on which port is Jupyter Notebooks running

docker ps

4. Start ngrok on that port

ngrok http 32768

This will open ngrok with information on that connection, and you will see the following on your screen. If you access the URL you will open Jupyter Notebooks from anywhere.

5. Now lets use screen

Now you can press crtl + a and then d to leave (detach) screen.

ngrok will still be running. If your SSH connection goes down, ngrok will still be running.

6. To reconnect to (attach) screen and stop ngrok type

screen -r ngrok_jupyter

and press ctrl + c. This will interrupt ngrok

You should also know that

  • If the server connection gets down, you won’t be able to access it unless you are in front of it (physically).
  • You can use the same steps to forward any port from your local server to a public ngrok url. For example, a web server (apache2 or nginx), a custom app, a python server and so on. You only need to know the port on which you can access it locally and then write in when running ngrok

    ngrok http PORT

  • If you don’t want to use screen, you can append & at the end of the command to run it in the background.

  • If you don’t use screen nor append & your ngrok forward will stop if your SSH connection goes down.
  • You can also run ngrok locally. No need to use SSH if you are in front of the server.

Comments

Comments powered by Disqus