如何通过SSH在远程服务器启动Jupyter Notebook并在本地浏览器运行?
Absolutely, this is a super common workflow and totally solves the hassle of coding in a terminal-only environment. Here's a step-by-step guide to get you set up smoothly:
Step 1: Install Jupyter on the Remote Server
First, SSH into your remote server like you normally do. If Jupyter isn’t already installed, run one of these commands based on your package manager:
- For pip users:
pip install jupyter(add--userif you don’t have admin rights) - For conda users:
conda install -c conda-forge jupyter
Step 2: Start Jupyter Notebook on the Server
Launch Jupyter without opening a browser on the server, and specify a port (we’ll use 8889 here, but you can pick any unused port):
jupyter notebook --no-browser --port=8889
If you haven’t set a password for Jupyter yet, it’ll output a token in the terminal—save that, you’ll need it to log in locally. For a more convenient experience, set a password ahead of time with:
jupyter notebook password
Step 3: Set Up SSH Port Forwarding from Local to Server
Open a new terminal window on your local machine (don’t close the one running Jupyter!) and run this command to create an encrypted tunnel:
ssh -L 8889:localhost:8889 your_username@remote_server_address
Quick breakdown:
-Ltells SSH to set up a local port forward8889:localhost:8889means "forward local port 8889 to the server’s localhost port 8889"- Swap
your_usernameandremote_server_addresswith your actual server credentials
Step 4: Access Jupyter from Your Local Browser
Open your favorite browser and navigate to:http://localhost:8889
Enter your Jupyter password if you set one, or paste the token from the server terminal into the login page (you can also append it to the URL like http://localhost:8889?token=your_token_here).
Pro Tips & Notes
- Port Conflicts: If 8889 is in use locally, just pick a different port (like 8890) — update both the server’s
--portflag and the local forward command (e.g.,-L 8890:localhost:8889). - Security: Since we’re using SSH tunneling, you don’t need to open the Jupyter port to the public on your server’s firewall—all traffic is encrypted through the SSH connection, which is way safer.
- Persistent Sessions: To keep Jupyter running even after you close your SSH terminal, use tools like
screenortmuxon the server. For example:screen -S jupyter_session # Start Jupyter as usual here # To detach: press Ctrl+A then D # To reattach later: screen -r jupyter_session
内容的提问来源于stack exchange,提问作者Deepak

