Anaconda------配置jupyter

jupyter的功能和作用无需多讲,根据Anaconda------环境管理中创建的python36,在这里演示一下如何在这个python36中配置jupyter并且在宿主机中访问到。

环境:
虚拟机IP:192.168.43.61
宿主机IP:192.168.43.192
操作系统:centos-release-7-2.1511.el7.centos.2.10.x86_64
虚拟机:VirtualBox 6.1.24
Anaconda版本:Anaconda3-5.2.0-Linux-x86_64

1.进入python36环境

[root@localhost home]# conda activate python36

2.生成配置文件

(python36) [root@localhost home]# jupyter notebook --generate-config --allow-root
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

提示创建的配置文件是/root/.jupyter/jupyter_notebook_config.py
 

3.创建密码
 

打开ipython,生成sha1的密码

(python36) [root@localhost home]# ipython
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from notebook.auth import passwd

In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:dcf803292d2d:fb68c58cb21a9e4a062dd4374cc0b7f4fa17d7df'

In [3]: quit()
(python36) [root@localhost home]# 

需要记住自己输入的密码,这里只会显示加密后的信息,我用的密码是123456.测试呗,无所谓
 

4.生成一个自签名认证的key
 

(python36) [root@localhost home]# openssl req -x509 -nodes -days 365 -newkey rsa:4096 -keyout jkey.key -out jcert.pem
Generating a RSA private key
................................................................++++
....................++++
writing new private key to 'jkey.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:BJ
Locality Name (eg, city) []:bj
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
(python36) [root@localhost home]# 

5.修改自动生成的配置文件 

vi /root/.jupyter/jupyter_notebook_config.py

 配置内容如下

#就是刚才passwd()指令生成的密钥复制过来
c.NotebookApp.password = 'sha1:dcf803292d2d:fb68c58cb21a9e4a062dd4374cc0b7f4fa17d7df'
#服务运行的端口,我这里指定的是8881
c.NotebookApp.port = 8881

#修改为0.0.0.0可以任意远程IP访问
c.NotebookApp.ip = '0.0.0.0'

#默认不打开浏览器
c.NotebookApp.open_browser = False 
c.NotebookApp.certfile = '/home/conda_udf_data/jupyter/certification/jcert.pem'
c.NotebookApp.keyfile = '/home/conda_udf_data/jupyter/certification/jkey.key'

 切记:上面的参数值一定要放在单引号里面
 jcert.pem和jkey.key是两个认证文件。
因为上面第2步是在\home下执行的,因此创建的认证文件也是在\home下面,其实这样这样不大安全,应该是事先创建好妥当的目录然后再创建认证文件,现在认证文件已经创建好了,我只能是在/home下重新创建了conda_udf_data/jupyter/certification然后把这俩认证文件移入到这个新的目录中,因此上面的c.NotebookApp.certfile 和 c.NotebookApp.keyfile 的文件路径发生了变化。

6.启动校验

jupyter notebook --allow-root 

可以看到jupyter已经启动成功,在宿主机上访问 https://192.168.43.61:8881/,可以打开jupyter WEB UI

 可以从界面上看到这里有3个目录,再来看看\home目录下的情况:
 

(python36) [root@bogon home]# pwd
/home
(python36) [root@bogon home]# ll
total 0
drwxr-xr-x. 2 root root 44 Oct 13 04:44 backup
drwxr-xr-x. 3 root root 20 Oct 14 01:45 conda_udf_data
drwxr-xr-x. 2 root root  6 Oct 14 01:45 jupyter
(python36) [root@bogon home]# 

 可以看到home目录下也是这三个目录,因此可以确定一个事实:在哪个目录下启动了jupyter,那么启动的web UI 上就可以显示哪个个目录下的内容,为了避免保留一些敏感信息,还是建议创建好一个空的目录,比如/home/conda_udf_data/jupyter/jupyer_root,然后再来启动jupyter,最好的办法是在/root/.jupyter/jupyter_notebook_config.py里面设置好工作目录:


c.NotebookApp.notebook_dir = '/home/conda_udf_data/jupyter/jupyer_root'

那么完整的设置是这样:
 

#就是刚才passwd()指令生成的密钥复制过来
c.NotebookApp.password = 'sha1:dcf803292d2d:fb68c58cb21a9e4a062dd4374cc0b7f4fa17d7df'

#服务运行的端口,我这里指定的是8881
c.NotebookApp.port = 8881

#修改为0.0.0.0可以任意远程IP访问
c.NotebookApp.ip = '0.0.0.0'

#默认不打开浏览器
c.NotebookApp.open_browser = False 
c.NotebookApp.certfile = '/home/conda_udf_data/jupyter/certification/jcert.pem'
c.NotebookApp.keyfile = '/home/conda_udf_data/jupyter/certification/jkey.key'

#设置工作路径
c.NotebookApp.notebook_dir = '/home/conda_udf_data/jupyter/jupyer_root'

我先在/home/conda_udf_data/jupyter/jupyer_root下创建一个文件,比如
ip addr >ip_addr.txt
 

[root@bogon jupyer_root]# ll
total 4
-rw-r--r--. 1 root root 783 Oct 14 02:48 ip_addr.txt
[root@bogon jupyer_root]# pwd
/home/conda_udf_data/jupyter/jupyer_root
[root@bogon jupyer_root]# 

这个文件已经存在了,然后返回的到\home后启动jupyter

(python36) [root@bogon jupyter]# cd /home/
(python36) [root@bogon home]# ll
total 0
drwxr-xr-x. 2 root root 44 Oct 13 04:44 backup
drwxr-xr-x. 3 root root 20 Oct 14 01:45 conda_udf_data
drwxr-xr-x. 2 root root  6 Oct 14 01:45 jupyter
(python36) [root@bogon home]# jupyter notebook --allow-root
[I 02:55:12.297 NotebookApp] JupyterLab beta preview extension loaded from /usr/lib64/anaconda3/lib/python3.6/site-packages/jupyterlab
[I 02:55:12.297 NotebookApp] JupyterLab application directory is /usr/lib64/anaconda3/share/jupyter/lab
[I 02:55:12.301 NotebookApp] Serving notebooks from local directory: /home/conda_udf_data/jupyter/jupyer_root
[I 02:55:12.302 NotebookApp] 0 active kernels
[I 02:55:12.302 NotebookApp] The Jupyter Notebook is running at:
[I 02:55:12.302 NotebookApp] https://bogon:8881/
[I 02:55:12.302 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

在宿主机中查看https://192.168.43.61:8881

 可以看到这次启动后看到的根目录就是指定的目录。
OK,大工搞成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值