Sub topic 14
Understand token authentication and remote access risks
This chapter assumes you can start JupyterLab from a virtual environment with jupyter lab, as in the earlier chapters. You should also know basic terminal use and what a port is.
Why a notebook server needs protection
A Jupyter server runs code on your machine. Anyone who can reach it and open a notebook or terminal can run any command as your user. That includes reading your files, using your cloud credentials, and installing software. Treat access to the server as equal to a login on the computer.
How token authentication works
By default, JupyterLab listens only on localhost (your own machine) and requires a token. The token is a long random string generated each time the server starts. It appears in the startup URL:
http://localhost:8888/lab?token=3f9a...c1The browser sends the token once, then keeps a cookie. Without the token or cookie, the server refuses requests. You can list running servers and their URLs with:
jupyter server listKeep these points in mind:
- The token is a secret. Do not paste full URLs into chat, tickets, or screenshots.
- A new token is created at each start unless you configure a fixed one.
- Tokens protect against other users and other machines. They do not protect against malware already running as you.
Password instead of token
If restarting the server and copying a new URL gets tedious, you can set a password. The command prompts for it and stores a hash in your Jupyter configuration:
jupyter server passwordChoose a strong, unique password. Never set the token to an empty string to disable login, except in a throwaway sandbox with no network access.
Remote access risks
Running JupyterLab on a remote machine, such as a cloud VM, is common because it gives you more memory or a GPU. The risk appears when you make the server listen on all network interfaces:
jupyter lab --ip=0.0.0.0 --no-browserThis exposes the server to every network the machine is on. Jupyter traffic is plain HTTP by default, so the token travels unencrypted. Automated scanners look for open Jupyter ports on the public internet, and exposed servers are a known way for attackers to gain access.
| Setup | Exposure | Verdict |
|---|---|---|
| Default, `localhost` only | Your machine only | Safe for local work |
| `--ip=0.0.0.0` on a cloud VM with an open port | Anyone who can reach the port | Avoid |
| Localhost on the remote host plus an SSH tunnel | Only users with SSH access | Recommended |
Use an SSH tunnel
An SSH tunnel forwards a port on your laptop to a port on the remote machine through an encrypted connection. The server stays bound to localhost on the remote host, so nothing is opened to the network.
On the remote machine, start the server without a browser:
jupyter lab --no-browser --port 8888On your laptop, open the tunnel (replace the user and host with your own):
ssh -N -L 8888:localhost:8888 user@remote-hostThen open the URL with the token in your local browser, using localhost:8888.
Checklist
- Keep the default
localhostbinding unless you have a specific reason to change it. - Never disable the token or password.
- Use an SSH tunnel for remote work.
- Stop servers you are not using.
jupyter server listshows what is running. - Do not store secrets such as API keys in notebooks. Notebooks get committed and shared.
Exercise
You start jupyter lab --ip=0.0.0.0 on a cloud VM and open port 8888 in the firewall. Name two risks, and describe a safer setup.
Show answer
The server is reachable by anyone on the internet, and traffic, including the token, is unencrypted HTTP, so it can be intercepted or guessed by scanners. A safer setup is to leave Jupyter bound to localhost on the VM, keep the port closed, and connect with ssh -N -L 8888:localhost:8888 user@remote-host, then browse to localhost:8888 on your laptop.