参考博客: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 远端服务器ip地址 -L 8888:localhost:8888 # 8888→8002....
然后本地浏览器输入http://远端服务器地址:8888就可以实现远程访问了。
五、远程使用jupyter
重新激活环境,输入账号密码
conda activate pytorch
打开jupyter
jupyter notebook
[I 00:18:50.918 NotebookApp] Writing notebook server cookie secret to /home/ly/.local/share/jupyter/runtime/notebook_cookie_secret
[W 00:18:51.356 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 00:18:51.361 NotebookApp] The port 8888 is already in use, trying another port.
[I 00:18:51.362 NotebookApp] Serving notebooks from local directory: /home/ly
[I 00:18:51.362 NotebookApp] Jupyter Notebook 6.4.5 is running at:
[I 00:18:51.363 NotebookApp] http://dgx:8889/
[I 00:18:51.363 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[I 00:19:03.675 NotebookApp] 302 GET / (101.5.224.29) 0.680000ms
[I 00:19:03.791 NotebookApp] 302 GET /tree? (101.5.224.29) 2.430000ms
本地浏览器输入网址:
http://服务器ip地址:8889/
停止运行jupyter
Control-C