我刚接触 AWS,创建了一个新的 Windows EC2 实例。我看到可以使用 PuTTY 连接到不同实例,如 Amazon Linux、Ubuntu,但无法连接 Windows 实例。难道不能使用 PuTTY 连接 Windows 实例吗?谢谢。
我刚接触 AWS,创建了一个新的 Windows EC2 实例。我看到可以使用 PuTTY 连接到不同实例,如 Amazon Linux、Ubuntu,但无法连接 Windows 实例。难道不能使用 PuTTY 连接 Windows 实例吗?谢谢。
您可以通过SSH连接到在AWS上的Linux和Mac实例。 您可以使用远程桌面(RDP)访问Windows实例。
假设您想要SSH连接到Windows实例,那么您需要安装OpenSSH。 但是这仍然需要访问Windows实例。
AWS提供了几种访问Windows实例的方式,其中之一是AWS Systems Manager Session Manager (SSM)。您需要通过SSM启用RDP。您可以通过此workshop了解如何执行此操作。或者,配置SSM以针对该实例运行powershell命令。注意:实例必须具有IAM访问SSM的角色。如果实例位于私有VPC中,则需要通过endpoints访问SSM。打开Amazon EC2控制台,然后选择“实例”。
选择实例的复选框,然后展开“操作”下拉列表。如果您使用旧版控制台,则选择“获取Windows密码”。如果您使用新版控制台,则选择“安全性”,然后选择“获取Windows密码”。 注意:在首次启动新实例后,这个选项可能需要几分钟才能可用。
选择“浏览”,选择您的密钥对文件,然后选择“打开”。
-或-
将您的密钥对内容粘贴到文本框中。 选择“解密密码”。
连接实例
在密码屏幕上,选择“下载远程桌面文件”。浏览器会提示您打开或保存RDP快捷方式文件。选择保存文件选项。下载完成后,选择“取消”返回到实例页面。
进入您的下载目录并打开RDP快捷方式文件。
您可能会收到一个警告,远程连接的发布者未知。选择“连接”以继续连接到您的实例。
管理员帐户是默认选择的。复制并粘贴您之前保存的密码。
您还可以使用EC2串行控制台来访问运行Windows的EC2实例,这对于调试启动问题非常有用。
由于自签名证书的性质,您可能会收到一个警告,安全证书无法得到验证。使用以下步骤验证远程计算机的身份,或者如果信任该证书,则只需选择“是”(Windows)或“继续”(Mac OS X)。
有关连接详细信息,请访问此站点。
Build a Windows AMI based on Server 2019 or later (e.g. use Windows_Server-2019-English-Full-ECS_Optimized-2022.12.14
as the base AMI). As part of that AMI:
Install OpenSSH and configure the sshd
and ssh-agent
services as described in the above link:
$ErrorActionPreference = 'Stop'
Write-Host 'Installing and starting sshd'
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType Automatic
Start-Service sshd
Write-Host 'Installing and starting ssh-agent'
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent
Write-Host 'Set PowerShell as the default SSH shell'
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value (Get-Command powershell.exe).Path -PropertyType String -Force
Configure PowerShell as the default SSH shell:
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value (Get-Command powershell.exe).Path -PropertyType String -Force
Launch an EC2 instance using the new AMI:
Provide a valid, existing SSH keypair.
Select IMDSv2.
Provide the following PowerShell script as the userdata script. This script will ensure the SSH keypair specified when launching will be added to the the Administrator
user's authorized keys file. Note that the <powershell>
and </powershell>
tags are part of the userdata; they are parsed and extracted by AWS prior to the script being executed.
<powershell>
# Userdata script to enable SSH access as user Administrator via SSH keypair.
# This assumes that
# 1. the SSH service (sshd) has already been installed, configured, and started during AMI creation;
# 2. a valid SSH key is selected when the EC2 instance is being launched; and
# 3. IMDSv2 is selected when launching the EC2 instance.
# Save the private key from instance metadata.
$ImdsToken = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/api/token' -Method 'PUT' -Headers @{'X-aws-ec2-metadata-token-ttl-seconds' = 2160} -UseBasicParsing).Content
$ImdsHeaders = @{'X-aws-ec2-metadata-token' = $ImdsToken}
$AuthorizedKey = (Invoke-WebRequest -Uri 'http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key' -Headers $ImdsHeaders -UseBasicParsing).Content
$AuthorizedKeysPath = 'C:\ProgramData\ssh\administrators_authorized_keys'
New-Item -Path $AuthorizedKeysPath -ItemType File -Value $AuthorizedKey -Force
# Set appropriate permissions on administrators_authorized_keys by copying them from an existing key.
Get-ACL C:\ProgramData\ssh\ssh_host_dsa_key | Set-ACL $AuthorizedKeysPath
# Ensure the SSH agent pulls in the new key.
Set-Service -Name ssh-agent -StartupType "Automatic"
Restart-Service -Name ssh-agent
</powershell>
Connect to the instance via SSH like normal. Provide the SSH keypair you specified when launching and user Administrator
. For example:
ssh -i ~/.ssh/my-keypair Administrator@my.ec2.instance
AWS Session Manager
连接到 Windows EC2。它允许您从基于浏览器的 shell 管理实例。像 SSH 一样,您可以从 SSM 提供的 shell 中管理 EC2。您可以从控制台执行 PowerShell 命令。AmazonSSMRoleForInstancesQuickSetup
的角色用于 EC2。您可以使用相同的角色或利用其中的策略。PuTTy不是通常用于Windows机器的工具。虽然我相信可以为Windows机器设置SSH访问,但下面的方法更典型且更容易设置。
如果您有与EC2实例相关联的密钥对,则可以使用AWS控制台向您显示用户名和密码,以便通过提供pem文件来使用Windows远程桌面访问您的计算机。这还需要您在安全组中启用入站RDP访问(与SSH不同的端口)。
如果您只想要PowerShell访问,并且正在使用已安装SSM代理程序(例如Amazon Linux AMI)和具有所需SSM权限的角色的实例,则可以使用Session Manager进行连接。这不需要密钥对或直接网络访问您的计算机,因此这是一种更安全的方法,但需要进行更多设置。
这两种技术在下面的参考资料中有更详细的描述。
参考资料:
我们能够在AWS上通过SSH连接到Windows EC2实例吗?- 不行。
您可以使用RDP(远程桌面)连接Windows EC2。您可以从AWS控制台下载.rdp文件,方法是通过选择实例 -> 连接 -> RDP
SSH连接适用于基于Linux的EC2机器。