使用 jupyterlab 开发 python 程序非常方便,但是只能单用户访问。想要多用户共用服务器使用 jupyterlab 可以采用 jupyterhub 实现。下面演示在 CentOS 上搭建 jupyterhub 实现多用户访问 jupyterlab。本篇所有命令均使用 root 用户运行,jupyterhub 版本小于 2.0.0,对于 jupyterhub 2.0.0 的部署方法请参看这篇。
安装 Miniconda
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| yum install wget -y wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh chmod +x Miniconda*.sh
./Miniconda*.sh rm -rf Miniconda*.sh
echo 'export PATH=/usr/local/miniconda/bin:$PATH' >> /etc/profile source /etc/profile
groupadd miniconda chgrp -R miniconda /usr/local/miniconda chmod 770 -R /usr/local/miniconda usermod -a -G miniconda root usermod -a -G miniconda jinzhongxu
id root id jinzhongxu groups
pip install --upgrade pip jupyterhub jupyterlab pandas matplotlib youtube_dl numba boost scipy numpy sympy scikit-learn
pip install jupyterlab_code_formatter;jupyter server extension enable jupyterlab_code_formatter;pip install black isort
|
安装 npm, nodejs
1 2 3 4 5 6
| yum update -y yum remove nodejs npm -y yum install epel-release -y yum install psmisc vim htop zsh git nload curl bzip2 util-linux-user gcc npm -y curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - yum install nodejs -y
|
配置 jupyterhub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| yum install npm -y npm install -g configurable-http-proxy
jupyterhub --generate-config mkdir -p /etc/jupyterhub mv jupyterhub_config.py /etc/jupyterhub/config.py echo "c.JupyterHub.base_url = '/jupyterhub'" >> /etc/jupyterhub/config.py echo "c.JupyterHub.ip = '0.0.0.0'" >> /etc/jupyterhub/config.py echo "c.Spawner.default_url = '/lab'" >> /etc/jupyterhub/config.py echo "c.PAMAuthenticator.open_sessions = False" >> /etc/jupyterhub/config.py echo "c.LocalAuthenticator.create_system_users = True" >> /etc/jupyterhub/config.py echo 'c.Authenticator.admin_users = set(["jinzhongxu"])' >> /etc/jupyterhub/config.py echo "c.JupyterHub.admin_access = True" >> /etc/jupyterhub/config.py echo "c.Spawner.cmd = ['/usr/local/miniconda/bin/jupyterhub-singleuser']" >> /etc/jupyterhub/config.py wget https://www.dropbox.com/s/vak1nsajt7s8muj/jupyterhub.service?dl=0 -O jupyterhub.service mv jupyterhub.service /etc/systemd/system/jupyterhub.service systemctl daemon-reload systemctl restart jupyterhub.service systemctl enable jupyterhub.service
|
配置 nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| vim /etc/nginx/nginx.conf
server { listen 80; server_name www.vvv.com; root /usr/share/nginx/html; index index.php index.html index.htm;
location /jupyterhub { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-Scheme $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_read_timeout 120s; proxy_next_upstream error; }
|
上面所有都配置好后,访问 https://www.vvv.com/jupyterhub 即可登录。
更多搭建方式(如 docker)可参考 https://github.com/jupyterhub/jupyterhub
增加新用户
在网页上的以用户 jinzhongxu 登录 jupyterhub后,默认进入 jupyterlab,点击 File –> Hub Control Panel 进入 jupyterhub 管理后台,点击 admin 即可管理增加用户,开启服务,关闭服务,关闭 Hub 等。
点击增加用户 username 后,根据上面的配置默认会在系统上创建该用户及家目录。但此时还不能使用过 jupyterlab 服务,需要管理员登录 shell,将新创建的用户增加到 miniconda 组中,命令如下:
1 2 3
| usermod -a -G miniconda username
passwd username
|
此时,该用户即可登录 https://www.vvv.com/jupyterhub 了,用户名和密码就是该用户登录 shell 的用户名(username)和密码。
禁止某用户登录
一种方法是使用命令将该用户的密码删除
另一种方法是,将该用户从 miniconda 组中剔除
1 2 3 4
| deluser username miniconda
gpasswd -d username miniconda
|
参考文献
github:jupyterhub
How to set a multiuser Jupyterlab server with Jupyterhub
jupyterhub 安装教程
JupyterHub 部署与应用指南
单机多用户 JupyterLab 环境搭建
Linux下多用户Anaconda环境的安装与卸载
Linux: 删除用户密码/禁止登录
如何在 Ubuntu 上为用户授予和移除 sudo 权限