JupyterLab 是一個基於 Web 的互動式開發環境,可以用於資料科學、數值計算、機器學習等領域的開發和研究工作。Jupyter 是 JUlia、PYT、R 三者的合寫。
1 安裝
有兩種方式:直接在機器上安裝和基於 docker 安裝。
1.1 直接安裝
用 python 的包管理器就行,pip和conda都可以。
python3 -m pip install --user --upgrade jupyterlab
1.2 用 docker
首先需要安裝 docker,可以參考本網站其他部分。然後獲取相應的 docker image,這部分也可以參照這裡。
docker pull jupyter/datascience-notebook
2 設定 jupyterlab
2.1 本地安裝的 jupyter 用 systemctl 來管理
需要建立一個jupyter.service檔案,地址是/etc/systemd/system/jupyter.service:
[Unit]
Description=Jupyter Lab
After=syslog.target network.target
[Service]
User=your_username
Group=your_groupname
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
ExecStart=/usr/bin/jupyter lab --ip=0.0.0.0 --port=8888 --no-browser
WorkingDirectory=/home/your_username/
Restart=always
RestartSec=10
KillMode=mixed
[Install]
WantedBy=multi-user.target
注意,您需要將 User 和 Group 欄位替換為您的使用者名稱和組名。WorkingDirectory 欄位應指向您要在其中執行 Jupyter Lab 的目錄。對服務進行管理:
sudo systemctl daemon-reload
sudo systemctl start jupyter
sudo systemctl enable jupyter
設定防火牆規則。如果您的伺服器上啟用了防火牆,請確保開放 Jupyter Lab 使用的埠(預設情況下為 8888)。
2.2 使用 docker 來設定
建立一個資料夾用於儲存 JupyterLab 的設定檔,例如:
mkdir jupyterlab_config
在終端中,使用以下命令來拉取 JupyterLab 的 Docker 映像:
docker pull jupyter/scipy-notebook
該命令將從 Docker Hub 中拉取 JupyterLab 的 scipy-notebook 版本的映像。你可以選擇其他版本,例如 jupyter/datascience-notebook 或者 jupyter/minimal-notebook,具體可以參考 Docker Hub 中的說明文件。執行以下命令來啟動 JupyterLab 容器:
docker run -p 8888:8888 -v /path/to/jupyterlab_config:/home/jovyan/.jupyter jupyter/scipy-notebook
這將啟動 JupyterLab 容器,並將本地的 8888 埠對映到容器的 8888 埠,同時將 JupyterLab 的設定檔儲存在本地 /path/to/jupyterlab_config 資料夾中。
在瀏覽器中訪問 JupyterLab,輸入 http://localhost:8888 並按下回車鍵。你將看到 JupyterLab 的登入頁面。在登入時,請使用 token 身份驗證,可以在容器日誌中找到該令牌,或者可以使用以下命令來獲取令牌:
docker logs <container_name_or_id>
在容器日誌中,可以找到類似於下面這樣的令牌:
http://127.0.0.1:8888/?token=xxxxxxxxxxxx
將令牌複製並貼上到登入頁面上即可。
完成後,可以在 JupyterLab 中使用自己的 Python 程式碼和庫了。
3 安全功能
主要是設定密碼,開啟 2FA。
3.1 設定密碼
要設定 JupyterLab 的密碼,請執行以下命令:
jupyter notebook password
然後,按照提示輸入所需的密碼即可。密碼將以雜湊方式儲存在您的 JupyterLab 設定檔中。
3.1 使用 2fa 來設定
要啟用 JupyterLab 的 2FA,可以使用 jupyter-server-mfa 擴充。以下是一些簡單的步驟:
python3 -m pip install jupyter-server-mfa
生成並設定共享金鑰。以下命令生成 64 位元組的隨機金鑰:
jupyter mfa-keygen
將生成的金鑰儲存在安全的地方,並將其新增到 JupyterLab 設定檔中:
c.ServerMFA.shared_secret = b'your_secret_key'
設定 JupyterLab。要啟用 2FA,需要在 JupyterLab 設定檔中新增以下行:
c.ServerApp.authenticator_class = 'jupyter_server_mfa.MFAAuthenticator'
c.ServerApp.disable_check_xsrf = True
重新啟動 JupyterLab,然後使用使用者名稱和密碼登入。登入後,您將被要求輸入 TOTP(時間同步一次性密碼)程式碼,可以使用 Google Authenticator 或類似的應用程式生成它。
希望這些步驟可以幫助您在 JupyterLab 中設定密碼或啟用 2FA。請記住,使用強密碼、2FA 等安全措施可以幫助保護您的系統和資料。