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

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

相关推荐
anod9 小时前
Amazon反爬虫:一场从自动化到Chrome插件的折腾之旅
javascript·chrome·amazon·插件
李顿波1 天前
Chrome 扩展程序 MV3 架构组件概览
前端·chrome·浏览器插件
GHL2842710901 天前
安装chrome浏览器
前端·chrome
雁鸣零落1 天前
浏览器扩展 CaptionGo,在网页视频上显示双语字幕,支持 PC 和手机端使用
android·chrome·edge·firefox·安卓
轻动琴弦4 天前
VoltTool —— 一站式在线开发者工具平台,69+实用工具免费使用
前端·chrome
Tim_Van5 天前
在 CentOS 7 中安装 Chromium 的完整步骤
linux·运维·chrome·centos
一只猫的梦5 天前
自动化模块 (Automation Module)
前端·chrome
一只猫的梦5 天前
AI 适配器架构
前端·chrome
HAPPY酷5 天前
【ROS2】PyCharm 启动 Gazebo 仿真报错?从配置 Shell 到修复路径的全流程复盘
ide·chrome·pycharm
sztomarch5 天前
Disable-Download-Chrome-AI-Mode
前端·chrome