Sub topic 14
Understand token authentication and remote access risks
This chapter explains how a Jupyter server decides who may connect, and why exposing one to a network is risky. It assumes you can start JupyterLab from a virtual environment, as in the earlier chapters. It also assumes you know basic terminal use and have a rough idea of what an IP address and a port are. Remote machines come up later, so basic familiarity with SSH helps but is not required.
Why a notebook server is a sensitive service
A Jupyter server does more than display notebooks. It runs code you send it, and it runs that code with the permissions of the operating system user who started it. That code can read your files, delete them, use your cloud credentials, and open network connections. The server also provides a terminal in the browser.
Anyone who can reach the server's web interface and authenticate has the same powers as you. So the question is not whether someone could see your notebooks. The question is whether someone could run arbitrary commands as you. Treat the server like an SSH login, not like a document viewer.
What happens when you run `jupyter lab`
By default, the server does three things that protect you:
- It listens only on the loopback address,
127.0.0.1(also calledlocalhost). Only programs on your own machine can connect. - It listens on port 8888. If that port is busy, it tries the next ones (8889, 8890, and so on).
- It generates a random token when it starts and requires that token for access.
The terminal output includes a URL like this:
http://localhost:8888/lab?token=3f1c9a...long-random-stringA token is a long random string that works as a shared secret. Knowing the URL with the token proves you are allowed in. A new token is generated every time the server starts, so an old URL stops working after a restart.
How token authentication works
When your browser opens the URL, the server checks the token in the query string. If it matches, the server sends back a session cookie. The browser then uses the cookie for later requests, so you do not need the token on every page. Scripts and tools can also send the token in an HTTP header, Authorization: token <value>.
The token is a bearer secret. Whoever holds it can log in, with no username or second factor involved. This is why the places a token can leak matter:
- Browser history and bookmarks, which store the full URL.
- Screenshots, screen shares, and screen recordings that show the terminal.
- Terminal logs, CI logs, and chat messages where you pasted the URL.
- Plain HTTP traffic on a network, where anyone on the path can read it.
If you suspect a leak, stop the server and start it again. That generates a new token.
Finding and managing tokens
You can list running servers, with their tokens, from any terminal:
jupyter server listIf you closed the terminal that started the server, this is the easiest way to get the URL back.
You can also set a password instead of relying on random tokens. This is useful for a server that stays up for a long time:
jupyter server passwordThe command asks you to type a password twice. It stores only a salted hash in jupyter_server_config.json inside your Jupyter config directory. After that, the login page accepts the password. Tokens still work unless you disable them, and you should not disable them casually.
Run jupyter --paths to see where the config directory is on your system. To make persistent settings, generate a config file:
jupyter server --generate-configThis writes jupyter_server_config.py into that config directory. It contains every option, commented out.
Remote access: what changes
You may want to run a notebook on a more powerful machine, a cloud virtual machine, or a lab server. The server must then be reachable from your laptop. The simplest and most dangerous way is to bind it to all network interfaces:
jupyter lab --ip=0.0.0.0This makes the server accept connections from any machine that can route to it. With the default token still on, you have some protection. But the connection is plain HTTP, so the token and everything you type or view travel unencrypted. Port scanners also constantly probe public addresses, so an exposed notebook server on a public IP is found quickly. Servers exposed to the internet with authentication switched off have been abused to run cryptocurrency miners and to steal credentials.
The worst combination is a public address with no token or password:
# Do NOT do this
jupyter lab --ip=0.0.0.0 --ServerApp.token='' --ServerApp.password=''This gives every visitor a shell on your machine. Never use it, not even briefly, unless the machine is isolated from every network you do not control.
Safer remote patterns
Use an SSH tunnel
An SSH tunnel is usually the best option. Leave the server bound to localhost on the remote machine and forward a local port to it through an encrypted SSH connection.
On the remote machine, start the server without opening a browser:
jupyter lab --no-browser --port=8888On your laptop, create the tunnel:
ssh -L 8888:localhost:8888 user@remote-hostThen open http://localhost:8888/lab?token=... in your laptop's browser, using the token printed on the remote machine. Traffic goes through SSH, so it is encrypted. The Jupyter port is never open to the network, and SSH handles authentication with your existing keys.
Use HTTPS if you must expose the port
If a tunnel is not possible, serve Jupyter over TLS, the encryption layer behind HTTPS. You can supply a certificate and key in the config file:
# jupyter_server_config.py
c = get_config() # noqa: F821 (provided by Jupyter when it loads this file)
c.ServerApp.ip = "0.0.0.0"
c.ServerApp.open_browser = False
c.ServerApp.certfile = "/etc/ssl/certs/notebook.pem"
c.ServerApp.keyfile = "/etc/ssl/private/notebook.key"A certificate from a trusted authority avoids browser warnings. A self-signed one encrypts traffic but trains you to click through warnings, which is a bad habit. In many organizations, a reverse proxy such as nginx handles TLS and login instead, and the Jupyter server stays on localhost behind it. A reverse proxy for Jupyter needs WebSocket support, because kernels communicate over WebSockets.
Add network-level limits
Even with a token and TLS, restrict who can reach the port. Use a firewall or cloud security group that allows only your IP address or a VPN range. Use the smallest rule that works.
Quick comparison
| Setup | Encrypted | Reachable from network | Risk level |
|---|---|---|---|
| Default `jupyter lab` on localhost | Not needed, local only | No | Low |
| SSH tunnel to remote localhost | Yes | No (only SSH is open) | Low |
| `--ip=0.0.0.0` with token, plain HTTP | No | Yes | High |
| `--ip=0.0.0.0` with TLS, token, firewall rule | Yes | Limited | Moderate |
| `--ip=0.0.0.0` with no token or password | No | Yes | Critical |
Common mistakes and edge cases
- Running as root. If you start Jupyter as root, every cell runs as root. Use a normal user, and in containers create one rather than relying on root.
- Sharing a server between people. A plain Jupyter server has one user's identity. Two people logging in share the same permissions and the same files. Multi-user setups need JupyterHub or a similar system that gives each user a separate account.
- Trusting notebooks you download. Opening a notebook from an unknown source does not run it. Running its cells does. Read untrusted code before executing it, and run it in a throwaway environment.
- Leaving servers running. A forgotten server on a shared machine stays reachable as long as it runs. Shut it down with
File > Shut Downin JupyterLab, or pressCtrl+Ctwice in the terminal. - Secrets in notebooks. API keys typed into cells are saved in the
.ipynbfile and may end up in Git. Read secrets from environment variables instead. - Reusing the same token or password everywhere. If you set a password, use one you do not use elsewhere.
Checklist for a safe setup
- Keep the default bind address (
127.0.0.1) unless you have a reason to change it. - Leave token authentication enabled.
- For remote machines, use an SSH tunnel with
--no-browser. - If you must expose the port, add TLS and restrict it with a firewall.
- Do not paste token URLs into logs, tickets, or chats.
- Restart the server to rotate the token if it may have leaked.
- Run the server as an ordinary user, not root.
Exercise
You start a notebook server on a cloud VM with jupyter lab --ip=0.0.0.0, and a colleague says "the token protects it, so it's fine." Give two reasons this is still risky, and name a safer way to reach the server from your laptop.
Show answer
First, the connection uses plain HTTP, so the token and all notebook traffic can be read by anyone on the network path. Second, the open port is visible to internet scanners, and a leaked or guessed token gives the attacker a shell as your user. A safer way is to keep the server on localhost, start it with --no-browser, and connect with ssh -L 8888:localhost:8888 user@remote-host, then open the localhost URL in your laptop's browser.
Exercise
You closed the terminal that started JupyterLab, but the server is still running and you need its URL again. Which command shows it, and what should you do if you think the URL has been shared by accident?
Show answer
Run jupyter server list to see the running servers and their tokenized URLs. If the URL may have leaked, stop the server and start it again, because every start generates a new token and the old one stops working.