传统方式 SSH 进 EC2,要么给实例挂公网 IP + 安全组开放 22(暴露面大),要么搭堡垒机(成本和运维负担)。EC2 Instance Connect Endpoint(下称 EIC Endpoint)是 AWS 推出的方案,本质上是一种Interface VPC Endpoint(基于 PrivateLink),让你从本地直接 SSH 到私有子网里的 EC2,全程走 AWS 内网,不碰公网。
它解决了什么问题
- 不开公网 22:实例无需公网 IP,安全组的 22 只对 Endpoint 开放
- 不用堡垒机:少一台机器、少一层跳板
- 不用长期密钥:每次连接 CLI 自动生成临时密钥对,公钥推送到实例元数据,连接结束即失效
原理与流量路径
你的笔记本 │ (AWS API 调用:推送临时公钥 + 建立隧道) ▼ EIC Endpoint ENI ← VPC 内的一个弹性网络接口 │ (从 ENI 发起 SSH 到实例的 22) ▼ EC2 实例 (私有 IP)关键认知:EIC 最终还是要连到实例的 22 端口——它省的是"把 22 暴露到公网",不是省掉 22。所以实例安全组必须放行 22,但只对 Endpoint 开放。
前置条件
AWS CLI v2(SSO 登录需要 v2)
aws--version# aws-cli/2.x.x ...实例侧:已安装
ec2-instance-connect包,sshd 已配置从实例元数据取公钥(AuthorizedKeysCommand)。Amazon Linux 系列默认已装;Ubuntu/Debian 需确认(见文末注意事项)。网络侧:EIC Endpoint 与实例在同一 VPC;实例安全组放行 22 来自 Endpoint。
一、认证配置
EIC 命令走标准 AWS CLI 认证,AK/SK 和 SSO 都支持。
方式 A:IAM Identity Center(SSO)
适合企业账号通过统一身份门户分发访问权限的场景——当对方给你一个xxx.awsapps.com/start登录地址时,走这条路。
aws configure sso按提示填写:
| 提示 | 填什么 |
|---|---|
| SSO session name | 随便起,如myorg |
| SSO start URL | https://d-xxxxxxxxxx.awsapps.com/start |
| SSO region | Identity Center 目录所在区域(见下方注意) |
| SSO registration scopes | 回车选默认sso:account:access |
随后浏览器自动打开,用 SSO 账号登录授权。回到终端后会列出可用的 AWS 账号和角色,选择后给 profile 命名:
CLI profile name [RoleName-111111111111]: my-profile之后所有命令带--profile my-profile即可。
⚠️SSO region 不是实例 region,而是 Identity Center 目录托管区域,从 start URL 看不出来,需向管理员确认(常见
us-east-1、ap-northeast-1)。填错会报InvalidGrantException。⚠️ SSO token 一般 8–12 小时过期,过期后重跑
aws sso login --profile my-profile。
方式 B:AK/SK(访问密钥)
适合个人账号或持有长期密钥的场景。
aws configure# AWS Access Key ID: AKIA...# AWS Secret Access Key: ****# Default region name: us-east-1# Default output format: json二、创建 EIC Endpoint
先拿到目标实例的子网和 VPC:
aws ec2 describe-instances --instance-ids i-0xxxxxxxxxxxx\--profilemy-profile--regionus-east-1\--query'Reservations[].Instances[].[VpcId,SubnetId]'--outputtext在实例所在子网创建 Endpoint:
aws ec2 create-instance-connect-endpoint\--subnet-id subnet-0xxxxxxxxxxxx\--profilemy-profile--regionus-east-1等待状态变为create-complete:
aws ec2 describe-instance-connect-endpoints\--profilemy-profile--regionus-east-1\--query'InstanceConnectEndpoints[].[InstanceId,State]'--outputtext也可在控制台创建:VPC → Endpoints → Create Endpoint,服务选择 EC2 Instance Connect Endpoint。
三、配置实例安全组
Endpoint 会从其 ENI 发起 SSH 到实例的 22,所以实例安全组要放行来自 Endpoint 的 22。两种写法。
写法 1:放行 Endpoint 的安全组(推荐,最精确)
先拿到 Endpoint ENI 关联的安全组:
aws ec2 describe-network-interfaces\--profilemy-profile--regionus-east-1\--filters"Name=description,Values=*Instance Connect*"\--query'NetworkInterfaces[].[NetworkInterfaceId,Groups[].GroupId]'--outputtext然后给实例 SG 加规则:
aws ec2 authorize-security-group-ingress\--group-id sg-0实例的SG\--protocoltcp--port22\--source-security-group-id sg-0Endpoint的SG\--profilemy-profile--regionus-east-1写法 2:放行整个 VPC 网段(省事)
aws ec2 authorize-security-group-ingress\--group-id sg-0实例的SG\--protocoltcp--port22\--cidr172.31.0.0/16\--profilemy-profile--regionus-east-1⚠️ 这里放行的是实例 SG 对 Endpoint 开 22,不是对公网开。Endpoint 的 ENI IP 在 VPC 网段内,用 VPC CIDR 即可覆盖。
四、连接
核心命令:
aws ec2-instance-connectssh\--instance-id i-0xxxxxxxxxxxx\--connection-type eice\--os-user ubuntu\--profilemy-profile\--regionus-east-1两个参数是成败关键:
⚠️
--connection-type eice:强制走 EIC Endpoint。不加的话默认auto,在实例有公网 IP 时会退回直连公网 IP,而本地 IP 又不在安全组里,于是Operation timed out。这是最高频的坑。
⚠️
--os-user:按 AMI 选对应用户,默认ec2-user只对 Amazon Linux 系列有效。常见对照:
AMI os-user Amazon Linux 2 / 2023 ec2-userUbuntu ubuntuDebian adminRHEL / CentOS ec2-user或centos用户名错了会报
Permission denied (publickey)——CLI 把临时公钥推到"该用户"的元数据槽位,SSH 登录用户必须与之一致。
不确定 AMI 类型时查一下:
aws ec2 describe-images --image-ids ami-0xxx\--profilemy-profile--regionus-east-1\--query'Images[].[Name,Description]'--outputtext注意事项汇总
--connection-type eice必须显式指定,否则有公网 IP 的实例会走直连公网超时。--os-user按 AMI 选,Ubuntu 用ubuntu,不是ec2-user。SSO token 过期→
aws sso login --profile my-profile重登。实例必须装
ec2-instance-connect包,否则 sshd 不会去元数据取临时公钥,报Permission denied (publickey)。Ubuntu 安装:sudoapt-getupdate&&sudoapt-getinstall-yec2-instance-connectsudosystemctl restartssh安全组 22 是对 Endpoint 开,不是对公网开——这正是 EIC 的意义所在。
五、简化日常使用
每次敲一长串命令太烦,做个 alias:
# ~/.zshrcaliassshfb='aws ec2-instance-connect ssh \ --instance-id i-0xxxxxxxxxxxx \ --connection-type eice \ --os-user ubuntu \ --profile my-profile \ --region us-east-1'source~/.zshrc sshfb# 一键进入或写进~/.ssh/config配合ProxyCommand,让ssh my-ec2像连普通服务器一样自然。