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

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

相关推荐
qq_203769491 小时前
在conda环境中使用jupyter
python·jupyter·conda
潇凝子潇2 小时前
Linux 服务器实时监控Shell 脚本
linux·服务器·chrome
ClassOps3 小时前
Chrome 插件记录
前端·chrome
神秘剑客_CN19 小时前
deepin安装chrome
chrome·deepin
Xiaok10181 天前
在 Jupyter Notebook 中启动 TensorBoard
人工智能·python·jupyter
Pixeler1 天前
[特殊字符] Selenium 实战教程:等待与表单元素操作
chrome·selenium·测试工具
命运之光2 天前
【最新】ChromeDriver最新版本下载安装教程,ChromeDriver版本与Chrome不匹配问题
前端·chrome
裤裤兔2 天前
python爬取pdf文件并保存至本地
chrome·爬虫·python·pdf·网络爬虫
bitbrowser2 天前
实操总结:如何高效导入Chrome书签到指纹浏览器?
前端·chrome·python