Connecting to a Jupyter Notebook on a remote Linux machine

My desktop at work is a powerful machine I use for exploratory data analysis and other machine learning work-flows.

In this post I will go over how I connect to my work machine and run Jupyter Notebook work loads when I am working remotely.

SSH into remote machine

Step 1 is to ssh into your remote machine and launch Jupyter Notebook to a local port with the --no-browser option.

user@local_machine$ ssh user@remote_machine 
user@remote_machine$ jupyter notebook --no-browser --port=8889

Setting up an SSH tunnel

Step 2 is to set up an SSH tunnel from your local machine to port 8889 on the remote machine where the Jupyter Notebook is being served.

user@local_machine$ ssh -N -L localhost:8888:localhost:8889 user@remote_mahcine

Here is a break down of the ssh options

  • -N Do not execute a remote command. This is useful for just forwarding ports
  • -L local_socket:remote_socket
    Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.
  • Port forwardings can also be specified in the configuration file. Only the superuser can forward privileged ports. IPv6 addresses can be specified by enclosing the address in square brackets.
  • By default, the local port is bound in accordance with the GatewayPorts setting. However, an explicit bind_address may be used to bind the connection to a specific address. The bind_address of “localhost” indicates that the listening port be bound for local use only, while an empty address or ‘*’ indicates that the port should be available from all interfaces.

Note that running the SSH tunnel with -N will not log any outputs, as long as you do not get an error this means you have established a tunnel.

Load Jupyter Notebook on a local brower

On the local machine browser load localhost:8888 and the Jupyter Notebook from your remote machine will load out as expected.