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

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

相关推荐
QT 小鲜肉5 小时前
【Linux命令大全】001.文件管理(理论篇)
linux·数据库·chrome·笔记
Spirited_Away5 小时前
修改请求头插件迁移manifest V3记录
前端·chrome
靳某某。6 小时前
linux 多个PHP版本切换版本
linux·chrome·php
亮子AI7 小时前
Chrome 和 Edge 生成的 fingerprint 是一样的?
前端·chrome·edge
老兵发新帖7 小时前
Ubuntu如何判断获取到的IP地址是静态IP还是动态?
chrome·tcp/ip·ubuntu
The Electronic Cat1 天前
树莓派安装nginx
运维·chrome·nginx
suoyue_zhan1 天前
GBase 8s V8.8 安装部署实践指南
前端·数据库·chrome
嘻哈baby1 天前
Let‘s Encrypt免费证书与HTTPS配置完全指南
chrome·网络协议·https
qq_257563072 天前
anoconda简单操作
jupyter·cell·anoconda·markerdown
搬山境KL攻城狮2 天前
记-SPA单页面应用Chrome自动翻译导致中文错别字问题
前端·chrome