Run Python notebook over ssh - theunissenlab/lab-documentation GitHub Wiki

Total 4 steps for connect & exit Jupyter Notebook with a SSH server:

  1. SSH to the Server and start a jupyter session on a port that is not the default 8888 (could be already used on the remote machine). Run:

    jupyter notebook --no-browser --port=9999
  2. Open another terminal window on your local machine, input:

    ssh -NL localhost:1234:localhost:9999 <remoteuser@server>

    It listens port 1234 (you can change this number) on local machine, which is forwarded from port 9999 of the remote machine.

    The <remoteuser@server> doesn't need to be a format with "@" symbol in it. It can be specified in c:\users\<username>\.ssh\config, I think you must have learned it if you are a SSH user.

  3. Open browser on your local machine, input localhost:1234, and copy the token from the step 1. If you don't want a token, set a password instead using jupter notebook password.

  4. After things are done, CTRL + C to terminate the remote SSH Jupyter server. To stop local machine's port listening on 1234, use netstat to find the PID of process on the port 1234:

    # If local is Linux
    sudo netstat -lpn | grep :1234
    kill <PID number>
    

    For Windows, things are similar:

    # If local is Windows, close the terminal directly and reopen one with Administrator:
    ## If using CMD
    netstat -ano | findstr :1234
    ## If using Powershell
    Get-Process -Id (Get-NetTCPConnection -LocalPort 1234).OwningProcess
    

    then kill it with taskkill /PID <PID number> /F.

⚠️ **GitHub.com Fallback** ⚠️