从命令行自动配置Jupyter密码

时间:2017-11-03 09:56:17

标签: 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

我想自动化这个过程(在Dockerfile中,当提示输入密码时我无法手动输入),像这样,

echo 'password' | jupyter notebook password

当提示输入密码验证密码

时,这应自动将'password'输入到shell

你能给我一个shell命令,可以在没有用户干预的情况下自动设置密码。

4 个答案:

答案 0 :(得分:1)

我设法用this commit解决了这个问题。我创建了a custom python file,它重用了部分笔记本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"),

答案 1 :(得分:0)

您可以提供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命令),以确保您能够与容器外部的文件夹同步进行读写。

答案 2 :(得分:0)

1。使用passwd函数生成哈希值

在控制台中手动生成您的密码的哈希值:

In [1]: from notebook.auth import passwd; passwd()
Enter password: *************
Verify password: *************
Out[1]: 'sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'

或在脚本中:

$ python3 -c "from notebook.auth import passwd; print(passwd('your_password'))"
sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea

2。使用NotebookApp.password param

运行jupyter

开始时:

jupyter notebook --NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'

或通过jupyter配置,例如在this answer中的Dockerfile中:

RUN jupyter notebook --generate-config
RUN echo "c.NotebookApp.password='sha1:a58051cdbd5c:8ee35109f0076445b37be17d926e56bee5910bea'">>/root/.jupyter/jupyter_notebook_config.py

答案 3 :(得分:0)

要使用回显从bash设置密码,您可以执行以下操作

jupyter notebook --NotebookApp.password="$(echo hello | python -c 'from notebook.auth import passwd;print(passwd(input()))')"

请注意,通过回显发送密码非常不安全。

相关问题