Thursday 21 March 2019

How to Connect to Your Linux Instance if You Lose Your Private Key by Raj Gupat

First stop the EC2 server and detach the volume





Now create the temporary ec2 server and attach the volume of main ec2 server to this temporary server



Now Logging into temporary ec2 server


 Now run the below command to temporary server

[root@ip-172-31-94-4 ~]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0   8G  0 disk         This is volume of main EC2 server
└─xvdf1 202:81   0   8G  0 part


Create a temporary directory to mount the volume.


[root@ip-172-31-94-4 ~]# sudo mkdir /mnt/tempvol

Now mount the volume
[root@ip-172-31-94-4 ~]# sudo mount -o nouuid /dev/xvdf1 /mnt/tempvol

[root@ip-172-31-94-4 ~]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0   8G  0 disk
└─xvdf1 202:81   0   8G  0 part /mnt/tempvol   --Now volume are mount

Now copy the key of temporary ec2 server to main volume

[root@ip-172-31-94-4 ~]# cp .ssh/authorized_keys /mnt/tempvol/home/ec2-user/.ssh/authorized_keys

Now unmount the main volume from temporary ec2 server

[root@ip-172-31-94-4 ~]# sudo umount /mnt/tempvol


[root@ip-172-31-94-4 ~]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0   8G  0 disk
└─xvdf1 202:81   0   8G  0 part

Now detach the volume from temporary server and attach it to main ec2 server



Now add this volume to main EC2 and replace /dev/sdf to /dev/xvda otherwise you are not able to restart your ec2 server



Now start EC2 and logging into it with new key pair


No comments:

Post a Comment