Jupyter Notebook设置密码验证

本文介绍了如何在Jupyter Notebook中切换身份验证方式,从默认的token验证到设置密码登录。详细步骤包括查看当前token、设置密码、查看密码配置以及取消密码登录的方法。设置密码后,用户可以通过输入密码而非token来访问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 使用基于 Token 的认证机制来保护其服务。然而,在某些场景下可能更倾向于通过密码方式进行身份验证。以下是实现这一功能的方法。 #### 修改配置文件以启用密码认证 为了使 Jupyter Notebook 支持密码登录,需要生成一个哈希后的密码并将其写入到 `jupyter_notebook_config.py` 文件中。具体操作如下: 1. **生成密码哈希** 可以利用 Python 脚本生成安全的密码哈希值。运行以下命令: ```python from notebook.auth import passwd hashed_password = passwd() print(hashed_password) ``` 这段脚本会提示输入两次密码,并返回一个加密后的字符串作为哈希值[^4]。 2. **编辑配置文件** 找到或创建 Jupyter Notebook 的配置文件 `jupyter_notebook_config.py`。如果尚未存在该文件,则可以通过执行以下命令生成: ```bash jupyter notebook --generate-config ``` 接下来,将生成的哈希密码添加至配置文件中的对应字段。例如: ```python c.NotebookApp.password = 'sha1:<your_hashed_password>' ``` 3. **禁用 Token 认证** 同样在 `jupyter_notebook_config.py` 文件中,设置以下参数以关闭 Token 功能: ```python c.NotebookApp.token = '' c.NotebookApp.open_browser = False ``` 此外,可以指定绑定 IP 地址以及端口号以便更好地控制访问范围: ```python c.NotebookApp.ip = '0.0.0.0' c.NotebookApp.port = 8888 ``` #### 安全注意事项 尽管上述方法实现了基于密码的身份验证,但仍需注意安全性问题。建议采取额外措施加强防护,比如仅允许本地网络内的设备连接或者部署 SSL/TLS 加密传输协议[^5]。 ```python from notebook.auth import passwd hashed_password = passwd() print(hashed_password) # 输出用于配置文件中的密码哈希值 ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值