这篇笔记的操作主要参考这篇博主的文章:https://blog.csdn.net/qq_33362102/article/details/109321066
在这里我的远程服务器是CentOS7,本地计算机是Windows7系统。
首先在Linux服务器上安装Jupyter notebook。前提是已经安装好了Anaconda3:
用pip进行安装
pip install Jupyter
生成Jupyter notebook 配置文件
jupyter notebook --generate-config
配置Jupyter notebook密码
$jupyter notebook password
Enter password: ****(自定义)
Verify password: ****
新版的Jupyter notebook只需要输入密码确认,然后它会自动帮你把生成含有密码的hash码输入进jupyter_notebook_config.json 文件。
还有一种操作是用ipython交互式shell来完成密码设置:
ipython
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'argon2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
In [3]: exit
然后将生成的hash存入jupyter_notebook_config.py文件最后:
c.NotebookApp.password = u'sha1:ceXXX‘
配置 jupyter_notebook_config.py 文件
vim ~/.jupyter/jupyter_notebook_config.py
在最后一行后加入如下配置信息:
c.NotebookApp.allow_remote_access = True
c.NotebookApp.open_browser = False
c.NotebookApp.ip = '*'
c.NotebookApp.allow_root = True
c.NotebookApp.port = 8888 #端口可以更改
如果想在特定文件夹打开则可加入一下行:
c.NotebookApp.notebook_dir = ''
保存退出vim即可。
在完成服务器端设置后,首先在服务器上启动jupyter notebook:
jupyter notebook --no-browser --port=8889 --ip=127.0.0.1
这样启动jupyter之后会在服务器前台占据一个终端窗口,最好可以将它转为后台运行:
nohup jupyter notebook --no-browser --port=8889 --ip=127.0.0.1 &
然后在本地的cmd窗口打以下命令:
ssh -N -f -L localhost:8888:localhost:8889 -p 22 remote_user@remote_host
按照提示输入服务器密码即可。这里的-L参数为隧道转发,将本地机(客户机)的某个端口转发到远端指定机器的指定端口,其语法为:
-L host:port:host:port
因此这里的localhost可以直接写localhost,也可以写localhost的默认IP:127.0.0.1,8888端口为本地端口,即接下来用来在本地打开jupyter的端口,也是jupyter_notebook_config.py配置文件里设置的端口,8889为服务器开的端口,即服务器启动时设置的端口,不要搞混淆了。
-p参数后面的端口为ssh的连接端口,默认是22,如果服务器的ssh端口更改过,则需要填写更改的端口号。
接下来在本地浏览器网址栏输入http://127.0.0.1:8888,然后就可以看到jupyter-notebook登录界面了。输入设置好的密码即可。
由于我使用的是Win7系统,cmd没有自带ssh命令,因此需要安装OpenSSH软件来是cmd可以运行ssh命令,这一部分可以参考一下这篇文章:
https://blog.csdn.net/jerrygaoling/article/details/115308184