(推荐阅读时间,2min)
Jupyter Notebook和Jupyter Lab是机器学习和深度学习常用工具,本地安装Jupyter和运行非常方便,这里一笔带过。
如果你安装anaconda或者miniconda,直接就自带Jupyter,在命令行里输入jupyter notebook,就启动了服务。
如果没有输入命令:
pip install jupyter 或 pip3 install jupyter 进行安装。
这里主要提如何在服务器端配置远程Jupyter服务,以便在各个地方都能访问。
1. 首先生成config文件
先检查下configure文件是否存在,一般默认在
• 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
如果没有,
$ jupyter notebook —generate-config
Writing default config to: /home/tiger/.jupyter/jupyter_notebook_config.py
2.修改config文件
vim /root/.jupyter/jupyter_notebook_config.py
你的未必在/root/下,推荐~/.jupyter/jupyter_notebook_config.py, 进去之后,配置文件很长,需要耐心找到两个重要的配置:
或者搜索内容,搜索内容的快捷键。按住“/”+内容
例如 ‘/.ip’很快会定位到c.NotebookApp.ip。注意不能是insert模式,否则插入一个’/’字符串。
修改允许远程访问:
## The IP address the notebook server will listen on.
c.NotebookApp.ip = ‘*'
c.NotebookApp.allow_remote_access = True
修改端口,可选:
port:默认8888,建议你该为8899,9999这样防止与本地jupyter重复。
把’localhost’改为‘*',则远程通过IP地址也能访问。
把’localhost’改为’xxx.xxx’,你的ip地址,则远程通过IP地址也能访问。
## The port the notebook server will listen on.
c.NotebookApp.port = 8899
另外可以考虑改的,是否允许根用户。以及是否打开浏览器,由于远程服务器启动Jupyter并不需要打开浏览器,所以此处可以设置False。
## Whether to allow the user to run the notebook as root.
#c.NotebookApp.allow_root = False
#c.NotebookApp.open_browser = True 改为
c.NotebookApp.open_browser = False
默认不允许根用户,打开浏览器,你也可以在后面以参数的形式给出,如下:
jupyter notebook --no-browser —allow-root
3.访问。
输入jupyter notebook命令,在在本地浏览器里输入这个服务器的ip地址,和jupyter监听的端口,便可以访问到远程服务器的Jupyter了,如下。
http://xxx.xxx.xxx.xxx:8899
4.设置密码:
如果你想给Jupyter设置密码,防止每次都要输入长长的token,我推荐使用:jupyter-notebook password
[xxx]$ jupyter-notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /home/tiger/.jupyter/jupyter_notebook_config.json
这样就设置了密码. 再次访问远程Jupyter服务器时,发现需要密码,输入刚刚的密码即可。
当然网上很多也有使用ipython的做法,稍微多了几步,既然装了anaconda肯定有ipython了,直接输入命令ipython,把这个密文’sha1:xxxx’设置到jupyter config里。
In [1]: from IPython.lib import passwd
...: password = passwd("secret")
...: password
Out[1]: 'sha1:5141e7c77e59:6994fae81098c2a208287ea0876f7a14eea7d3b8'