远程remote 01——Ubuntu由ssh远程访问jupyter notebook

Ubuntu由ssh远程访问jupyter notebook

1. Jupyter-notebook configuration file

jupyter notebook --generate-config

Note: 会有以下配置文件生成的提醒:
Writing default config to: /home/computer_name/.jupyter/jupyter_notebook_config.

2. Generate password

使用python,远程电脑里有ipython可以直接输入ipython

$ python
from notebook.auth import passwd
passwd()
# 输入完后会出现以下输入密码,该密码为远程登录jupyter密码
Enter password:******
Verify password:******
# 下面为配置文件的密文
输出为: 'sha1:cd723d3e4b0afg34hasrsf65yr453t32hj456d2627fb'

3. Modify configuration file

用vim或者gedit, 修改第一步生成的配置文件:

vim ~/.jupyter/jupyter_notebook_config.py

修改如下:

# *号表示任何的IP地址都可以访问
c.NotebookApp.ip= '*'                                                             #204 line
# 前面第一步生成的密文
c.NotebookApp.password = u'sha1:cd723d3e4b0afg34hasrsf65yr453t32hj456d2627fb'     #267 line
# 表示在打开jupyter的时候不会自动打开远程的浏览器
c.NotebookApp.open_browser = False                                                #276 line
# 这里可以任意定义一个端口进行连接需要
c.NotebookApp.port = 9999                                                         #288 line

以上行数为修改之前的行数。
Note: 这里打开以后所有的配置是被注释掉的, 所以先复制上面的配置, 然后在另起一行进行修改.如下:

# c.NotebookApp.ip= 'hostname' 
c.NotebookApp.ip= '*' 

4. Launch jupyter-notebook

对不同python环境下的jupyter不熟悉的可以查看我另外一篇的最后一节 Link
1.在远程电脑上的terminal中输入jupyter-notebook, 然后会出现一个警告, 这个没关系, 记住之前设置密文的密码就行.

[W 17:28:47.794 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.

2.root 用户使用

jupyter notebook --allow-root

3.后台执行

nohup jupyter notebook --allow-root &

5. Remote login

在自己的电脑上, 可以直接从本地浏览器直接访问http://address_of_remote:8888就可以看到jupyter的登陆界面.
比如: 远程电脑的ip地址是: 127.0.0.1
你设置的端口号为: 9999
那么浏览器输入: 127.0.0.1:9999 即可.
可以看到如下界面, 然后输入第一步设置的密码即可登录到远程电脑的home界面.
jupyter
之后只需要远程使用轻薄本, 来使用高性能台式机为自己的程序进行测试.

6. Problem

以上设置,测试过没有问题的,但是最近远程电脑重装了以后,按照以上步骤出现的一些错误

~$ jupyter-notebook

Traceback (most recent call last):
  File "/home/zzl/anaconda3/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/zzl/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 864, in _default_allow_remote
    addr = ipaddress.ip_address(self.ip)
  File "/home/zzl/anaconda3/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/zzl/anaconda3/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/home/zzl/anaconda3/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/zzl/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "<decorator-gen-7>", line 2, in initialize
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 1628, in initialize
    self.init_webapp()
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 1378, in init_webapp
    self.jinja_environment_options,
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 159, in __init__
    default_url, settings_overrides, jinja_env_options)
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 252, in init_settings
    allow_remote_access=jupyter_app.allow_remote_access,
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/traitlets/traitlets.py", line 556, in __get__
    return self.get(obj, cls)
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/traitlets/traitlets.py", line 535, in get
    value = self._validate(obj, dynamic_default())
  File "/home/zzl/anaconda3/lib/python3.6/site-packages/notebook/notebookapp.py", line 867, in _default_allow_remote
    for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
  File "/home/zzl/anaconda3/lib/python3.6/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

可以看到最后错误显示是hostname有问题,也就是这个这个出了问题:

# c.NotebookApp.ip= 'hostname' 
c.NotebookApp.ip= '*' 

原来的给任意的ip地址'*'不合适,这里修改为远程电脑的ip地址

c.NotebookApp.ip= 'your_remote_ip' 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章