jupyter notebook远程连接

参考博客:https://blog.csdn.net/u014636245/article/details/83652126

在安装完成jupyter后,很多时候需要从远端登录notebook来进行调试使用,这时候就需要将它设置为一个notebook server,从而实现远端访问。

总共分为四步

  • 生成配置文件
  • 设置密码
  • 修改配置文件
  • 端口映射

一、生成配置文件jupyter_notebook_config.py

使用下面的jupyter命令

$ jupyter notebook --generate-config

此时会得到一个配置文件,其默认路径一般如下所示:

Windows: C:\Users\USERNAME\.jupyter\jupyter_notebook_config.py
OS X: /Users/USERNAME/.jupyter/jupyter_notebook_config.py
Linux: /home/USERNAME/.jupyter/jupyter_notebook_config.py

二、设置登录密码

  • 自动设置(推荐)
    在jupyter5.0以后的版本,可以使用jupyter notebook password来设置密码:
$ jupyter notebook password
Enter password:  yourcode  #输入密码
Verify password: yourcodeagain   #再次输入密码确认
#运行后结果
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json    #密码被保存的位置 ~/.jupyter/jupyter_notebook_config.json
  • 手动设置
#利用Ipython工具来设置密码
$ ipython
#进入ipython环境
In [1]: from notebook.auth import passwd    #导入授权模块设置密码
In [2]: passwd()
Enter password:    yourcode          #输入密码
Verify password:    yourcodeagain    #再次输入密码
Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'    #这是一串密码的哈希值
#将在~/.jupyter/jupyter_notebook_config.py配置文件中设置,需要保存好

#----------------advance-----------------#
#可选
$ jupyter notebook --certfile=mycert.pem --keyfile mykey.key
#用来设置安全通信协议
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem
#授权签名

三、修改配置文件

为了能在远程访问jupyter,需要修改刚刚生成的配置文件~/.jupyter/jupyter_notebook_config.py

  • 对于自动模式
    打开配置文件后修改三个地方:
#把前面的#去掉
c.NotebookApp.ip = '*'    #允许所有ip访问  补充:报错 No address associated with hostname可设置为:'0.0.0.0'
c.NotebookApp.open_browser = False    #不打开浏览器
c.NotebookApp.port = 8888             #端口为8888,可以自定义,比如8002
c.NotebookApp.allow_remote_access = True
  • 对于手动方法,除了上述修改之外,还需要配置哈希秘钥:
#配置刚刚生成的秘钥,一长串哈希码
c.NotebookApp.password = u'sha1:bcd259ccf...<your hashed password here>'

#----------------------advanced--------------------------------#
#选配认证和授权
c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
c.NotebookApp.keyfile = u'/absolute/path/to/your/certificate/mykey.key'

四、端口映射

ssh 远端服务器地址 -L 8888:localhost:8888      # 8888→8002....

然后本地浏览器输入http://远端服务器地址:8888就可以实现远程访问了。

发布了18 篇原创文章 · 获赞 0 · 访问量 1757

猜你喜欢

转载自blog.csdn.net/weixin_44159487/article/details/103361277