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 需要手动修改配置文件
适用场景 临时、一次性使用 长期、频繁使用

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

相关推荐
yeflx1 天前
Ubuntu22.04重装显卡驱动
前端·chrome
如意IT1 天前
Firefox火狐指纹浏览器定制WebGPU指纹方案说明
chrome·firefox·chromium·webgpu·指纹浏览器·浏览器指纹
techdashen1 天前
你想在 Rust 中实现动态库热重载?
开发语言·chrome·rust
着迷不白1 天前
六、Bash Shell 与进程管理
前端·chrome
worxfr1 天前
Linux 磁盘空间排查与清理指南
linux·运维·chrome
王琦03181 天前
shell 第二章 变量和引用
前端·chrome
Xpower 171 天前
Codex 桌面端更新后 Chrome 插件和 Computer Use 不可用,怎么排查和修复
前端·人工智能·chrome·python·学习
剑神一笑2 天前
Linux pgrep 命令详解:按名称查找进程 PID 的高效方法
linux·运维·chrome
剑神一笑2 天前
Linux killall 命令详解:按进程名批量终止进程的原理与实践
linux·运维·chrome
嵌入式小站2 天前
STM32 零基础可移植教程 14:ADC 单通道采样,不接电位器也能读电压
chrome·stm32·嵌入式硬件