Auto configure Jupyter password from command line

Viewed 10075

I learnt that Jupyter-notebook can be configured with a password instead of token.

Two steps-

$ jupyter notebook password
Enter password:  ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json

I want to automate this process (in Dockerfile, I won't be able to enter manually when prompted for password), something like this,

echo 'password' | jupyter notebook password

This should auto input my 'password' to the shell when prompted to Enter password and Verify password

Can you give me a shell command which can automate this password setup without user intervention.

4 Answers

1. Generate hash with passwd function

Generate hash for your_password manually in console:

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

or in script:

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

2. Run jupyter with NotebookApp.password param

When started:

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

or via jupyter config, e.g. in Dockerfile as in this answer:

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

You can provide the token argument as a workaround for a password to jupyter in Docker:

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/

Then, enter in your address:8888 (without the token) and, when prompted, enter your token instead of your password.

Don't forget to check your USERID (with the id command in terminal), to ensure you will be able to read+write in sync with folders outside your container.

To setup a password from bash using a echo, you can do the following

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

Please notice that sending password over an echo is highly insecure.

Related