CentOS7.6 设置Jupyter Lab(anaconda)远程访问,以系统服务运行

安装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.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值