JupyterNotebook開啟遠(yuǎn)程訪問

JupyterNotebook在關(guān)閉SSH遠(yuǎn)程連接之后,JupyterNotebook所有服務(wù)將被關(guān)閉,而且默認(rèn)是不開啟遠(yuǎn)程客戶端訪問的。

1 JupyterNotebook開啟遠(yuǎn)程服務(wù)

1.1 建立 JupyterNotebook配置文件

JupyterNotebook默認(rèn)配置文件為:~/.jupyter/jupyter_notebook_config.py,默認(rèn)是不存在的,需要通過命令生成配置文件。

# 非root用戶
jupyter notebook --generate-config

# root用戶
jupyter notebook --generate-config --allow-root

命令執(zhí)行后,出現(xiàn)如下信息表明配置文件已經(jīng)生成。

Writing default config to: /home/zhou/.jupyter/jupyter_notebook_config.py

1.2 生成 JupyterNotebook密碼

(1)從JupyterNotebook5.0開始,生成密碼的命令:

jupyter notebook password
xxx@XXX:~$ jupyter notebook password
Enter password: 
Verify password: 
[NotebookPasswordApp] Wrote hashed password to /home/zhou/.jupyter/jupyter_notebook_config.json

生成的密碼寫入文件:jupyter_notebook_config.json。.
(2)還可以利用命令行模式手動(dòng)生成密碼,在Python命令行中輸入如下命令。

from notebook.auth import passwd;
passwd()
Python 3.7.5 (default, Oct 25 2019, 15:51:11) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from notebook.auth import passwd
>>> passwd()
Enter password: 
Verify password: 
'sha1:c3b2259c3408:7db3d3bfd334c5e29032720e947792925e4d28a6'

手動(dòng)生成的密碼顯示為sha1值。
手動(dòng)生成的密碼需要修改jupyter_notebook_config.py文件中相應(yīng)的選項(xiàng)。

c.NotebookApp.password ='sha1:c3b2259c3408:7db3d3bfd334c5e29032720e947792925e4d28a6'
# 取消這一行前面的注釋符號(#)

注意:jupyter_notebook_config.json生產(chǎn)的密碼優(yōu)先級要高于jupyter_notebook_config.py文件中手動(dòng)設(shè)置的密碼。根據(jù)具體情情況選使用哪種密碼生成方式。

(3) 設(shè)置遠(yuǎn)程連接IP、端口號等

c.NotebookApp.ip='*'  # 允許任何ip訪問
c.NotebookApp.open_browser = False  # 默認(rèn)不打開瀏覽器
c.NotebookApp.port =8888  #可自行指定一個(gè)端口, 訪問時(shí)使用該端口.
# 取消每一行前面的注釋符號(#)

1.3 打開遠(yuǎn)程服務(wù)

在服務(wù)器上啟用jupyter notebook,root用戶使用jupyter notebook --allow-root啟用服務(wù),使用 服務(wù)器IP:8888訪問jupyternotebook

xxx@XXX:~$ jupyter notebook
[I 02:14:52.196 NotebookApp] Writing notebook server cookie secret to /home/zhou/.local/share/jupyter/runtime/notebook_cookie_secret
[I 02:14:52.566 NotebookApp] Serving notebooks from local directory: /home/zhou
[I 02:14:52.566 NotebookApp] The Jupyter Notebook is running at:
[I 02:14:52.566 NotebookApp] http://localhost:8888/
[I 02:14:52.566 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 02:14:52.574 NotebookApp] No web browser found: could not locate runnable browser.

注意:利用jupyter notebook生成的服務(wù)在SSH連接關(guān)閉之后會自動(dòng)關(guān)閉,如果需要在關(guān)閉SSH連接之后還能在后臺運(yùn)行,需要在運(yùn)行jupyter notebook命令后面添加“&”,即jupyter notebook&

1.4 映射JupyterNotebook遠(yuǎn)程8888端口為本機(jī)8888端口

遠(yuǎn)程連接JupyterNotebook服務(wù)器需要每次輸入遠(yuǎn)程服務(wù)器IP地址,映射為本地端口后會方便很多。
在VmwareWorkstation中選擇“編輯”-“虛擬網(wǎng)絡(luò)編輯器”,在其中添加虛擬機(jī)中的端口號和本機(jī)端口號之間的映射。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容