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

问题描述

我正在尝试通过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实例配置

平台:Amazon 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 article(解决方案#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. 在控制台上连接EC2实例连接后,即使大约10分钟后,终端窗口仍为空白。

解决方法

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

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

NACL Ephemeral Ports

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...