如何在AWS管理控制台中更改ec2实例的密钥对?我可以停止实例,我可以创建新的密钥对,但我没有看到任何链接来修改实例的密钥对。
当前回答
来自AWS EC2支持的指令:
Change pem login go to your EC2 Console Under NETWORK & SECURITY, click on Key Pair Click on Create Key Pair Give your new key pair a name, save the .pem file. The name of the key pair will be used to connect to your instance Create SSH connection to your instance and keep it open in PuttyGen, click "Load" to load your .pem file Keep the SSH-2 RSA radio button checked. Click on "Save private key" You'll get pop-up window warning, click "Yes” click on "Save public key" as well, so to generate the public key. This is the public key that we're going to copy across to your current instance Save the public key with the new key pair name and with the extension .pub Open the public key content in a notepad copy the content below "Comment: "imported-openssh-key" and before "---- END SSH2 PUBLIC KEY ---- Note - you need to copy the content as one line - delete all new lines on your connected instance, open your authorized_keys file using the tool vi. Run the following command: vi .ssh/authorized_keys you should see the original public key in the file also move your cursor on the file to the end of your first public key content :type "i" for insert on the new line, type "ssh-rsa" and add a space before you paste the content of the public key , space, and the name of the .pem file (without the .pem) Note - you should get a line with the same format as the previous line press the Esc key, and then type :wq!
这将保存更新后的authorized_keys文件
现在尝试使用新的密钥pai打开一个新的SSH会话到您的实例
当您确认能够使用新的密钥对SSH进入实例时,您可以vi . SSH /authorized_key并删除旧的密钥。
对Shaggie评论的回答:
如果您无法连接到实例(例如密钥损坏),请使用AWS控制台分离卷(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-detaching-volume.html)并将其重新连接到工作实例,然后更改卷上的密钥并将其重新连接到前一个实例。
其他回答
我认为最简单的方法是:
创建现有实例的AMI映像。 使用AMI映像(由步骤1创建)和新的密钥对启动新的EC2实例。 使用新密钥登录到新的EC2实例。
你能做什么…
创建一个新的实例配置文件/角色,其中附加AmazonEC2RoleForSSM策略。 将此实例概要文件附加到实例。 使用SSM会话管理器登录到实例。 在本地机器上使用keygen创建密钥对。 使用SSM会话将该密钥的公共部分推到实例上。 利润。
我注意到,当由Elastic Beanstalk管理时,您可以更改活动的EC2密钥对。2 .在“Elastic Beanstalk > Configuration > Security”下的“EC2密钥对”下拉框中选择新密钥。你会看到这条消息询问你是否确定:
EC2KeyName:对选项EC2KeyName设置的更改将不会生效 立即。您现有的每个EC2实例都将被替换 届时,您的新设置将生效。
当我这样做时,我的实例已经终止了。然后开始,结束,再开始。显然,“替换”意味着终止并创建一个新实例。如果您已经修改了引导卷,那么首先创建一个AMI,然后在相同的Elastic Beanstalk > Configuration > Instances表单中指定这个AMI作为自定义AMI ID。这也会对替换EC2实例发出警告。
在修改了EC2密钥对和Custom AMI ID并看到有关这两者的警告之后,单击Save继续。
请记住,当重新创建实例时,IP地址会发生变化,因此您需要从EC2控制台检索一个新的IP地址,以便在通过SSH连接时使用。
一旦启动了实例,就无法在元数据级别上更改与该实例关联的密钥对,但是可以更改用于连接该实例的ssh密钥。
在大多数ami上都有一个启动过程,下载公共ssh密钥并将其安装在.ssh/authorized_keys文件中,以便您可以作为该用户使用相应的私有ssh密钥进行ssh登录。
如果您希望更改用于访问实例的ssh密钥,则需要编辑实例本身的authorized_keys文件并将其转换为新的ssh公钥。
authorized_keys文件位于您正在登录的用户的主目录下的.ssh子目录下。取决于你正在运行的AMI,它可能在以下情况之一:
/home/ec2-user/.ssh/authorized_keys
/home/ubuntu/.ssh/authorized_keys
/root/.ssh/authorized_keys
编辑authorized_keys文件后,在断开用于编辑该文件的会话之前,总是使用不同的终端来确认您能够通过ssh登录到该实例。您不希望犯错误并将自己完全锁定在实例之外。
当您在考虑EC2上的ssh密钥对时,我建议您将自己的个人ssh公钥上传到EC2,而不是让Amazon为您生成密钥对。
这是我写的一篇文章:
上传个人ssh密钥到Amazon EC2 http://alestic.com/2010/10/ec2-ssh-keys
这只适用于您运行的新实例。
如果您正在使用ElasticBeanstalk平台,您可以通过执行以下命令更改密钥:
弹性豆茎面板 配置 实例(右上方的齿轮) EC2密钥对
这将终止当前实例,并创建一个具有所选键/设置的新实例。
推荐文章
- 亚马逊ECS和亚马逊EC2有什么区别?
- 运行ssh后立即执行命令
- 我如何知道我在S3桶中存储了多少对象?
- S3 Bucket操作不应用于任何资源
- 将AWS凭证传递给Docker容器的最佳方法是什么?
- 当权限为S3时,AccessDenied for ListObjects for S3 bucket:*
- 电子邮件地址未验证(AWS SES)
- ssh -L转发多个端口
- 使用Boto3将S3对象作为字符串打开
- 从PKCS12文件中提取公钥/私钥,供以后在SSH-PK-Authentication中使用
- 在python shell中按方向键时看到转义字符
- 如何忽略ansible SSH的真实性检查?
- 如何移除SSH密钥?
- AWS VPC - Internet网关vs. NAT
- 如何在AWS Lambda中加载npm模块?