【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

时间:2023-03-09 00:53:28
【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

问题描述

在Azure的VM中已经安装好Jupyter,并且通过jupyter notebook --port 9999 已经启动,但是通过本机浏览器,访问VM的公网IP,则始终是不能访问的错误。(This site can’t be reached)

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

问题分析

通过对端口9999进行tcping,结果发现该端口没有响应。而通过putty连接到Linux中,使用netstat -an命令查看内部是否已经开始监听端口。发现已经监听VM本地的IP及端口为:127.0.0.1:9999。 而问题就出现在127.0.0.1中,在虚拟机中,127.0.0.1的监听是错误的,应该配置为0.0.0.0 , 所以这里需要在启动jupyter notebook的命令中添加--ip参数。 完整命令为:

jupyter notebook --port 9999 --ip 0.0.0.0 

启动后使用提示的URL连接(需要用VM的公网IP替换localhost)

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

在浏览器中访问的效果如下(jupyter 页面启动成功):

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

使用 netstat -an 监听的结果为:

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

注意事项

1) 创建VM时候,为了快速准备好环境,可以使用已经安装好jupyter的镜像文件。如:Data Science Virtual Machine - Ubuntu 18.04

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

2) 在VM门户Network设置中,为需要的端口开启入站允许。如:9999

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

3) 如需要验证端口是否可以正常ping,可以使用tcping.exe小工具(默认windows中没有,需要下载此文件。下载地址:https://elifulkerson.com/projects/tcping.php

tcping 40.73.58.137 8888

【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)

参考资料

https://discuss.gluon.ai/t/topic/2140/22

screen jupyter notebook --ip=0.0.0.0
然后你用浏览器进入你的服务器 IP:8888 就可以连上服务器

https://www.it-swarm.dev/zh/jupyter-notebook/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E6%97%A0%E6%B3%95%E8%AE%BF%E9%97%AE%E8%BF%9C%E7%A8%8Bjupyter-notebook%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%9F/831623850/

您是否已将jupyter_notebook_config.py文件配置为允许外部连接?

默认情况下,Jupyter Notebook仅接受来自本地主机的连接(例如,从其运行所在的同一台计算机)。通过将NotebookApp.allow_Origin选项从默认的''修改为'*',您可以从外部访问Jupyter。

c.NotebookApp.allow_Origin = '*' #allow all origins

您还需要更改笔记本将侦听的IP:

c.NotebookApp.ip = '0.0.0.0' # listen on all IPs