Linux 部署 JupyterLab + 内网云公网访问教程
🎯 目标:在你的 Linux 服务器上装一个 JupyterLab,然后用内网云隧道把它暴露到公网——出门在外也能写代码!
🤔 这个教程适合谁?
- 你有一台 Linux 服务器(物理机、虚拟机、云服务器、树莓派……都行)
- 你想搞个 JupyterLab 写 Python、做数据分析、跑 Notebook
- 你想让这个 JupyterLab 内网公网都能用
- 你不想花钱买公网 IP(因为内网云帮你搞定)
预计耗时:20-30 分钟(手把手那种)
📋 你需要准备什么?
| 准备 | 说明 |
|---|---|
| 一台 Linux 服务器 | Debian/Ubuntu/CentOS 都行,推荐 Debian 12 |
| SSH 访问权限 | 能 ssh 到服务器就行 |
| 内网云隧道账号 | 没有的话去 内网云官网 注册一个 |
| root 或 sudo 权限 | 安装软件需要 |
第一步:安装 JupyterLab
💡 这一步就是给你的服务器装上一套交互式 Python 开发环境。
1.1 配置 pip 国内镜像(必做!)
国内访问 PyPI 官方源慢得离谱,先换成清华镜像,不然你等着吧:
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
🚀 配完这个,安装速度从「泡杯咖啡等」变成「眨眼就完」。
1.2 安装基础依赖
apt update && apt install -y python3-pip python3-venv curl
💡
python3-venv是 Python 虚拟环境工具,后面要用。curl用来测试和下载。
1.3 创建虚拟环境 + 安装 JupyterLab
# 创建独立的 Python 虚拟环境(放在 ~/jupyterlab 目录下)
python3 -m venv ~/jupyterlab
# 激活虚拟环境
source ~/jupyterlab/bin/activate
# 安装 JupyterLab
pip install jupyterlab
💡 为什么用虚拟环境? 就像给 JupyterLab 单独租了个小房间,不会跟你服务器上其他 Python 项目打架,卸载也方便(删掉目录就行)。
1.4 验证安装
jupyter lab --version
# 应该输出类似 4.5.x 的版本号
看到版本号就说明安装成功了 ✅
第二步:配置 JupyterLab
💡 这一步是给 JupyterLab 设置「门锁」——登录密码和监听地址。
2.1 生成配置文件
jupyter lab --generate-config -y
2.2 设置登录密码
# 把 yourpassword 换成你自己的密码(后面不再提醒了)
HASH=$(python3 -c "from jupyter_server.auth import passwd; print(passwd('yourpassword'))")
⚠️ 这里的
yourpassword只是个占位符,请换成你自己的强密码。
2.3 写入配置
cat > ~/.jupyter/jupyter_lab_config.py << EOF
c.ServerApp.ip = '0.0.0.0'
c.ServerApp.port = 8888
c.ServerApp.open_browser = False
c.ServerApp.allow_root = True
c.ServerApp.password = '${HASH}'
c.ServerApp.root_dir = '/root'
EOF
每个配置项是啥意思?
| 配置 | 值 | 人话翻译 |
|---|---|---|
ip = '0.0.0.0' | 监听所有网卡 | 局域网里的其他设备也能访问 |
port = 8888 | 端口号 | JupyterLab 的标准端口 |
open_browser = False | 不弹浏览器 | 服务器没有桌面环境,弹了也没用 |
allow_root = True | 允许 root 用户 | 如果你用 root 登录的就加上 |
password | 密码哈希 | 登录时要输入的密码 |
2.4 启动 JupyterLab
nohup jupyter lab --no-browser > /tmp/jupyter.log 2>&1 &
💡
nohup让它在后台跑,关掉终端也不会挂。
2.5 验证本地访问
打开浏览器,访问 http://你的服务器IP:8888
看到这个登录页面就对了 👇
输入密码,登录后看到这个界面——恭喜,JupyterLab 已经跑起来了!🎉

你可以试试在左侧点「Python 3」图标,创建一个 Notebook 写几行代码测试一下:
print("Hello, JupyterLab! 🎉")
运行(按 Shift+Enter),看到输出就说明一切正常 ✅
第三步:公网访问(内网云隧道)
💡 这一步是「魔法」——让只存在于内网的 JupyterLab,全世界都能访问。
3.1 下载 nssh 客户端
nssh 是内网云官方的隧道工具,零依赖、5MB 大小、下载即用。
👉 去 内网云官网下载页 下载对应平台的客户端
💡 Linux 用户选
linux-amd64版本(arm 服务器选linux-arm64)。
下载后放到 /usr/local/bin/ 并赋予执行权限:
chmod +x /usr/local/bin/nssh_client
3.2 建立隧道
nssh_client --daemon -R 80:127.0.0.1:8888 yourname@服务器地址 -p 22 --passwd yourpassword
参数解释:
| 参数 | 作用 |
|---|---|
--daemon | 后台运行,关掉终端也不影响 |
-R 80:127.0.0.1:8888 | 把远程 80 端口映射到本地 8888 端口 |
yourname | 你的内网云隧道用户名 |
服务器地址 | 内网云提供的隧道服务器地址 |
--passwd yourpassword | 你的隧道密码 |
💡 建议用
127.0.0.1— nssh 和 JupyterLab 跑在同一台服务器时,127.0.0.1最简单。如果 nssh 在另一台机器上,用目标服务器的局域网 IP 即可。
3.3 验证隧道
nssh_client --list
看到类似这样的输出就说明隧道通了:
# local-server user remote-server status
---------------------------------------------------------------
1 127.0.0.1:8888 yourname 服务器地址:22 online
3.4 公网访问验证
现在,打开浏览器,访问你的内网云公网域名:
输入密码,登录成功!
🎉🎉🎉 你的 JupyterLab 已经可以在全世界任何地方访问了!
第四步:持久化部署(开机自启)
💡 如果你不做这一步,服务器重启后 JupyterLab 和隧道都会挂掉——然后你就会在外出差时发现「咦怎么连不上了?」。
4.1 JupyterLab 开机自启
cat > /etc/systemd/system/jupyterlab.service << 'EOF'
[Unit]
Description=JupyterLab Server
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root
ExecStart=/root/jupyterlab/bin/jupyter lab --no-browser
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable jupyterlab
systemctl start jupyterlab
💡 以后可以用这些命令管理服务:
systemctl status jupyterlab→ 查看状态systemctl restart jupyterlab→ 重启systemctl stop jupyterlab→ 停止
4.2 nssh 隧道开机自启
cat > /etc/systemd/system/nssh-tunnel.service << 'EOF'
[Unit]
Description=nssh Tunnel to JupyterLab
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/nssh_client --daemon -R 80:127.0.0.1:8888 yourname@服务器地址 -p 22 --passwd yourpassword
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable nssh-tunnel
systemctl start nssh-tunnel
💡 两个服务都配了
Restart=always——挂了会自动拉起来,你根本不用操心。
4.3 确认两个服务都在跑
systemctl status jupyterlab
systemctl status nssh-tunnel
两个都显示 active (running) 就完美了 ✅
🔒 安全建议
-
改掉弱密码