天天看點

更改EC2執行個體的密鑰對

本文翻譯自:Change key pair for ec2 instance

How do I change the key pair for my ec2 instance in AWS management console?

如何在AWS管理控制台中為我的ec2執行個體更改密鑰對?

I can stop the instance, I can create new key pair, but I don't see any link to modify the instance's key pair.

我可以停止執行個體,可以建立新的密鑰對,但是看不到任何修改執行個體密鑰對的連結。

#1樓

參考:https://stackoom.com/question/X4KT/更改EC-執行個體的密鑰對

#2樓

This answer is useful in the case you no longer have SSH access to the existing server (ie you lost your private key).

如果您不再具有對現有伺服器的SSH通路權限 (即,丢失了私鑰) ,則此答案很有用。

If you still have SSH access, please use one of the answers below.

如果您仍然具有SSH通路權限,請使用以下答案之一。

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#replacing-lost-key-pair

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#replacing-lost-key-pair

Here is what I did, thanks to Eric Hammond's blog post:

感謝Eric Hammond的部落格文章,這是我的工作:
  1. Stop the running EC2 instance 停止正在運作的EC2執行個體
  2. Detach its

    /dev/xvda1

    volume (let's call it volume A) - see here 分離其

    /dev/xvda1

    卷(我們将其稱為卷A)- 參見此處
  3. Start new t1.micro EC2 instance, using my new key pair. 使用我的新密鑰對啟動新的t1.micro EC2執行個體。 Make sure you create it in the same subnet , otherwise you will have to terminate the instance and create it again. 確定在同一子網中建立它 ,否則,您将不得不終止執行個體并再次建立它。 - see here - 看這裡
  4. Attach volume A to the new micro instance, as

    /dev/xvdf

    (or

    /dev/sdf

    ) 将卷A作為

    /dev/xvdf

    (或

    /dev/sdf

    )附加到新的微型執行個體。
  5. SSH to the new micro instance and mount volume A to

    /mnt/tmp

    SSH到新的微型執行個體,并将卷A挂載到

    /mnt/tmp

    $ sudo mount /dev/xvdf1 /mnt/tmp $ sudo安裝/ dev / xvdf1 / mnt / tmp
  6. Copy

    ~/.ssh/authorized_keys

    to

    /mnt/tmp/home/ubuntu/.ssh/authorized_keys

    ~/.ssh/authorized_keys

    複制到

    /mnt/tmp/home/ubuntu/.ssh/authorized_keys

  7. Logout 登出
  8. Terminate micro instance 終止微執行個體
  9. Detach volume A from it 從中分離體積A
  10. Attach volume A back to the main instance as

    /dev/xvda

    将卷A作為

    /dev/xvda

    附加回主執行個體
  11. Start the main instance 啟動主執行個體
  12. Login as before, using your new

    .pem

    file 使用新的

    .pem

    檔案像以前一樣登入

That's it.

而已。

#3樓

I went through this approach, and after some time, was able to make it work.

我經曆了這種方法,經過一段時間後,它才得以發揮作用。

The lack of actual commands made it tough, but I figured it out.

缺乏實際的指令使操作變得很困難,但我發現了。

HOWEVER - much easier approach was found and tested shortly after:

但是-在此之後不久發現并測試了一種更為簡便的方法:
  1. Save your instance as an AMI (reboot or not, I suggest reboot). 将您的執行個體另存為AMI(是否重新開機,建議重新開機)。 This will only work if EBS backed. 這僅在EBS支援的情況下有效。
  2. Then, simply start an instance from this AMI and assign your new Keyfile. 然後,隻需從此AMI啟動執行個體并配置設定新的密鑰檔案。
  3. Move over your elastic IP (if applicable) to your new instance, and you are done. 将您的彈性IP(如果适用)移到新執行個體,就完成了。

#4樓

I noticed that when managed by Elastic Beanstalk, you can change your active EC2 key pair.

我注意到,當由Elastic Beanstalk管理時,您可以更改活動的EC2密鑰對。

Under Elastic Beanstalk > Configuration > Security, choose the new key from the EC2 key pair drop-down.

在Elastic Beanstalk>配置>安全下,從EC2密鑰對下拉清單中選擇新密鑰。

You'll see this message asking if you're sure:

您會看到此消息,詢問您是否确定:
EC2KeyName: Changes to option EC2KeyName settings will not take effect immediately. EC2KeyName:對選項EC2KeyName設定的更改不會立即生效。 Each of your existing EC2 instances will be replaced and your new settings will take effect then. 您現有的每個EC2執行個體将被替換,新設定将生效。

My instance was already terminated when I did this.

執行此操作時,我的執行個體已被終止。

It then started, terminated, and started again.

然後啟動,終止并再次啟動。

Apparently "replacing" means terminating and creating a new instance.

顯然,“替換”是指終止并建立一個新執行個體。

If you've modified your boot volume, create an AMI first, then specify that AMI in the same Elastic Beanstalk > Configuration > Instances form as the Custom AMI ID .

如果您已經修改了啟動卷,請首先建立一個AMI,然後在與Custom AMI ID相同的Elastic Beanstalk>配置>執行個體表單中指定該AMI 。

This also warns about replacing the EC2 instances.

這也警告要更換EC2執行個體。

After you've modified your EC2 key pair and Custom AMI ID, and after seeing warnings about both, click Save to continue.

修改完EC2密鑰對和“自定義AMI ID”之後,并在看到有關二者的警告後,請單擊“ 儲存”以繼續。

Remember that the IP address changes when the instance is re-created so you'll need to retrieve a new IP address from the EC2 console to use when connecting via SSH.

請記住,重新建立執行個體時IP位址會更改,是以您需要從EC2控制台檢索新的IP位址,以便在通過SSH連接配接時使用。

#5樓

Run this command after you download your AWS pem.

下載下傳您的AWS pem後,運作此指令。
ssh-keygen -f YOURKEY.pem -y
           

Then dump the output into

authorized_keys

.

然後将輸出轉儲到

authorized_keys

Or copy pem file to your AWS instance and execute following commands

或将pem檔案複制到您的AWS執行個體并執行以下指令
chmod 600 YOURKEY.pem
           

and then

接着
ssh-keygen -f YOURKEY.pem -y >> ~/.ssh/authorized_keys
           

#6樓

This will work only if you have access to the instance you want to change/add the key in. You can create a new key pair.

僅當您有權通路要更改/添加密鑰的執行個體時,此方法才有效。您可以建立一個新的密鑰對。

Or if you already have the key pair, then you can paste the public key of the new pair in the authorized_keys file on your instance.

或者,如果您已經擁有密鑰對,則可以将新密鑰對的公鑰粘貼到執行個體上的authorized_keys檔案中。

vim .ssh/authorized_keys

vim .ssh / authorized_keys

Now you can use the private key for that pair and log in.

現在,您可以為該對使用私鑰并登入。

Hope this helps.

希望這可以幫助。

繼續閱讀