Jupyter-notebook 远程访问设置

默认安装好 jupyter-notebook 后,打开访问localhost:8888即可,但是若想远程访问需要重新配置。

1. 生成一个 notebook 配置文件

默认情况下,配置文件 ~/.jupyter/jupyter_notebook_config.py 并不存在,需要自行创建。使用下列命令生成配置文件:

jupyter notebook --generate-config

如果是 root 用户执行上面的命令,则需要加上 --allow-root

jupyter notebook --generate-config --allow-config

执行成功后,会出现下面的信息:

Writing default config to: /root/.jupyter/jupyter_notebook_config.py

2. 生成密码

  • 自动生成
    Jupyter notebook 5.0版本开始就可以自动设置密码了:
jupyter notebook password
  • 手动生成
    打开ipython
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: '***********'

Out出的那一串就是需要在 jupyter_notebook_config.py中添加的密码:

c.NotebookApp.password = u'***********'

3. 修改配置文件

jupyter_notebook_config.py中找到下面的行,取消注释并修改:

c.NotebookApp.ip='*'
c.NotebookApp.password = u'***********'
c.NotebookApp.open_browser = False #仅开启服务,不打开浏览器
c.NotebookApp.port =8888 #可自行指定一个端口, 访问时使用该端口
  • 设置完成后,服务器启动jupyter notebook,用户访问 IP:端口号即可。

注:

  1. root 用户使用 jupyter notebook --allow-root打开jupyter notebook。
  2. 不能在隐藏目录 (以 . 开头的目录)下启动 jupyter notebook, 否则无法正常访问文件。
  3. 安全起见,建议小白可先进行ufw的配置之后再进行Jupyter-notebook的远程访问设置
最后修改:2023 年 08 月 26 日
如果觉得我的文章对你有用,请随意赞赏