Remote jupyter notebook via ssh port forwarding

1 minute read

Published:

Remote jupyter notebook can be useful when the remote computer has better computing power than the local one or the remote is a server that you can connect from different places.

Prepare

  • First, you need to have openssh server installed on the remote machine.
  • You will need to generate public key authentication on our local machine.
  • In the remote machine, copy the content of id_rsa.pub to ~/.ssh/authorized_keys (reference link)
  • Test ssh without typing the password
    $ssh <username>@<remoteIP>
    
  • In the remote machine, install anaconda or miniconda
  • Create a deeplearning env (optional) using conda
  • Activate deeplearning env, and install jupyter notebook (reference)
  • Configure Jupyter to use Python kernel
    ipython kernel install --user --name=deeplearning
    
  • In the remote machine, add PATH for jupyter notebook in ~/.bashrc file. I am using the path in miniconda.
    export PATH="/home/leiming/miniconda3/envs/deeplearning/bin:$PATH"
    

Test

  • Now, from your local machine, you can ssh to remote.

  • Start jupyter notebook via port 8889 in the remote machine. (Do not close the current terminal!)
    jupyter notebook --no-browser --port 8889
    
  • Start a new terminal in your local terminal
    ssh -N -L localhost:8899:localhost:8889 <username>@<remoteIP>
    
  • Copy the localhost link (from the remote machine terminal) to your browser in your local machine
    http://localhost:8889/?token=dd5ac17bc80e068ece002ea35ccf867547030cfa0328
    
  • Now, you should see your brower has a jupyter notebook with the default deeplearning conda env.