繁体   English   中英

aws ec2 ssh 错误:`ssh:连接到主机 x 端口 22:操作超时`

[英]aws ec2 ssh error: `ssh: connect to host x port 22: Operation timed out`

我正在尝试通过 SSH 连接到 EC2 实例。 当我尝试连接到实例时,使用ssh -i "test.pem" ec2-user@xx.xx.xx.xx我收到错误: ssh: connect to host xx.xx.xx.xx port 22: Operation timed out

EC2 实例配置

平台:亚马逊Linux

AMI:ami-0841edc20334f9287

实例类型:t2.micro

公共 IPv4:关联的弹性 IPv4

EC2 实例关联的 VPC 子网配置:

路由表:(目的地 - 目标)

10.0.0.0/24 - local

0.0.0.0/0 - igw-...

ACL:(类型 - 协议 - 端口范围 - 源)

入站

SSH - TCP - 22 - 0.0.0.0/0

出境

HTTPS - TCP - 443 - 0.0.0.0/0

安全组:(类型 - 协议 - 端口范围 - 源)

入站

SSH - TCP - 22 - xx.x.xxx.xxx/xx (My IP)

出境

HTTPS - TCP - 443 - 0.0.0.0/0

尝试:

  1. 按照这篇 AWS 文章(解决方案 #3),他们建议将此脚本添加到实例的用户数据中:
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type:
    text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
iptables -F
service sshd restart
--//

这导致了同样的错误: ssh: connect to host public.ip port 22: Operation timed out

  1. 更改 ssh 用户:

ssh -i "test.pem" ec2-user@public.ip

ssh -i "test.pem" root@public.ip

这导致了同样的错误: ssh: connect to host public.ip port 22: Operation timed out

  1. 我尝试在同一个 VPC 中使用我的公共子网,并且能够通过 ssh 连接到 ec2。 在比较公共子网和私有子网时,唯一的区别是公共子网的 NACL 允许所有流量入站流量。

  2. 即使在 10 分钟左右后,在控制台上使用 EC2 Instance Connect 进行连接也会导致终端窗口空白。

您的 NACL 具有 ssh 的入站规则。 您还应该在出站 NACL 规则中允许返回 ssh 流量。 因为与安全组不同,NACL 是无状态的。

除此之外,您还应该允许入站和出站 NACL 规则的临时端口范围。 临时端口是范围在 1024 和 65535 之间的随机端口,发起请求的客户端选择作为源端口。

NACL 临时端口

尝试在您的 aws 控制台中运行Reachability Analyzer

AWS 控制台 >> VPC >> 网络分析 >> 可达性分析器

您可以选择分析器的源作为 Internet 网关,选择目标作为您的 ec2 实例。 运行分析器后,它会告诉您实例是否可以从您的源(在这种情况下为 IGW)访问,有趣的是,它还会为您提供无法访问的原因,这将有助于进一步调试原因.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM