马春杰杰 Exit Reader Mode

[mcj]如何在Windows下远程访问Ubuntu中的Jupyter Notebook

前段时间安装了anaconda,自带了Jupyter Notebook,虽然服务器很好用,但是目前服务器用的是vnc,没有配置中文输入法,所以总是感觉有点别扭~

现在想通过本地电脑(Windows)来远程操作服务器中的notebook,具体步骤如下:

1 生成配置文件

在服务器端输入以下命令来生成配置文件:

jupyter notebook --generate-config

完成之后会得到生成的配置文件

(pytorch) ubuntu@mcj:~/pytorch$ jupyter notebook --generate-config
Writing default config to: /home/ubuntu/.jupyter/jupyter_notebook_config.py

2 设置登陆密码

设置登陆密码一般有两种方式,一种是手动,一种是自动,这里我们采用自动的方式,输入以下指令

jupyter notebook password

然后会让你输入密码,需要输入两遍,输入之后会自动将密码保存。

(pytorch) ubuntu@mcj:~/pytorch$ jupyter notebook password
Enter password: 
Verify password: 
[NotebookPasswordApp] Wrote hashed password to /home/ubuntu/.jupyter/jupyter_notebook_config.json

3 修改配置文件

为了能够在我们本地的电脑访问notebook,需要修改一下配置文件。打开配置文件:

(pytorch) ubuntu@mcj:~/pytorch$ nano /home/ubuntu/.jupyter/jupyter_notebook_config.py

修改以下三处地方:

#把前面的#去掉
c.NotebookApp.ip = '0.0.0.0'    #允许所有ip访问
c.NotebookApp.open_browser = False    #不打开浏览器,注意大小写
c.NotebookApp.port = 8889             #端口为8889

4 打开notebook

好了,现在就可以用了。输入jupyter notebook(本地或者服务器端都可以)

(pytorch) ubuntu@mcj:~/PycharmProjects/pytorch$ jupyter notebook
[W 15:24:52.822 NotebookApp] Collisions detected in /home/ubuntu/.jupyter/jupyter_notebook_config.py and /home/ubuntu/.jupyter/jupyter_notebook_config.json config files. /home/ubuntu/.jupyter/jupyter_notebook_config.json has higher priority: {
      "NotebookApp": {
        "password": "'sha1:2f6327866778:19b18a2a0ed976efdfe82876bfb9a4e8252d742d' ignored, using 'sha1:3fff29fdf67a:862b0e4566a37bd0a3d13dd976c43ee2f9a8182d'"
      }
    }
[I 15:24:53.257 NotebookApp] [nb_conda_kernels] enabled, 1 kernels found
[I 15:24:54.022 NotebookApp] [nb_conda] enabled
[I 15:24:54.022 NotebookApp] Serving notebooks from local directory: /home/ubuntu/PycharmProjects/pytorch
[I 15:24:54.022 NotebookApp] The Jupyter Notebook is running at:
[I 15:24:54.022 NotebookApp] https://(mcj or 127.0.0.1):8889/
[I 15:24:54.022 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

这时,我们在本地浏览器输入:https://服务器IP:8889,即可打开登陆界面,输入第一步的密码即可登陆。

5 错误记录

5.1 socket.gaierror: [Errno -2] Name or service not known

在c.NotebookApp.ip处,如果设置为”*”的话,打开notebook会提示:

Traceback (most recent call last):
  File "/home/ubuntu/.conda/envs/pytorch/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/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/notebook/notebookapp.py", line 864, in _default_allow_remote
    addr = ipaddress.ip_address(self.ip)
  File "/home/ubuntu/.conda/envs/pytorch/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/ubuntu/.conda/envs/pytorch/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/home/ubuntu/.conda/envs/pytorch/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/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "</home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/decorator.py:decorator-gen-7>", line 2, in initialize
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/notebook/notebookapp.py", line 1630, in initialize
    self.init_webapp()
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/notebook/notebookapp.py", line 1378, in init_webapp
    self.jinja_environment_options,
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/notebook/notebookapp.py", line 159, in __init__
    default_url, settings_overrides, jinja_env_options)
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/notebook/notebookapp.py", line 252, in init_settings
    allow_remote_access=jupyter_app.allow_remote_access,
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/traitlets/traitlets.py", line 556, in __get__
    return self.get(obj, cls)
  File "/home/ubuntu/.conda/envs/pytorch/lib/python3.6/site-packages/traitlets/traitlets.py", line 535, in get
    value = self._validate(obj, dynamic_default())
  File "/home/ubuntu/.conda/envs/pytorch/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/ubuntu/.conda/envs/pytorch/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

将*改为0.0.0.0即可。

5.2 连接被拒绝

如果提示连接被拒绝,有可能端口未开放,如何开放端口,请参考下面文章:

[mcj]Ubuntu如何查看已开放端口|Ubuntu开放关闭指定端口方法[https://www.machunjie.com/linux/105.html]

本文最后更新于2023年6月6日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!