AWS EC2 丢失密钥处理办法

管理员 系统管理344,4156字数 1733阅读5分46秒阅读模式

Amazon EC2密钥是创建EC2时必须下载。因为密钥是连接EC2实例的唯一途径。如果丢失了密钥,将无法登录服务器的。那么该如何处理呢?在这里介绍一下丢失EC2密钥时应该怎么操作。

EBS-Backed实例是可重新获得登录到Linux系统的权限。 ※instance store-backed实例不支持以下操作。文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

流程

  1. 停止EC2实例foraws
  2. Detach foraws实例的root Volume
  3. 把foraws实例的root Volume,Attach到别的Linux EC2实例
  4. 修改authorized_keys以后,Detach foraws实例的root Volume
  5. 把foraws的root Volume,Attach到foraws实例
  6. 重启foraws实例

步骤

确认Root Device类型文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

aws文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

记录以下信息

  • Amazon EC2 ID

aws文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

  • AMI ID

aws文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

  • EBS ID

aws文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

停止Amzon EC2实例(foraws)文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

aws文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

Detach foraws实例的root Volume文章源自运维生存时间-https://www.ttlsa.com/system/aws-ec2-lost-key-process/

aws文章源自运维生存时间-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/

  1. 确认temp实例在Stopped状态
  2. 把foraws实例的root Volume,Attach到temp实例

aws文章源自运维生存时间-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/

aws

把Volume,Attach到foraws EC2实例

aws

instance选择foraws,Device填写/dev/sda(如果root Volume以进行分区的话,填写/dev/sda1)。
※Amazon EC2是把/dev/sda当作root Device的。

aws

确认Volume的State变为in-use之后,启动foraws实例,并进行登录就可以。在这里登录foraws实例时的密钥是,temp实例的密钥。

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
管理员
  • 本文由 发表于 05/09/2015 01:00:48
  • 转载请务必保留本文链接:https://www.ttlsa.com/system/aws-ec2-lost-key-process/
评论  3  访客  3
    • 春风十里
      春风十里 1

      另一个Linux EC2实例(temp)是从哪里来的?

      • cloud
        cloud 9

        照此方法,果然解决问题 ,靠谱!

          • Winfield
            Winfield 1

            @ cloud 你好,请问怎么获得另一个“Linux EC2 实例(temp)”?

        评论已关闭!