参考博客:1.《Pycharm远程Jupyter》
2.《在远程使用jupyter时 设置密码报错No module named ‘notebook.auth‘》
3.《Windows10远程访问Ubuntu服务器上的Jupyter Notebook解决办法》
1.服务器上面按照jupyer模块
pip install jupyter
from jupyter_server.auth import passwd
2.服务器生成配置文件
jupyter notebook --generate-config
3.服务器生成jupyter notebook的token
使用ipython进入交互式python命令行
ipython
输入对应的代码:
from jupyter_server.auth import passwd
#passwd()别用,会生成argon2:$argon2id$v=19$m=10240,t=...类型的token,最后一步会报错Invalid credentials
passwd(algorithm='sha1')#(下面的图片我没改,记得自己要该)
然后设置密码,就可以得到token了
4.编辑Jupyter Notebook配置文件 jupyter_notebook_config.py(第2步有它的路径)
$ vim /home/username/.jupyter/jupyter_notebook_config.py
#配置ip
c.NotebookApp.ip = '*'
# 配置之前生成的hash密码
c.NotebookApp.password = u'sha1:d6ee7d31ec9d:ef3df400549e...<your hashed password here>' #
# 配置是否自动打开服务器浏览器
c.NotebookApp.open_browser = False
# 配置用于远程访问的端口号 # It is a good idea to set a known, fixed port for server access
c.NotebookApp.port = 9999
#配置jupyter的根目录(这个是根据自己的情况来配置的,没需求不建议修改)
#c.ServerApp.notebook_dir = '/home2/'
以上要是找不到c.NotebookApp,就用c.ServerApp
5.服务器启动jupyter-notebook并挂在后台
#先用这个命令确定一下jupyter是否正常运行
jupyter notebook --allow-root
此时,理论上你就可以通过服务器ip+你设置的端口号ip:port
访问到你的远程jupyter了,不需要用nginx再代理一遍了(我就是,以为需要用nginx代理一下,浪费了不少时间在nginx上面)
#如果一切正常,退出上面的命令,然后使用下面的命令,将jupyter挂在后台
nohup jupyter notebook --allow-root > jupyter.log 2>&1 &
这样就不用担心服务器断开连接,jupyter就用不了了
(命令:ps aux | grep jupyter
可以查看是不是有jupyter程序在运行,命令:kill <项目PID>
可以关闭挂在后台的jupyter)
6.本地pycharm设置jupyter访问(后面的步骤来自参考博客,我不知道为什么一直会出错)
选择另外一个configure server
填写内容(注意:token就是之前我们在服务上面获得的,服务器ip就是你的远程服务器,端口号可以在服务器上面使用jupyter-notebook --allow-root
试着显式启动一下jupyter就可以看到了 ):
http://192.168.0.16:8888/?token=b0ec9d44067b:48a6149c7013c4b66cac
如果拼接了密钥还是不行,可以试试不需要加入密钥