1. 程式人生 > >JupyterNotebook配置遠端登入

JupyterNotebook配置遠端登入

jupyter notebook 預設只開啟了本地埠,需要使用jupyter_notebook_config.py配置jupyter伺服器來實現遠端瀏覽器登入

在這裡插入圖片描述
在安裝完成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

Ubuntu 下一般會儲存在~/.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訪問
c.NotebookApp.open_browser = False    #不開啟瀏覽器
c.NotebookApp.port = 8888             #埠為8888
  • 對於手動方法,除了上述修改之外,還需要配置雜湊祕鑰:
#配置剛剛生成的祕鑰,一長串雜湊碼
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'

注意:此時啟動就可以在區域網內的其它電腦上訪問jupyter了,在瀏覽器輸入192.168.1.111::8888(安裝並啟動了jupyter server的電腦ip)就有如下登入介面:
輸入剛才設定的密碼即可。
如果需要在外網訪問,需要設定埠轉發:利用路由器的埠轉發功能或者使用花生殼等內網穿透來實現,將這臺電腦的埠繫結到對應外網的ip的某個埠上。
在這裡插入圖片描述

在這裡插入圖片描述
icon from easyicon


ref:
doc:https://jupyter-notebook.readthedocs.io/en/latest/public_server.html#
簡書:https://www.jianshu.com/p/803d09a0a92e
埠轉發:https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/