如何在Jupyter Notebook会话中禁用密码请求?

111

多年来,我一直使用以下命令启动Jupyter Notebook:

jupyter-notebook --port=7000 --no-browser --no-mathjax

当我尝试在浏览器上打开Jupyter时,它要求我输入密码,即使我以前从未设置过密码。需要注意的是,如果我将端口设置为与7000不同的值(例如,默认值8888),则界面将无问题地打开。
我正在本地运行Jupyter,并且在以下设置上:
Python 3.5.2

已安装以下模块:

jupyter (1.0.0), jupyter-client (4.4.0), jupyter-console (5.0.0), jupyter-core (4.2.1), ipykernel (4.5.2), ipython (5.1.0), ipython-genutils (0.1.0), nbconvert (4.3.0), nbformat (4.2.0), notebook (4.3.0)

注意:我没有jupyter配置文件

以下是服务器输出的一些行:

[I 19:16:24.358 NotebookApp] Serving notebooks from local directory: /Users/my_user_name
[I 19:16:24.358 NotebookApp] 0 active kernels
[I 19:16:24.358 NotebookApp] The Jupyter Notebook is running at: http://localhost:7000/?token=aa0dab6e2d85766f3e2e4f0f6633e4473db56a56c94cac76
[I 19:16:24.358 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

以下是我尝试在浏览器中打开它(使用端口7000)后出现的消息:

[I 19:21:56.848 NotebookApp] 302 GET /tree (::1) 8.46ms
[D 19:21:56.857 NotebookApp] Using contents: services/contents
[D 19:21:56.919 NotebookApp] Path base/images/favicon.ico served from /usr/local/lib/python3.5/site-packages/notebook/static/base/images/favicon.ico
[D 19:21:56.920 NotebookApp] Path components/jquery-ui/themes/smoothness/jquery-ui.min.css served from /usr/local/lib/python3.5/site-packages/notebook/static/components/jquery-ui/themes/smoothness/jquery-ui.min.css
[D 19:21:56.922 NotebookApp] Path components/jquery-typeahead/dist/jquery.typeahead.min.css served from /usr/local/lib/python3.5/site-packages/notebook/static/components/jquery-typeahead/dist/jquery.typeahead.min.css
[D 19:21:56.923 NotebookApp] Path style/style.min.css served from /usr/local/lib/python3.5/site-packages/notebook/static/style/style.min.css
[D 19:21:56.925 NotebookApp] Path auth/css/override.css served from /usr/local/lib/python3.5/site-packages/notebook/static/auth/css/override.css
[D 19:21:56.926 NotebookApp] Path components/es6-promise/promise.min.js served from /usr/local/lib/python3.5/site-packages/notebook/static/components/es6-promise/promise.min.js
[D 19:21:56.926 NotebookApp] Path components/requirejs/require.js served from /usr/local/lib/python3.5/site-packages/notebook/static/components/requirejs/require.js
[D 19:21:56.933 NotebookApp] Path base/images/logo.png served from /usr/local/lib/python3.5/site-packages/notebook/static/base/images/logo.png
[D 19:21:56.934 NotebookApp] 200 GET /login?next=%2Ftree (::1) 80.86ms
[D 19:21:57.001 NotebookApp] Path custom.css served from /usr/local/lib/python3.5/site-packages/notebook/static/custom/custom.css
[D 19:21:57.003 NotebookApp] 304 GET /custom/custom.css (::1) 3.11ms
[D 19:21:57.341 NotebookApp] Path auth/js/main.min.js served from /usr/local/lib/python3.5/site-packages/notebook/static/auth/js/main.min.js
[D 19:21:57.344 NotebookApp] 200 GET /static/auth/js/main.min.js?v=20161219191623 (::1) 3.57ms

此时,Jupyter 页面要求我输入一个从未设置过的密码。

问题已解决!

使用最新的 notebook 模块(4.3.1),问题已经得到解决。

一旦启动 Jupyter Notebook,用户会在第一次连接时被提示将 URL 粘贴到浏览器中:

Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
    http://localhost:7000/?token=32be0f5ee74cfe521187bc479855ce8b9fbab9e8021701c9

问题已经得到解决!


如果您不运行Jupyter,是否会发生相同的情况?Jupyter会打印哪些消息到标准输出? - user824425
是的!这些是最后的输出行(已删除方括号中的起始部分):路径base/images/logo.png从/usr/local/lib/python3.5/site-packages/notebook/static/base/images/logo.png提供 200 GET /login?next=%2Ftree (::1) 106.60ms路径custom.css从/usr/local/lib/python3.5/site-packages/notebook/static/custom/custom.css提供 304 GET /custom/custom.css (::1) 3.09ms路径auth/js/main.min.js从/usr/local/lib/python3.5/site-packages/notebook/static/auth/js/main.min.js提供 200 GET /static/auth/js/main.min.js?v=20161215192744 (::1) 4.07ms - Salvatore Cosentino
我认为另一个程序正在使用端口7000。在这里,最后的输出行并不那么有趣;如果端口被占用,你应该在第一个输出行中看到它。请编辑您的问题(而不是评论)以包括jupyter-notebook命令的输出。 - user824425
已解决!”,更准确地说是绕开了,而不是修复。似乎在浏览器中运行Python的所有内容都应该具有这种类型的“安全性”。修复它需要不使用浏览器,因此可能是一开始就做出的错误设计选择。来自Jupyter团队的更多答案可以在2019年中找到,其中提到了一个环境变量JUPYTER_TOKEN,另一个绕过方法。 - mins
14个回答

123

以下方法十分不安全,但您可以使用以下方式彻底删除密码:

jupyter notebook --ip='*' --NotebookApp.token='' --NotebookApp.password=''

如果没有使用 --NotebookApp.password='',当从远程计算机连接到本地 Jupyter 时,只需简单启动:

jupyter notebook --ip='*'

出于安全考虑,它仍然要求输入密码,因为拥有访问权限的用户可以在服务器机器上运行任意的Python代码!

请注意,在我的机器上,只需运行:

jupyter notebook

已经在我的浏览器中打开了一个已登录的窗口,并且标准输出(stdout) 包含:

    To access the notebook, open this file in a browser:
        file:///home/ciro/.local/share/jupyter/runtime/nbserver-3286-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=7c9265bf9df5f57cf5da88f410a71b097e2548ae375826b7
     or http://127.0.0.1:8888/?token=7c9265bf9df5f57cf5da88f410a71b097e2548ae375826b7
如果您的浏览器没有自动打开,您可以尝试其中一个链接,它们似乎有一个登录令牌,然后调查为什么您的浏览器没有自动打开。
在Jupyter 4.4.x,Ubuntu 18.04上进行了测试。

2
这会在代理后面抛出一个错误,提示“POST请求缺少'_xsrf'参数”。 - Jinna Balu
“以下操作非常不安全,但您可以完全删除密码。” 如果这是非常不安全的操作,为什么要提及它呢?是针对那些与网络断开连接的计算机吗? - mins
2
在家庭局域网上,这可能是可以的。OP的问题本质上是关于克服安全机制的。 - Ciro Santilli OurBigBook.com
3
它只在某些情况下才是“不安全的”。重要的是,在大多数情况下,与涉及在本地网络上无意义地复制令牌的默认设置相比,它要少得多地令人烦恼。 - quant
1
是的,它会禁用令牌认证。如果您正在使用 Docker(我的用例),则可以通过在构建镜像时编辑 .sh 脚本来添加它,或者像这样使用 Docker 运行命令 start-notebook.sh --NotebookApp.token='' 添加它。 - Bruce wayne - The Geek Killer
由于我正在远程服务器上运行此程序,我该如何修改打印的URL,使其指向公共IP地址而不是127.0.0.1?复制粘贴令牌并在浏览器中添加我写的地址非常麻烦。 - undefined

30

如何通过禁用 Jupyter Notebook 密码和令牌来避免“无效凭证”

首先打开Anaconda Prompt

  1. 输入命令 jupyter notebook --generate-config

enter image description here

  1. 从 jupyter 目录中,编辑 jupyter_notebook_config.py文件

将以下命令更改为:

 c.NotebookApp.token = ''
 c.NotebookApp.password = u''
 c.NotebookApp.open_browser = True
 c.NotebookApp.ip = 'localhost'

现在从Anaconda Navigator启动Jupyter笔记本电脑,问题一定会很快解决。


22

如果您想无需密码运行docker,只需像下面这样使用CMD

CMD ["jupyter", "notebook", "--no-browser","--NotebookApp.token=''","--NotebookApp.password=''"]

1
是的,没错。我们也可以编辑 .sh 文件,我的情况下是 custom-start.sh,我写了这样的内容 jupyter notebook --allow-root --no-browser --ip=0.0.0.0 --port=8888 --NotebookApp.token='' --NotebookApp.password='' 并构建了 Jupiter Notebook 镜像。 - Bruce wayne - The Geek Killer
如果有人阅读了上面的评论并想尝试该方法,我在下面的“答案”中进行了解释。 - Bruce wayne - The Geek Killer

10

Notebook 4.3.0默认启用了登录安全设置。在启动notebook服务器时,密码字段输入的令牌将被打印在输出信息中(或者可以直接包含在URL中)。

The Jupyter Notebook is running at: http://0.0.0.0:8888/?token=f3e7fa23fb7e347ad05914368b625416b7a95a674dc078f7

请参阅http://jupyter-notebook.readthedocs.io/en/latest/security.html#server-security了解更多信息,包括禁用此功能。
然而,这并不能解释为什么在一个端口上运行时会提示密码,而在另一个端口上不会。

使用的 cookie 在不同的端口上是不同的,因此您可以在一个端口上登录到服务器,但在另一个端口上却无法登录。您可以使用 jupyter notebook list 命令查看所有正在运行的服务器的带有令牌的 URL。 - minrk

6

您可以先创建一个jupyter配置文件:

cd ~/.jupyter

jupyter notebook --generate-config

然后在所创建的配置文件中将c.NotebookApp.token参数设置为空字符串:

c.NotebookApp.token = ''

正如评论中提到的,将其设置为空字符串会完全禁用身份验证,这是不建议的。


4

对于我来说,上述解决方案在Docker中不适用。

以下解决方案在Linux环境下非常有效:

详细步骤:

  • 使用镜像:tensorflow/tensorflow:latest-py3-jupyter
  • 我配置的密码为:'password'
  • 以用户身份运行Jupyter(而不是root用户)

在Docker中使用预定义密码启动Jupyter的步骤:

  1. export JUPYTER_TOKEN='password'
  2. docker run -it --rm -p 8888:8888 -u $(id -u ${USER}):$(id -g ${USER}) -e JUPYTER_TOKEN=$JUPYTER_TOKEN -v /home/<user>/jupyter:/tf/ tensorflow/tensorflow:latest-py3-jupyter
  3. 打开http://localhost:8888,使用'password'作为密码
  4. 在浏览器中保存密码

对于我来说,这是最简单的方法,可以摆脱令人讨厌的令牌验证。


4
我通过在命令行上运行jupyter notebook password,并将其替换到jupyter_notebook_config.json中的密码,成功解决了令牌身份验证问题。
(base) C:\WINDOWS\system32>jupyter notebook list
http://localhost:8888/ :: C:\Users\320089053
http://localhost:8889/ :: C:\Users\320089053

(base) C:\WINDOWS\system32>jupyter notebook list
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to C:\Users\320089053\.jupyter\jupyter_notebook_config.json

4
如果您正在尝试从Docker中运行,有不同的方法可以禁用身份验证。如果只是简单的docker run命令,我们可以尝试这样做。
docker run  -p 8888:8888 jupyter/minimal-notebook:57f8546c0386 start-notebook.sh --NotebookApp.token=''

如果我们正在使用 jupyter/minimal-notebook 作为基础镜像来构建镜像,
  1. 创建一个custom-start.sh文件

jupyter notebook --allow-root --no-browser --ip=0.0.0.0 --port=8888 --NotebookApp.token='' --NotebookApp.password=''

  1. 在Dockerfile中添加以下行

COPY custom-start.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/custom-start.sh CMD ["custom-start.sh"]

如果是docker-compose文件:

version: '3.8'
services:
  some_name:
    ports: ['8888:8888']  # docker run -p option
    image: jupyter/minimal-notebook:57f8546c0386
    command: start-notebook.sh --NotebookApp.token=''

4
自从 Jupyter Notebook 包的最后更新以来,我的电脑也出现了同样的问题。安装版本之后问题得到了解决。
jupyter-notebook-4.3.0-1-any.pkg.tar.xz

它提示我输入一个从未设置过的密码。进行降级
jupyter-notebook-4.2.3-1-any.pkg.tar.xz

对我来说,保持系统的生产环境很有帮助。当然,这只是一个快速修补。

我也想知道密码是在哪里设置的,因为我的.jupyter文件夹中没有明确的配置文件。我自己设置了一个:

password_required=False

没有任何区别。


3

首先打开Anaconda Prompt,然后输入以下命令:

jupyter notebook --generate-config

在我的情况下,该文件存储在以下目录中(Jupyter Notebook服务器配置目录)。

C:\Users\<username>\.jupyter

在这个目录中,编辑jupyter_notebook_config.py文件以更改以下配置设置:

c.NotebookApp.token = ''
c.NotebookApp.password = u''
c.NotebookApp.open_browser = True
c.NotebookApp.ip = 'localhost'

保存文件,然后重新启动Jupyter Notebook服务器以应用配置更改。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接