本地用浏览器远程访问服务器上的jupyter notebook或者jupyter lab,并解决启动时的PermissionError错误

大家都知道,对于从事数据科学或者机器学习的人来说,用jupyter做实验简直就是一个神器,但是往往许多算法和模型我们都希望利用远程的linux服务器进行实现和测试
这里介绍一种能用本地的浏览器访问服务器上架设的jupyter的方案,使得我们可以通过本地浏览器使用服务器的计算资源、访问服务器上的文件系统

下面正片开始:

  1. 官网下载anaconda,或者用pyenv下载都行,安装到服务器,个人用户的东西安装不要用sudo权限。(anaconda是一个很方面的python管理和集成包,里面包含一个python版本和对应的一些工具,其中就包括jupyter)
  2. 接下来,通过下面的命令在~/.jupyter/下生成一个jupyter_notebook_config.py配置文件
$ jupyter notebook --generate-config
  1. 这将创建一个文件夹,其中包含一些原始的配置文件。我们跳转到这个文件夹进行一些配置
$ cd ~/.ipython/profile_nbserver/

生成的配置文件,后来用来设置jupyter的配置
4. 启动ipython:(ipython其实就是jupyter的底层shell界面,所以我们装的anaconda里面会自带jupyter,同时也有了ipython)

$ ipython

创建远程连接密码:

In [1]: from IPython.lib import passwd
In [2]: passwd()

运行之后进入一个创建密码hash值的小程序,根据提示输入你想用的安全口令(这是之后在本地浏览器,登陆服务器所需要的密码):

Enterpassword:
Verifypassword:

然后会输出(下面是我的密码的hash值,后段被删除,你的密码得到的结果应该不同,这个hash值要记住,之后要写入配置文件):

Out [2]: sha1:b86e933199ad:a02e9592e59723da722..
  1. 然后退出ipython,开始编辑配置文件,通过vim编辑文件的命令如下:
vim ~/.jupyter/jupyter_notebook_config.py

在配置文件中添加:

# 所有matplotlib的图像都通过iline的方式显示
c.IPKernelApp.pylab='inline'
# 给出刚刚创建的密码的哈希值
c.NotebookApp.password=u'sha1:b86e933199ad:a02e9592e59723da722..'

# 给出运行的端口,ipython默认为8888
c.NotebookApp.port=8888

# 禁止在运行ipython的同时弹出浏览器
c.NotebookApp.open_browser=False

c.NotebookApp.ip='0.0.0.0'
c.NotebookApp.allow_remote_access = True #允许远程机器访问

复制粘贴进去之后,摁esc,然后 ‘:wq’ 保存退出。
7. 配置完毕以后就可以运行ipython notebook或者jupyter lab的服务端了:

$ jupyter notebook 
$ jupyter lab 
  1. 在你的本地浏览器地址栏输入:

ip地址:8888(这里就输入你服务器的IP地址,加上配置的端口号8888)

注意:最好用chrome 浏览器

最后浏览器打开的根路径是之前在服务器上启动jupyter notebook时所在的服务器路径,所以你最好在服务器的用户根目录下启动你配置好的jupyter,然后就和我们使用本地的jupyter一样了。


注意有时候服务器启动jupyter的时候可能报类似下面的这种错误:

PermissionError: [Errno 13] Permission denied: '/Users/JDMac/Library/Jupyter/runtime'

可以通过在服务器配置文件中加下面这条语句解决:

$ export XDG_RUNTIME_DIR="" then start the jupyter $jupyter notebook

配置文件中的最后两项没有正确配置的话,可能出现下面的错误,大多数教程这两项配置都不正确:

(fastai) paperspace@ps1r995v4:~/fastai$ jupyter notebook
[I 10:45:29.673 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret
Traceback (most recent call last):
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/traitlets/traitlets.py”, line 528, in get
value = obj._trait_values[self.name]
KeyError: ‘allow_remote_access’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/notebook/notebookapp.py”, line 869, in _default_allow_remote
addr = ipaddress.ip_address(self.ip)
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/ipaddress.py”, line 54, in ip_address
address)
ValueError: ‘’ does not appear to be an IPv4 or IPv6 address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/paperspace/anaconda3/envs/fastai/bin/jupyter-notebook”, line 11, in 
sys.exit(main())
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/jupyter_core/application.py”, line 266, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/traitlets/config/application.py”, line 657, in launch_instance
app.initialize(argv)
File “”, line 2, in initialize
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/traitlets/config/application.py”, line 87, in catch_config_error
return method(app, *args, **kwargs)
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/notebook/notebookapp.py”, line 1629, in initialize
self.init_webapp()
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/notebook/notebookapp.py”, line 1379, in init_webapp
self.jinja_environment_options,
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/notebook/notebookapp.py”, line 158, in init
default_url, settings_overrides, jinja_env_options)
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/notebook/notebookapp.py”, line 251, in init_settings
allow_remote_access=jupyter_app.allow_remote_access,
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/traitlets/traitlets.py”, line 556, in get
return self.get(obj, cls)
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/traitlets/traitlets.py”, line 535, in get
value = self._validate(obj, dynamic_default())
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/site-packages/notebook/notebookapp.py”, line 872, in _default_allow_remote
for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
File “/home/paperspace/anaconda3/envs/fastai/lib/python3.6/socket.py”, line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

你可能感兴趣的:(Linux系统操作,mac工程指导和环境配置)