Jupyter Notebook设置密码验证

Jupyter Notebook当前版本为6.3.0

概述

Jupyter Notebook默认是以token来验证身份的。当在命令行使用jupyter notebook命令启动Jupyter Notebook服务器时,会自动生成一个token用来验证身份。

PS C:\Users\Administrator> jupyter notebook
[I 21:48:21.163 NotebookApp] Serving notebooks from local directory: C:\Users\Administrator
[I 21:48:21.163 NotebookApp] The Jupyter Notebook is running at:
[I 21:48:21.164 NotebookApp] http://localhost:8888/?token=da83de690be95fec7863b97942cc1fadfdaba7ab430809a8
[I 21:48:21.164 NotebookApp]  or http://127.0.0.1:8888/?token=da83de690be95fec7863b97942cc1fadfdaba7ab430809a8
[I 21:48:21.164 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 21:48:21.275 NotebookApp]
    To access the notebook, open this file in a browser:
        file:///C:/Users/Administrator/AppData/Roaming/jupyter/runtime/nbserver-1808-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=da83de690be95fec7863b97942cc1fadfdaba7ab430809a8
     or http://127.0.0.1:8888/?token=da83de690be95fec7863b97942cc1fadfdaba7ab430809a8


当更换浏览器或者注销(logout)之后,再次打开http://localhost:8888/会跳转到http://localhost:8888/login,如下图所示。
通过上面命令行内容或者jupyter notebook list可获取当前token。

PS C:\Users\Administrator> jupyter notebook list
Currently running servers:
http://localhost:8888/?token=da83de690be95fec7863b97942cc1fadfdaba7ab430809a8 :: C:\Users\Administrator

在这里插入图片描述

设置密码

在上面的登录页面中可以在输入Token之后设置密码。
Jupyter Notebook 6.0之后的版本中,使用jupyter notebook password命令设置密码可能更简单。

PS C:\Users\Administrator> jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to C:\Users\Administrator\.jupyter\jupyter_notebook_config.json

在输入密码后,提示经过哈希后的密码保存在C:\Users\Administrator\.jupyter\jupyter_notebook_config.json

jupyter_notebook_config.json文件内容为:

{
  "NotebookApp": {
    "password": "sha1:1fa311682b41:cd33fc2cc572b7df960690bbad8a7f4264198740"
  }
}

设置密码后,再次使用jupyter notebook命令启动Jupyter Notebook服务器时,会出现如下页面提示输入密码。
在这里插入图片描述
再次运行jupyter notebook list可发现当前Jupyter Notebook服务器地址不再显示token。

PS C:\Users\Administrator> jupyter notebook list
Currently running servers:
http://localhost:8888/ :: C:\Users\Administrator

取消密码登录

最简单粗暴的方法就是删掉jupyter_notebook_config.json文件,但是该文件是jupyter的整体配置文件,如果有其他设置的话,删除不太方便。可以单独把"password": "sha1:1fa311682b41:cd33fc2cc572b7df960690bbad8a7f4264198740"密码这个键值对删除。

然后再启动Jupyter Notebook服务器,验证方式就会改回token方式。

### 配置 Jupyter Notebook 使用密码保护 为了使 Jupyter Notebook 启用密码保护,需编辑其配置文件 `jupyter_notebook_config.py` 或者创建一个新的配置文件如果尚未存在。具体操作如下: #### 创建或更新配置文件 首次启用安全措施时可能需要生成新的配置文件,在命令行执行以下指令来完成此过程[^3]: ```bash jupyter notebook --generate-config ``` 该命令会在用户目录下的 `.jupyter/` 文件夹内生成名为 `jupyter_notebook_config.py` 的配置文件。 #### 设置密码 通过 Python 解释器可以方便地设置加密后的密码字符串用于后续配置: ```python from IPython.lib import passwd passwd() ``` 上述代码会提示输入并确认新密码,并返回一串哈希值表示这个密码。将得到的结果妥善保存以便稍后使用。 #### 编辑配置文件加入密码验证机制 打开之前提到过的配置文件路径(通常是 `~/.jupyter/jupyter_notebook_config.py`),找到关于认证的部分并将下面的内容添加进去,注意替换 `<hashed_password>` 成实际获得的哈希值[^4]: ```python c.NotebookApp.password = u'sha1:<hashed_password>' ``` 这样就完成了基本的安全防护设定工作;当再次启动服务端程序 (`jupyter notebook`) 之后访问 Web 页面就会被要求提供用户名和刚才所设的密码来进行身份验证了。 #### 移除现有密码保护 若要移除现有的密码,则可以在配置文件中定位到对应的行将其注释掉或者清空字段内的值即可恢复匿名访问模式。 ```python #c.NotebookApp.password = '' ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值