菜鸟笔记
提升您的技术认知

centos7.6 设置jupyter lab(anaconda)远程访问,以系统服务运行-ag真人游戏

安装jupyter lab过程不再赘述。

测试

设置所有本机所有ip均可访问,服务端口为8080,设置启动目录为/root/ipynbs

由于当前用户为root,出于安全考虑,jupyter lab需要设置--allow-root才能运行。

(base) [root@ecs-9e76 ~]# jupyter lab --ip=0.0.0.0 --port=8080 --notebook-dir='/root/ipynbs' --allow-root
[i 22:49:45.533 labapp] jupyterlab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab
[i 22:49:45.533 labapp] jupyterlab application directory is /root/anaconda3/share/jupyter/lab
[i 22:49:45.535 labapp] serving notebooks from local directory: /root/ipynbs
[i 22:49:45.535 labapp] the jupyter notebook is running at:
[i 22:49:45.535 labapp] http://ecs-9e76:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
[i 22:49:45.535 labapp]  or http://127.0.0.1:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
[i 22:49:45.535 labapp] use control-c to stop this server and shut down all kernels (twice to skip confirmation).
[w 22:49:45.538 labapp] no web browser found: could not locate runnable browser.
[c 22:49:45.538 labapp] 
    to access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/nbserver-13713-open.html
    or copy and paste one of these urls:
        http://ecs-9e76:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d
     or http://127.0.0.1:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d

访问http://xx.xx.xx.xx:8080/?token=5c5548b668f5eb1a3c188e9f2c047af3e0fccb111686801d即可访问jupyter lab

设置密码访问

jupyter lab默认采用token访问,不够方便,设置采用密码访问。

输入jupyter notebook password,按提示输入密码。

密码文件保存在配置文件/root/.jupyter/jupyter_notebook_config.json中。

(base) [root@ecs-9e76 ~]# jupyter notebook password
enter password: 
verify password: 
[notebookpasswordapp] wrote hashed password to /root/.jupyter/jupyter_notebook_config.json
(base) [root@ecs-9e76 ~]# vi /root/.jupyter/jupyter_notebook_config.json
{
  
  "notebookapp": {
  
    "password": "sha1:d933b9053eb0:55de430361c7686fdc3ce5462af9804637ead074"
  }
}

修改配置文件

前面测试中直接用参数配置ip、端口等信息,为了便于使用,可在配置文件中做相关配置。

jupyter lab的配置文件有两种:jupyter_notebook_config.pyjupyter_notebook_config.json

两个配置文件选择其一即可。

1. 通过jupyter_notebook_config.py文件

  • 生成配置文件jupyter_notebook_config.py
    输入jupyter notebook --generate-config命令,生成jupyter_notebook_config.py文件。
(base) [root@ecs-9e76 ~]# jupyter notebook --generate-config
writing default config to: /root/.jupyter/jupyter_notebook_config.py
  • 修改配置文件
(base) [root@ecs-9e76 ~]# vi  /root/.jupyter/jupyter_notebook_config.py

修改配置文件,确保以下选项为未注释状态:

c.notebookapp.ip = '*' 
c.notebookapp.port = 8080
c.notebookapp.open_browser = false 
c.notebookapp.notebook_dir = '/root/ipynbs' 
c.notebookapp.allow_root = true

2. 通过jupyter_notebook_config.json文件

注意,jupyter_notebook_config.json文件原始内容为:

{
  
  "notebookapp": {
  
    "password": "sha1:d933b9053eb0:55de430361c7686fdc3ce5462af9804637ead074"
  }
}

修改上述配置后,对应的jupyter_notebook_config.json文件内容如下:

(base) [root@ecs-9e76 ~]# vi /root/.jupyter/jupyter_notebook_config.json
{
  
  "notebookapp": {
  
    "password": "sha1:d933b9053eb0:55de430361c7686fdc3ce5462af9804637ead074",
    "ip" : "*" ,
    "port" : 8080,
    "open_browser" : false ,
    "notebook_dir" : "/root/ipynbs" ,
    "allow_root": true
  }
}

运行效果如下:

(base) [root@ecs-9e76 ~]# jupyter lab
[w 00:47:39.022 labapp] warning: the notebook server is listening on all ip addresses and not using encryption. this is not recommended.
[i 00:47:39.028 labapp] jupyterlab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab
[i 00:47:39.028 labapp] jupyterlab application directory is /root/anaconda3/share/jupyter/lab
[i 00:47:39.030 labapp] serving notebooks from local directory: /root/ipynbs
[i 00:47:39.030 labapp] the jupyter notebook is running at:
[i 00:47:39.030 labapp] http://ecs-9e76:8080/
[i 00:47:39.030 labapp] use control-c to stop this server and shut down all kernels (twice to skip confirmation).

以系统服务形式运行jupyter lab

创建/etc/systemd/system/jupyter.service文件,文件内容如下:

[root@ecs-9e76 ~]# vi /etc/systemd/system/jupyter.service
[unit]
description=jupyterlab
after=network.service
[service]
execstart=/root/anaconda3/bin/jupyter lab 
[install]
wantedby=default.target

启动服务:systemctl start jupyter

[root@ecs-9e76 ~]# systemctl start jupyter

开机启动:systemctl enable jupyter

[root@ecs-9e76 ~]# systemctl enable jupyter
created symlink from /etc/systemd/system/default.target.wants/jupyter.service to /etc/systemd/system/jupyter.service.
网站地图