Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
我想使用相同的 SSH 密钥对来访问我所有 AWS 区域内的 Amazon Elastic Compute Cloud(Amazon EC2)实例。
**注意:**如果在运行 AWS 命令行界面(AWS CLI)命令时收到错误,请参阅排查 AWS CLI 错误。此外,确保您使用的是最新版本的 AWS CLI。
如果还没有 EC2 SSH 密钥对,请创建一个。
要使用 SSH 密钥对访问所有区域中的实例,请在操作系统上完成以下步骤。
Linux
完成以下步骤:
要从 SSH 私钥 (.pem) 文件生成 SSH 公钥 (.pem) 文件,请运行以下命令:
$ ssh-keygen -y -f MyKeyPair.pem > $HOME/.ssh/id_rsa_MyKeyPair.pub
**注意:**将 MyKeyPair.pem 替换为相应私有 .pem 文件的名称。确保使用 bash Shell,并使用具有有效访问权限的用户配置 AWS CLI。
要设置区域,请运行 describe-regions 命令:
$ AWS_REGIONS="$(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)"
**注意:**如果使用 ZSH Shell,则运行以下命令来激活分词功能,这样 for loop 命令就能正确迭代每个区域名称:
$ setopt shwordsplit
要将 SSH 公钥对导入到区域,请运行 import-key-pair 命令:
$ for each_region in ${AWS_REGIONS} ; do aws ec2 import-key-pair --key-name MyKeyPair --public-key-material fileb://$HOME/.ssh/id_rsa_MyKeyPair.pub --region $each_region ; done
Windows
要从 SSH 私钥 (.pem) 文件生成 SSH 公钥 (.pub) 文件,请执行以下操作: 打开 PuTTYgen。 选择加载以加载私钥文件。 选择保存公钥。
要将 SSH 公钥对导入到区域,请运行以下命令:
$PubFile = Get-Content .\MyKeyPair.pub -raw$Key = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($PubFile))foreach ($Region in (Get-AWSRegion).Region) {Import-EC2KeyPair -KeyName MyKeyPair -PublicKeyMaterial $Key -Region $Region}
**注意:**将 MyKeyPair.pub 替换为相应的公共 SSH 文件名。
AWS 服务端点
Import-EC2KeyPair Cmdlet