Linux 遠程Ipython Notebook
Ipython Notebook現在已經改名為Ipython jupyter,是最知名最好用的python資料分析工具。
下面講講怎麼在linux下安裝ipython jupyter,以及遠端存取,我這裡是在虛擬機器中配置ipython,windows訪問虛擬機器中的ipython jupyter。 1.安裝anaconda
anaconda是目前python資料分析最好用的發行版,整合了很多常用的資料分析模組,如果是自己安裝python環境,坑很多的。
在linux下安裝也很簡單,將anaconda.sh上傳到linux後,執行 bash anaconda.sh,根據提示安裝即可,最後一部是詢問是否將python加入環境變數,選擇yes,當然你也可以在安裝完後手動添加到bash中。
下面的安裝完後的.bashrc添加的python環境變數:
# added by Anaconda3 4.2.0 installerexport PATH="/home/zhenyu/anaconda3/bin:$PATH"
2.配置ipython jupyter
2.1.組建組態檔案
# 組建組態檔案jupyter notebook --generate-config# 此時組建組態檔案:# Writing default config to: /home/zhenyu/.jupyter/jupyter_notebook_config.py# 建立登入密碼# 開啟ipython,產生密鑰$ ipythonfrom notebook.auth import passwdpasswd()Enter password:Verify password:Out[2]: 'sha1:6f6193fcfbd5:614c4ba185334868fc8bbce2e9890b3ef7d1a79b' # 我這裡建立的密碼是123456,對應的密鑰是sha1xxxx的那一串# 然後退出ipython
2.2.建立自簽名的認證
使用openssl建立一個自我簽署憑證,由於是自簽名所以瀏覽器會提示警告,選擇信任exception即可。如果不想引起警告,需具備合格證compliant certificate,參考[http://arstechnica.com/security/2009/12/how-to-get-set-with-a-secure-sertificate-for-free/]
如果是內網訪問不擔心安全問題,不使用ssl速度會快一些。
# # 在linux下執行,遇到詢問的地方一路斷行符號即可openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem# 會在當前檔案夾下產生 mycert.pem,我將它移到.jupyter/secret檔案夾下面,方便管理# 先建立.secret檔案夾cd .jupytermkdir secret # 移動cd ~mv mycert.pem .jupyter/secret/
2.3.修改設定檔
# 開啟剛才建立的.jupyter/jupyter_notebook_config.py,先備份源檔案,然後再修改# 備份$ cp .jupyter/jupyter_notebook_config.py .jupyter/jupyter_notebook_config.py_bak# 修改如下,可以先刪除裡面的內容添加,也可以修改,或者直接在頭部添加,反正裡面的原先的內容都是注釋掉的:vi /home/zhenyu/.jupyter/jupyter_notebook_config.pyc = get_config()# Kernel configc.IPKernelApp.pylab = 'inline' # if you want plotting support alwaysc.NotebookApp.ip = '*' # 就是設定所有ip皆可訪問,在144行c.NotebookApp.open_browser = False # 禁止自動開啟瀏覽器# 密鑰,在194行。該密鑰就是2.1步產生的c.NotebookApp.password = 'sha1:74d233d59da1:50d7ef60a58456e2016dc427547fb42cdd971cea'c.NotebookApp.port = 6789 # 訪問連接埠,在197行# 自我簽署憑證位置,如果不使用ssl,可以不設定c.NotebookNotary.secret_file = '/home/zhenyu/.jupyter/secret/mycert.pem'c.NotebookApp.keyfile = '/home/zhenyu/.jupyter/.secret/mykey.key'# 設定目錄,存放建立的ipython notebook檔案c.NotebookApp.notebook_dir = '/home/zhenyu/ipython'
3.防火牆開放連接埠
啟動jupyter notebook後,在虛擬機器中開啟瀏覽器可以在訪問ipython jupyter,但是遠程是無法串連的,因為防火牆啊。
# 使用root使用者su# 開放6789連接埠/sbin/iptables -I INPUT -p tcp --dport 6789-j ACCEPT儲存/etc/rc.d/init.d/iptables save重啟服務service iptables restart
4.遠端存取
# 啟動ipython jupyter,不使用ssljupyter notebook# 或者開啟ssl# jupyter notebook --certfile=mycert.pem --keyfile mykey.keyjupyter notebook --certfile=/home/zhenyu/.jupyter/secret/mycert.pem# 輸出,看最後一行,此時jupyter notebook 可以接受任何IP訪問。[I 12:55:05.929 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found[W 12:55:05.960 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.[I 12:55:06.078 NotebookApp] The Jupyter Notebook is running at: http://[all ip addresses on your system]:6789/
此時開啟瀏覽器輸入 http://192.168.138.130:6789/即可訪問虛擬機器中的ipython notebook。 5.日誌和後台進程
上面的啟動方式,會在目前的目錄產生一個記錄檔,我忘了叫上面名字,總之隨著jupyter notebook的運行,記錄檔會越來越大,如果不是很重要,可以設定不記錄日誌,方法是將所有的輸出都重新導向到/dev/null 2>&1 &
此外,上面的啟動方式是啟動一個前台進程,如果ssh串連斷開,jupyter notebook也就失效了,所以需要將jupyter notebook作為一個後台進程啟動,在linux中是nohup命令。
# 不啟動ssl,不記錄日誌輸出,作為後台進程啟動jupyter notebooknohup jupyter notebook >/dev/null 2>&1 &
6.停止jupyter notebook
jupyter notebook作為後台進程啟動後,如果想要停止它,可以先找到進程ID,然後kill。
# 查看進程ps -ef | grep 'jupyter notebook'# 輸出如下,這裡的21983即為進程id,# hadoop 22136 21983 0 09:10 pts/1 00:00:00 grep jupyter notebook# 殺死進程kill -9 21983# 此時瀏覽器無法再串連jupyter notebook了吧。
參考
http://jupyter-notebook.readthedocs.io/en/latest/public_server.html
http://www.cnblogs.com/zhanglianbo/p/6109939.html
http://blog.csdn.net/gavin_john/article/details/53177630
http://jingyan.baidu.com/article/335530daa4707f19cb41c3ef.html