AWS实例的密钥登录机制
AWS创建EC2实例时会生成一个密钥文件,有了这个密钥才能使用SSH登录实例。可以使用 AWSSupport-ResetAccess 文档在 Amazon EC2 Windows 实例上自动重新启用本地管理员密码生成,以及在 Amazon EC2 Linux 实例上生成新 SSH 密钥。
工作原理
使用自动化和 AWSSupport-ResetAccess 对实例进行故障排除的工作原理如下:
- 您为实例指定 ID 并运行 Automation 工作流程。
- 系统创建一个临时 VPC,然后运行一系列 Lambda 函数以配置该 VPC。
- 系统在与您的原始实例相同的可用区内为您的临时 VPC 标识一个子网。
- 系统启动一个临时的启用了 SSM 的帮助程序实例。
- 系统停止您的原始实例并创建备份。然后,它将原始根卷附加到帮助程序实例。
- 系统使用 Run Command 在帮助程序实例上运行 EC2Rescue。在 Windows 上,EC2Rescue 通过在附加的原始根卷上使用 EC2Config 或 EC2Launch 为本地管理员启用密码生成。在 Linux 上,EC2Rescue 生成并注入新的 SSH 密钥并将私有密钥加密保存到 Parameter Store 中。完成后,EC2Rescue 重新将根卷附加回原始实例。
- 系统根据您的实例创建新 Amazon 系统映像 (AMI),现在密码生成已启用。您可以使用此 AMI 创建新 EC2 实例,并根据需要关联新密钥对。
- 系统重启您的原始实例,并终止临时实例。系统也将终止临时 VPC 和在自动化开始时创建的 Lambda 函数。
- Windows:您的实例生成一个新密码,您可以使用分配给实例的当前密钥对从 EC2 控制台对该密码进行解码。
Linux:您可以使用存储在 Systems Manager Parameter Store 中的 SSH 密钥(格式为 /ec2rl/openssh/instance_id/key),通过 SSH 连接到实例。
使用AWSSupport-ResetAccess重置EC2(Linux)密钥
第一步:使用CloudFormation创建角色
在 CloudFormation 中创建所需的 IAM 角色,复制 Output 中的 Value,这是后续 AssumeRole 的 ARN。
第二步:进入 AWSSupport-ResetAccess
在 AWS Systems Manager 控制台中,找到 AWSSupport-ResetAccess 自动化文档并打开。
第三步:输入参数
- InstanceID:指定无法访问实例的 ID。
- EC2RescueInstanceType:为 EC2Rescue 实例指定实例类型,默认实例类型为 t2.small。
- SubnetId:指定您指定的实例所在可用区中某个现有 VPC 的子网。
- AssumeRole:从 CloudFormation 控制台复制的 AssumeRole ARN。
第四步:执行自动化
点击执行,等待自动化流程完成。
第五步:检查结果
控制台显示执行成功后,前往 Parameter Store,路径 /ec2rl/openssh/instance_id/key 就是连接到服务器的密钥,通过 SSH 即可连接到服务器。
陈汉卿
云业务事业部 | 高级系统架构师
神州泰岳软件股份有限公司 · AWS战略合作伙伴
返回技术博客
AWS Instance Key Login Mechanism
When creating an EC2 instance, AWS generates a key file that is required to log in to the instance via SSH. You can use the AWSSupport-ResetAccess document to automatically re-enable local administrator password generation on Amazon EC2 Windows instances, and to generate a new SSH key on Amazon EC2 Linux instances.
How It Works
The process of troubleshooting an instance using Automation and AWSSupport-ResetAccess works as follows:
- You specify the instance ID and run the Automation workflow.
- The system creates a temporary VPC and then runs a series of Lambda functions to configure it.
- The system identifies a subnet in your temporary VPC within the same Availability Zone as your original instance.
- The system launches a temporary SSM-enabled helper instance.
- The system stops your original instance and creates a backup, then attaches the original root volume to the helper instance.
- The system uses Run Command to run EC2Rescue on the helper instance. On Windows, EC2Rescue enables password generation for the local administrator using EC2Config or EC2Launch on the attached original root volume. On Linux, EC2Rescue generates and injects a new SSH key and saves the private key encrypted to Parameter Store. After completion, EC2Rescue re-attaches the root volume back to the original instance.
- The system creates a new Amazon Machine Image (AMI) from your instance with password generation now enabled. You can use this AMI to create a new EC2 instance and associate a new key pair as needed.
- The system restarts your original instance and terminates the temporary instance, temporary VPC, and Lambda functions created at the start of the automation.
- Windows: Your instance generates a new password, which you can decode from the EC2 console using the current key pair assigned to the instance.
Linux: You can connect to the instance via SSH using the SSH key stored in Systems Manager Parameter Store at the path /ec2rl/openssh/instance_id/key.
Resetting an EC2 (Linux) Key Using AWSSupport-ResetAccess
Step 1: Create a Role Using CloudFormation
Create the required IAM role in CloudFormation. Copy the Value from the Output — this is the ARN for the subsequent AssumeRole.
Step 2: Open AWSSupport-ResetAccess
In the AWS Systems Manager console, find the AWSSupport-ResetAccess automation document and open it.
Step 3: Enter Parameters
- InstanceID: Specify the ID of the instance you cannot access.
- EC2RescueInstanceType: Specify the instance type for the EC2Rescue instance. The default is t2.small.
- SubnetId: Specify a subnet from an existing VPC in the same Availability Zone as your specified instance.
- AssumeRole: The AssumeRole ARN copied from the CloudFormation console.
Step 4: Execute the Automation
Click Execute and wait for the automation process to complete.
Step 5: Check the Results
Once the console shows successful execution, go to Parameter Store. The key at path /ec2rl/openssh/instance_id/key is the key to connect to your server — use it to SSH into the instance.
Chen Hanqing
Cloud Business Division | Senior Solutions Architect
Shenzhou Taiyue Software Co., Ltd. · AWS Strategic Partner
Back to Tech Blog