Amazon EC2密钥是创建EC2时必须下载。因为密钥是连接EC2实例的唯一途径。如果丢失了密钥,将无法登录服务器的。那么该如何处理呢?在这里介绍一下丢失EC2密钥时应该怎么操作。
EBS-Backed实例是可重新获得登录到Linux系统的权限。 ※instance store-backed实例不支持以下操作。文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
流程
- 停止EC2实例foraws
- Detach foraws实例的root Volume
- 把foraws实例的root Volume,Attach到别的Linux EC2实例
- 修改authorized_keys以后,Detach foraws实例的root Volume
- 把foraws的root Volume,Attach到foraws实例
- 重启foraws实例
步骤
确认Root Device类型文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
记录以下信息
- Amazon EC2 ID
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
- AMI ID
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
- EBS ID
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
停止Amzon EC2实例(foraws)文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
Detach foraws实例的root Volume文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
等到Volume的State变为available。文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
把foraws的root Volume,Attach到另外一个Linux EC2实例(temp)上文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
- 确认temp实例在Stopped状态
- 把foraws实例的root Volume,Attach到temp实例
文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
提示文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
Marketplace镜像里启动的EC2,必须在停止的状态下进行Attach。EC2在Running状态下的话,会报以下错误。 Error attaching volume: Cannot attach volume ‘vol-1dd3500b’ with Marketplace codes as the instance ‘i-a5522e56’ is not in the ‘stopped’ state.Error attaching volume with Marketplace codes as the instance is not in the ‘stopped’ state.文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
在temp实例上挂载foraws实例的Volume以后修改authorized_keys文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
用lsblk命令查看Volume分区情况。文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvdj 202:144 0 8G 0 disk / xvde 202:64 0 8G 0 disk
foraws的root Volume xvde没有被分区,所以mount时使用/dev/xvde,如果root Volume分区过的话,mount时,需指定/dev/xvde1。文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde 7.9G 651M 6.9G 9% / tmpfs 296M 0 296M 0% /dev/shm # mkdir /mnt/tmp # mount /dev/xvde /mnt/tmp # df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde 7.9G 651M 6.9G 9% / tmpfs 296M 0 296M 0% /dev/shm /dev/xvde 7.9G 650M 6.9G 9% /mnt/tmp
用现在系统authorized_keys,覆盖foraws实例的authorized_keys文件。文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
# cp -p /root/.ssh/authorized_keys /mnt/tmp/root/.ssh/authorized_keys # cd # umount /mnt/tmp # df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde 7.9G 651M 6.9G 9% / tmpfs 296M 0 296M 0% /dev/shm
停止temp实例,并Detach foraws实例的root Volume文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/
把Volume,Attach到foraws EC2实例
instance选择foraws,Device填写/dev/sda(如果root Volume以进行分区的话,填写/dev/sda1)。
※Amazon EC2是把/dev/sda当作root Device的。
确认Volume的State变为in-use之后,启动foraws实例,并进行登录就可以。在这里登录foraws实例时的密钥是,temp实例的密钥。

2F
另一个Linux EC2实例(temp)是从哪里来的?
1F
照此方法,果然解决问题 ,靠谱!
B1
@ cloud 你好,请问怎么获得另一个“Linux EC2 实例(temp)”?