Running Jupyter on a Remote Server and Connecting Locally

How to run Jupyter on a remote server and connect to it locally, using SSH tunnels.

Bash Logo

Create a tmux or screen session on the remote server

# tmux
tmux new -s jupyter
 
# screen
screen -S jupyter
# tmux
tmux new -s jupyter
 
# screen
screen -S jupyter

Start Jupyter on the remote server

jupyter notebook --no-browser --port=8889
jupyter notebook --no-browser --port=8889

Note: Copy the token from the output of the above command and save it somewhere.

Detach from the tmux or screen session

# tmux
Ctrl + b, d
 
# screen
Ctrl + a, d
# tmux
Ctrl + b, d
 
# screen
Ctrl + a, d

Create an SSH tunnel from the local machine to the remote server

ssh -N -f -L localhost:8888:localhost:8889 user@remote-server
ssh -N -f -L localhost:8888:localhost:8889 user@remote-server

Open Jupyter in the browser

Open http://localhost:8888 in the browser and enter the token from earlier.

Reattach to the tmux or screen session

# tmux
tmux attach -t jupyter
 
# screen
screen -r jupyter
# tmux
tmux attach -t jupyter
 
# screen
screen -r jupyter

Kill the tmux or screen session

# tmux
tmux kill-session -t jupyter
 
# screen
screen -X -S jupyter quit
# tmux
tmux kill-session -t jupyter
 
# screen
screen -X -S jupyter quit