无法连接AWS EC2实例上的Jupyter笔记本

26

我在AWS EC2实例上运行Ubuntu 14.04 LTS操作系统。 我正在尝试使用Safari从我的MacBookPro连接到Jupyter。 我在安全组中打开了443端口和TCP 8888端口。

ubuntu@ip-10-0-1-62:~$ netstat -a  | grep 8888
tcp        0      0 localhost:8888          *:*                     LISTEN     
tcp        0      0 localhost:8888          localhost:36190         TIME_WAIT  
ubuntu@ip-10-0-1-62:~$ netstat -a  | grep 443
unix  3      [ ]         STREAM     CONNECTED     12443    
ubuntu@ip-10-0-1-62:~$ 

我无法从我的Mac上的Safari浏览器连接到Jupyter。 (我可以通过6006端口连接到Tensorboard)。

ubuntu@ip-10-0-1-62:~$ jupyter notebook
[W 20:40:14.909 NotebookApp] Unrecognized JSON config file version, assuming version 1
[I 20:40:14.921 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret
[I 20:40:15.224 NotebookApp] JupyterLab alpha preview extension loaded from /home/ubuntu/anaconda/lib/python2.7/site-packages/jupyterlab
[I 20:40:15.230 NotebookApp] Serving notebooks from local directory: /home/ubuntu
[I 20:40:15.230 NotebookApp] 0 active kernels 
[I 20:40:15.230 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=c08a71a48c6e159bdbdcc95837c4e2e053349382c681899b
[I 20:40:15.231 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 20:40:15.232 NotebookApp] 

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=c08a71a48c6e159bdbdcc95837c4e2e053349382c681899b

输入图像描述

$ telnet 52.8.16.250 8888
Trying 52.8.16.250...
telnet: connect to address 52.8.16.250: Connection refused
telnet: Unable to connect to remote host
$ telnet 52.8.16.250 6006
Trying 52.8.16.250...
telnet: connect to address 52.8.16.250: Connection refused
telnet: Unable to connect to remote host
$ telnet 52.8.16.250 8080
Trying 52.8.16.250...
telnet: connect to address 52.8.16.250: Connection refused
telnet: Unable to connect to remote host
$ telnet ec2-52-8-16-250.us-west-1.compute.amazonaws.com 8888
Trying 52.8.16.250...
telnet: connect to address 52.8.16.250: Connection refused
telnet: Unable to connect to remote host
$ telnet ec2-52-8-16-250.us-west-1.compute.amazonaws.com 20
Trying 52.8.16.250...

telnet: connect to address 52.8.16.250: Operation timed out
telnet: Unable to connect to remote host

你找到解决方案了吗?我也遇到了同样的问题。我已经在EC2控制台中打开了443和8888端口,但我自己仍然遇到了麻烦。 - Wolf Rendall
6个回答

23

好的,问题已经找到:

编辑你的Jupyter配置文件:

jupyter notebook --generate-config

vim /home/ubuntu/.jupyter/jupyter_notebook_config.py

确保

c.NotebookApp.ip = '*'

同时确保您在 EC2 服务器中打开了相关安全组(例如端口 8888)。

运行 Jupyter:

jupyter notebook

现在您可以通过正确的端口远程访问它


13
jupyter notebook --ip=*开启笔记本对我而言就足够了。 - the-bass
1
可能需要你允许来源。jupyter笔记本--ip=* --allow_origin=*。https://dev59.com/M1gQ5IYBdhLWcg3wYy_y - Anwar
这解决了我的问题。谢谢你。 - Ariel

15

对我来说,ssh隧道在这种情况下非常有效。

ssh -i /path/to/your/AWS/key/file -NL 8157:localhost:8888 user@host

用户和主机取决于您的EC2实例。

之后,您可以浏览到http://localhost:8157/


清洁,无需更改。 - Atheryl

7

enter image description here

我遇到了上述错误,但尝试了以下命令:jupyter notebook --ip=*
这对我起了作用。

星号(*)代表我需要用我的某些URL部分替换它,或者它只是一个星号。 - sogu

5

请确保您的 AWS 实例安全组已按照下面的屏幕截图进行配置: aws 实例入站安全组已配置

如果您确定 AWS 权限已正确配置,请检查您的网络是否阻止出站流量。您可以尝试通过以下方式在 SSH 连接到实例时进行端口隧道:

ssh -i -L 8888:127.0.0.1:8888

然后您可以通过在浏览器中输入 localhost:8888 访问本地 jupyter。


3
这是在Amazon AWS EC2实例上运行Jupyter笔记本或Jupyterlab的完整工作流程(扩展了@alexopoulos7的答案)
  • 启动AWS EC2实例,例如使用免费套餐的T2微型实例进行测试。
  • 将系统磁盘的存储大小增加,从8GB增加到20GB,以获得更多安装软件包的空间。
  • 使用默认安全组这里不需要添加任何内容! 默认应该是:入站SSH,端口22,来源0.0.0.0/0,出站所有流量,所有端口)。
  • 选择默认密钥对创建新密钥对.pem文件)。
  • 将密钥文件保存到本地主目录,例如~/ec2.pem。使用chmod 400 ~/ec2.pem设置适当的文件权限。

enter image description here

  • 获取实例的公共IPv4地址(等待实例实际运行)。
  • 使用ssh -i ec2.pem ec2-user@<您的实例的公共IPv4地址> SSH 到您的实例。如果IP地址是例如18.1.10.199,则命令为ssh -i ec2.pem ec2-user@18.1.10.199
  • 安装 miniconda,使用wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh,然后运行bash Miniconda3-latest-Linux-x86_64.sh。确认所有默认选择项。在提示时输入yes,询问您是否希望安装程序初始化Miniconda。
  • exit 并重新SSH到您的实例以使更改生效。
  • 创建一个 conda 环境,使用conda create -n myenv python=3.8
  • 使用conda activate myenv激活环境。
  • 使用conda install jupyter notebook jupyterlab 安装 jupyter /jupyterlab
  • 使用例如jupyter lab --no-browser启动jupyter/lab。
  • 在本地计算机上设置与 EC2 实例的 SSH 隧道,使用ssh -i ec2.pem -NL 9999:localhost:8888 ec2-user@18.1.10.199。参数:-i 设置密钥文件,-N 只转发端口而不执行远程命令,-L 将连接从本地端口(在我们的示例中为9999)设置为远程主机/端口。
  • 从默认的8888本地使用不同端口具有优势,即您可以同时在本地和远程使用jupyter notebook。
  • 在本地计算机上打开浏览器窗口,输入 localhost:9999 作为 URL,并使用您在启动 jupyter 时在远程 shell 中看到的令牌登录。您应该可以开始了...

PS:别忘了在完成后终止实例。


1
最佳答案。谢谢! - sogu

0
我遇到了类似的问题,我使用Anaconda安装了Jupyter。
问题在于笔记本服务器没有监听公共EC2地址,只有本地地址。
我使用以下命令解决了这个问题。
jupyter notebook --ip=<public ipv4 DNS>

这里:

--ip=<Unicode> (NotebookApp.ip)
    Default: 'localhost'
    The IP address the notebook server will listen on.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接