Cha*_*ran 5 linux bash shell ubuntu command-line
我了解到 Jupyter-notebook 可以配置密码而不是令牌。
两步——
$ jupyter notebook password
Enter password: ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json
Run Code Online (Sandbox Code Playgroud)
我想自动化这个过程(在 Dockerfile 中,当提示输入密码时,我将无法手动输入),像这样,
echo 'password' | jupyter notebook password
Run Code Online (Sandbox Code Playgroud)
'password'当提示输入密码和验证密码时,这应该自动将 my 输入到 shell
你能给我一个 shell 命令,它可以在没有用户干预的情况下自动设置密码。
在控制台中手动生成your_password的哈希值:
In [1]: from notebook.auth import passwd; passwd()
Enter password: *************
Verify password: *************
Out[1]: 'sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'
Run Code Online (Sandbox Code Playgroud)
或者在脚本中:
$ python3 -c "from notebook.auth import passwd; print(passwd('your_password'))"
sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea
Run Code Online (Sandbox Code Playgroud)
何时开始:
jupyter notebook --NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'
Run Code Online (Sandbox Code Playgroud)
或通过 jupyter 配置,例如在 Dockerfile 中,如下答案所示:
RUN jupyter notebook --generate-config
RUN echo "c.NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'">>/root/.jupyter/jupyter_notebook_config.py
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以提供 token 参数作为 Docker 中 jupyter 密码的解决方法:
sudo docker run -i -t -p 8888:8888 -v /home/YOUR_USER:/home/ -e USERID=1003 continuumio/anaconda3 /opt/conda/bin/jupyter lab --ip='*' --port=8888 --no-browser --NotebookApp.token='YOUR_PASSWORD' --allow-root --notebook-dir=/home/
然后,输入您的地址:8888(不带令牌),并在出现提示时输入您的令牌而不是密码。
不要忘记检查您的 USERID(使用id终端中的命令),以确保您能够与容器外部的文件夹同步读写。
小智 3
我设法通过此提交解决了这个问题。我创建了一个自定义 python 文件,该文件重用笔记本 passwd() 函数的一部分,然后将其存储在~/.jupyter/jupyter_notebook_config.py文件中。就我而言,我使用的是 docker-compose,因此传递要保存的密码有点复杂,但您可以使用 Docker 轻松地将其作为环境文件传递:
# -*- encoding: utf-8 -*-
import os
import fileinput
import hashlib
import random
from ipython_genutils.py3compat import cast_bytes, str_to_bytes
# Get the password from the environment
password_environment_variable = os.environ.get('JUPYTER_PASSWORD')
# Hash the password, this is taken from https://github.com/jupyter/notebook/blob/master/notebook/auth/security.py
salt_len = 12
algorithm = 'sha1'
h = hashlib.new(algorithm)
salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
h.update(cast_bytes(password_environment_variable, 'utf-8') + str_to_bytes(salt, 'ascii'))
password = ':'.join((algorithm, salt, h.hexdigest()))
# Store the password in the configuration
setup_line = "#c.NotebookApp.password = ''"
new_setup_line = setup_line.replace("''", "u'" + password + "'")
new_setup_line = new_setup_line.replace("#", "")
setup_file = os.getenv("HOME") + "/.jupyter/jupyter_notebook_config.py"
for line in fileinput.input(setup_file, inplace=True):
print line.replace(setup_line, new_setup_line),
for line in fileinput.input(setup_file, inplace=True):
print line.replace("#c.NotebookApp.password_required = False", "c.NotebookApp.password_required = True"),
Run Code Online (Sandbox Code Playgroud)