如何在AWS管理控制台中更改ec2实例的密钥对?我可以停止实例,我可以创建新的密钥对,但我没有看到任何链接来修改实例的密钥对。


当前回答

您有几个选项可以替换EC2实例的密钥。

您可以在.ssh/authorized_keys文件中手动替换密钥。但是,这需要您实际访问实例或卷(如果未加密)。 您可以使用AWS系统管理器。这需要安装代理。

由于第一个选项可以很容易地在答案中或在您选择的搜索引擎中找到,所以我想重点介绍系统管理器。

打开服务系统管理器 单击左侧的Automation。 单击执行自动化 选择AWSSupport-TroubleshootSSH(通常在最后一页)

您可以在AWS官方文档中找到更多信息

其他回答

Yegor256的回答对我来说很有用,但我想我只是添加一些评论来帮助那些不太擅长挂载驱动器的人(比如我!):

Amazon允许您在附加卷时选择要如何命名它。您必须使用从/dev/sda到/dev/sdp范围内的名称 新版本的Ubuntu会将你放入的内容重命名为/dev/xvd(x)或类似的名称。

所以对我来说,我选择了/dev/sdp作为AWS中的挂载名称,然后我登录到服务器,发现Ubuntu已经将我的卷重命名为/dev/xvdp1)。然后我必须安装驱动器-对我来说,我必须这样做:

mount -t ext4 xvdp1 /mnt/tmp

在跳过所有这些步骤之后,我可以在/mnt/tmp目录下访问我的文件

步骤:

创建新密钥,例如使用AWS控制台,PuTTY密钥生成器,或ssh-keygen 停止实例 设置实例用户数据以将公钥推送到服务器 启动实例

#cloud-config
cloud_final_modules:
- [once]
bootcmd:
 - echo 'ssh-rsa AAAAB3Nz...' > /home/USERNAME/.ssh/authorized_keys

其中USERNAME是机器的预期用户名。可以从AWS获得默认用户名列表。

来自AWS的分步说明

这是为他们谁有两个不同的pem文件,并出于任何安全目的想要丢弃其中一个。假设我们想要丢弃1。pem

连接到服务器2,从~/.ssh/authorized_keys中复制ssh密钥 在另一个终端上连接服务器1,并将密钥粘贴到~/.ssh/authorized_keys中。现在您将有两个公共ssh密钥 现在,为了增强自信心,尝试使用2.pem连接服务器1。您将能够将服务器1与这两个1连接。Pem和2.pem 现在,注释1。Pem SSH,使用SSH -i连接。pem user@server1

来自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)并将其重新连接到工作实例,然后更改卷上的密钥并将其重新连接到前一个实例。

你能做什么…

创建一个新的实例配置文件/角色,其中附加AmazonEC2RoleForSSM策略。 将此实例概要文件附加到实例。 使用SSM会话管理器登录到实例。 在本地机器上使用keygen创建密钥对。 使用SSM会话将该密钥的公共部分推到实例上。 利润。