jupyter lab公开访问配置方法小记

文章目录

核心要点

  1. 绑定 IP 地址 :默认情况下,Jupyter Lab 只绑定到 127.0.0.1(localhost),即本机访问。让它绑定到 0.0.0.0,即可可以通过局域网 IP 访问。
  2. 设置认证:将服务暴露在网络上是非常危险的,必须设置访问密码或使用 Token 进行认证。最简单的方法是使用 Jupyter Lab 自动生成的 Token。
  3. 指定端口:明确指定一个端口号,避免端口冲突。

方法一:命令行直接启动 (快速、临时)

这是最直接的方法,适合临时使用。

  1. 获取服务器的局域网 IP 地址 ,在输出中找到 inet 字段,后面的地址就是局域网 IP,如 172.20.123.45

    bash 复制代码
    ip addr show eth0
  2. 启动 Jupyter Lab

    bash 复制代码
    jupyter lab --ip=0.0.0.0 --port=8888 --allow-root --no-browser
    • --ip=0.0.0.0: 允许所有 IP 访问。
    • --port=8888: 指定端口号(可以换成其他未被占用的)。
    • --allow-root: 允许 root 用户运行。
    • --no-browser: 因为是在服务器上,通常不需要自动打开浏览器。
  3. 获取访问 URL 和 Token

    复制代码
        To access the server, open this file in a browser:
            file:///root/.local/share/jupyter/runtime/jpserver-12345-open.html
        Or copy and paste one of these URLs:
            http://ip:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            or http://ip:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  4. 从其他机器访问 :在局域网内另一台电脑的浏览器中,将 URL 中的主机名(如 DESKTOP-96TPJ9I)替换为您在第一步中获取的局域网 IP 地址 http://172.20.123.45:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,即可访问。


方法二:生成配置文件 (推荐、永久)

  1. 生成配置文件 :运行命令,它会在 ~/.jupyter/ 目录下生成一个名为 jupyter_lab_config.py 的配置文件。

    bash 复制代码
    jupyter lab --generate-config
  2. 设置密码 (可选,但更安全) :运行命令,输入密码。

    bash 复制代码
    jupyter lab password
  3. 修改配置文件 ,保存并退出(在 vim 中是 Esc, :wq, Enter

    bash 复制代码
    vim ~/.jupyter/jupyter_lab_config.py

    在文件末尾添加或修改以下几行:

    python 复制代码
    # 允许所有IP访问
    c.ServerApp.ip = '0.0.0.0'
    # 指定一个端口,不指定则使用默认的8888
    c.ServerApp.port = 8888
    # 允许root用户运行
    c.ServerApp.allow_root = True
    # 禁止在服务器上自动打开浏览器
    c.ServerApp.open_browser = False
    # (可选) 如果设置了密码,这一行默认就是True
    c.ServerApp.password_required = True
  4. 启动 Jupyter Lab

    bash 复制代码
    jupyter lab --allow-root

    注意:即使配置文件里设置 allow_root = True,启动时有时仍需加上 --allow-root 参数。

  5. 从其他机器访问 :在浏览器中访问 http://<您的服务器IP>:8888,页面会提示您输入密码。

总结

特性 方法一 (命令行) 方法二 (配置文件)
优点 快速、无需配置文件 一次配置,永久生效,更安全(可设密码)
缺点 每次都要输入长命令,依赖Token 需要手动修改配置文件
适用场景 临时、一次性使用 长期、频繁使用

强烈推荐使用方法二,一劳永逸。

相关推荐
简离4 天前
前端调试实战:基于 chrome://webrtc-internals/ 高效排查WebRTC问题
前端·chrome·webrtc
Ai老司机5 天前
Chrome浏览器驱动(ChromeDriver)官方下载 - 全平台高速镜像站 | chromedrive.cn
前端·chrome
电商API_180079052475 天前
如何将小红书评论数据封装到API实现自动化分析?测试API实战指南
运维·chrome·自动化
~远在太平洋~5 天前
debian系统已安装python3.12却无法执行python命令
chrome·python·debian
阿珊和她的猫5 天前
浏览器跨页签数据共享方案
前端·javascript·vue.js·chrome
开发者小天5 天前
python中使用jupyter notebook 绘制正态分布直方图 密度图 小提琴图 模仿企鹅喙长分布图
开发语言·python·jupyter
阿珊和她的猫5 天前
Chrome性能测试关键参数解析
前端·vue.js·chrome
zhangfeng11335 天前
快速验证哪个文件坏了 模型文件损坏或不完整大语言模型开发 .llm
人工智能·chrome·语言模型
不光头强6 天前
Linux 系统中最常用的命令及具体使用方法
linux·运维·chrome
田里的水稻6 天前
OE_ubuntu24.04如何安装中文简体拼音输入法
运维·前端·chrome