jupyter 经常在重启工作站之后无法打开
1: 进入root
2: 进入conda环境
3: 查找端口号
🧩 JupyterHub Conda 安装与多用户配置完整指南
本文档介绍如何使用 Conda 安装 JupyterHub,并配置成支持 多用户登录与独立 Conda 环境 的科研/教学服务器。
📘 一、创建 Conda 环境
# 确认 Conda 可用
which conda
# 创建 jupyterhub 独立环境
conda create -n jhub_env python=3.10 -y
# 激活环境
conda activate jhub_env
📦 二、安装 JupyterHub 及依赖
# 安装核心组件
conda install -c conda-forge jupyterhub -y
# 安装 Notebook 与 Lab 支持
conda install -c conda-forge notebook jupyterlab -y
# 安装代理服务(必需)
conda install -c conda-forge configurable-http-proxy -y
验证路径:
which jupyterhub
# 示例输出:/opt/conda/envs/jhub_env/bin/jupyterhub
⚙️ 三、生成配置文件
sudo mkdir -p /etc/jupyterhub
sudo /opt/conda/envs/jhub_env/bin/jupyterhub --generate-config -f /etc/jupyterhub/jupyterhub_config.py
🧩 四、编辑配置文件(支持多用户 + Conda 环境)
将以下内容保存为:
/etc/jupyterhub/jupyterhub_config.py
###############################################################################
# 📘 基本信息
###############################################################################
c.JupyterHub.bind_url = 'http://:8000'
# Hub 运行的数据库和密钥存储
c.JupyterHub.cookie_secret_file = '/etc/jupyterhub/jupyterhub_cookie_secret'
c.JupyterHub.db_url = 'sqlite:////etc/jupyterhub/jupyterhub.sqlite'
###############################################################################
# 👥 用户认证配置
###############################################################################
from jupyterhub.auth import PAMAuthenticator
c.JupyterHub.authenticator_class = PAMAuthenticator
c.PAMAuthenticator.open_sessions = False
# 管理员与白名单
c.Authenticator.admin_users = {'root'}
# c.Authenticator.allowed_users = {'alice', 'bob', 'charlie'}
###############################################################################
# 🧠 Notebook / Lab 默认行为
###############################################################################
c.Spawner.default_url = '/lab'
c.Spawner.cmd = ['jupyter-labhub']
###############################################################################
# 🧰 自动创建用户目录
###############################################################################
import os
from jupyterhub.spawner import LocalProcessSpawner
class AutoHomeSpawner(LocalProcessSpawner):
def start(self):
user_home = os.path.expanduser(f'~{self.user.name}')
nb_dir = os.path.join(user_home, 'notebooks')
if not os.path.exists(nb_dir):
os.makedirs(nb_dir, exist_ok=True)
self.notebook_dir = nb_dir
return super().start()
c.JupyterHub.spawner_class = AutoHomeSpawner
###############################################################################
# 🧪 Conda 环境支持
###############################################################################
import shutil
def detect_user_conda_env(spawner):
user_env_path = f"/opt/conda/envs/{spawner.user.name}/bin/jupyter-labhub"
default_env_path = "/opt/conda/envs/jhub_env/bin/jupyter-labhub"
if os.path.exists(user_env_path):
return [user_env_path]
elif shutil.which("jupyter-labhub"):
return ["jupyter-labhub"]
else:
return [default_env_path]
c.Spawner.cmd = detect_user_conda_env
###############################################################################
# 📜 日志与调试
###############################################################################
c.JupyterHub.extra_log_file = '/var/log/jupyterhub.log'
c.JupyterHub.log_level = 'INFO'
###############################################################################
# 🔒 Proxy 与安全
###############################################################################
c.JupyterHub.proxy_cmd = ['/opt/conda/envs/jhub_env/bin/configurable-http-proxy']
# 若启用 HTTPS:
# c.JupyterHub.ssl_cert = '/etc/ssl/certs/jhub.crt'
# c.JupyterHub.ssl_key = '/etc/ssl/private/jhub.key'
###############################################################################
# ⚙️ 其他可选项
###############################################################################
c.JupyterHub.allow_named_servers = True
c.JupyterHub.cleanup_servers = False
###############################################################################
🧾 五、创建 systemd 服务
/etc/systemd/system/jupyterhub.service:
[Unit]
Description=JupyterHub (with conda multi-env support)
After=network.target
[Service]
Type=simple
ExecStart=/opt/conda/envs/jhub_env/bin/jupyterhub -f /etc/jupyterhub/jupyterhub_config.py
WorkingDirectory=/root
Environment="PATH=/opt/conda/envs/jhub_env/bin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Restart=always
User=root
[Install]
WantedBy=multi-user.target
加载并启动:
sudo systemctl daemon-reload
sudo systemctl enable jupyterhub
sudo systemctl restart jupyterhub
sudo systemctl status jupyterhub
⚙️ 六、用户环境管理
✅ 创建系统用户
sudo useradd -m alice
sudo passwd alice
✅ 创建对应 Conda 环境
conda create -n alice python=3.10 -y
conda activate alice
conda install -c conda-forge jupyterlab ipykernel -y
# 注册 kernel
python -m ipykernel install --user --name alice --display-name "Python (alice)"
✅ 七、访问与验证
浏览器访问:
http://<服务器IP>:8000
输入系统用户(如 alice)的用户名与密码登录。
🧩 八、日志与调试
查看运行状态:
sudo systemctl status jupyterhub
sudo tail -f /var/log/jupyterhub.log
查看端口监听:
sudo ss -tulnp | grep jupyterhub
🎯 附录:常见路径总结
| 项目 | 路径 |
|---|---|
| Conda 环境目录 | /opt/conda/envs/jhub_env/ |
| 用户专属 Conda 环境 | /opt/conda/envs/<username>/ |
| JupyterHub 配置 | /etc/jupyterhub/jupyterhub_config.py |
| JupyterHub 数据库 | /etc/jupyterhub/jupyterhub.sqlite |
| 日志文件 | /var/log/jupyterhub.log |
| 服务文件 | /etc/systemd/system/jupyterhub.service |