📜 ⬆️ ⬇️

Step-by-step instructions for restoring access to a Linux Amazon EC2 instance when you lose a pem file

If you didn’t create a key pair (KeyPair) to access it via SSH or lost it when creating a Linux instance in AWS, then you need to perform a number of operations to gain access to the machine. It is not possible to add keys to an already running instance via the AWS console. There are three ways we can restore SSH access to the instance.

The easiest way is to configure the key using the cloud-init mechanism (thanks for the hint yusman ). But it does not work if the rights to the /home directory are damaged or the distribution does not support cloud-init directives.

The second way is to create an image (Amazon Machine Image) from an existing instance and then launch a new instance based on it, but with the creation of a key. Thus, an existing machine will actually be cloned with the only difference that at the last stage it is possible to create new keys. Creating an image can take a long time for an instance with large volume disks, and you will have to configure all services tied to this machine again. Therefore, this method in most cases will take more time.
')
The official documentation from Amazon describes another way . It allows you to restore access and minimize the cost of reconfiguring services (compared to the second method), which are tied to the instance to which access is lost. The article gives a step by step description.

Attention! This method works only for instances that use block storage (Amazon Elastic Block Store) as the root device, and does not work for local storage of instances (Amazon EC2 Instance Store).

To find out what type of root device you are using, open the Amazon EC2 console, go to Instances , select an instance and check the Root device type parameter in the details pane.

Instance root type

If your machine is EBS , then this method is suitable for you.

Step 1. Preparation


First you need to save some information so that you do not have to be distracted and do not switch to other screens (especially it will be very inconvenient). All data can be found in the detailed information of the instance (you already know how to get there). Record the following settings:


For an instance running in a VPC , save the value of the VPC ID .

For EC2-classic : if an elastic IP address ( Elastic IP ) is created for the instance, then its value must be saved.

Instance settings to save
Image is clickable

To get the block storage identifier ( EBS ID ), click on the Root device name.

EBS ID


Step 2. Create a temporary instance


The next step is to create an instance, with which you will restore the keys on the original instance. You can skip this step if you have another running instance that is in the same zone (Availability zone) as the one being restored, and at the same time it is created using the same AMI or the version of the operating system on it allows you to connect the disk from the original instance and copy ssh keys to it. If there is no such instance, then do the following:



Step 3. Connecting the root disk from the restored machine to the temporary instance


First you need to disconnect the disk from the original instance and connect it to a temporary one. Since this is the root disk, the original instance will have to be stopped before it is disconnected.


Step 4. Preparing keys for connecting to the instance over SSH


Linux users do not need to generate any additional keys. It is only necessary to give permissions to read this file:

chmod 400 my-keypair.pem

Key preparation, if you are working under Windows
To connect to a Linux machine, we will use the PuTTY utility, so if you do not have it installed, download it. To connect via SSH with its help, you need a ppk-key , while with AWS the key was saved in pem format. In order to get the key to the desired format, do the following:

  • Open PuTTYgen (installed with PuTTY)
  • Select RSA 2048 bits in the parameters.

    RSA 2048 bit
  • Load the saved pem-key by clicking Load (Load an existing private key file)
  • Specify a passphrase and confirm it ( confirm passphrase ). It is not necessary to do this, but it is safer. The only difference is that when connecting using this key, you will always be prompted to enter this phrase to confirm the entry.
  • Save the file with the name you specified when creating the key pair ( KeyPair ) during the creation of the temporary instance (the name is the same as the name of the pem key )

The key is ready.


Step 5. Connecting to a temporary instance over SSH


Connect on port 22 using the key. The username depends on which AMI was used to create the instance. You recorded it at the very beginning. The following usernames are possible:


The name of the host you want to connect to is user@aws-host.amazon.com , where user is the name described above, and aws-host.amazon.com is the ip-address of your instance, which can be found on the detailed information tab (from it you saved parameters at the very beginning). The parameter is called IPv4 Public IP . Please note that if you do not use Elastic IP , each time the instance starts, it will have a new ip address.

Connection instructions for Windows users
  • Run PuTTY
  • In the Host Name address in the format username @ ip-address

    Configure PuTTY. Host name

  • Go to the Connection - SSH - Auth section and download the ppk-key (Private key file for authentication)
  • Click Open and then agree to trust the server.
  • If you specified a password phrase (passphrase) when creating a ppk key, enter it to confirm the entry:

     Using username "ubuntu". Authenticating with public key "imported-openssh-key" Passphrase for key "imported-openssh-key": 

  • You are on the server.

    Successful login



Step 6. Copying keys from the temporary instance to the original one


Mount the volume that you connected to the temporary instance so that you can access its file system.

Mounting a partition
For example, if the disk name is /dev/sdf (it may appear differently on your instance), use the following operations to mount the volume in /mnt/tempvol :

  • Determine the partition names of the mounted drive:

    [user ~]$ lsblk

     NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 10G 0 disk └─xvda1 202:1 0 10G 0 part / xvdf 202:80 0 10G 0 disk └─xvdf1 202:81 0 10G 0 part 

    /dev/xvda1 and /dev/xvdf1 - disk partitions. /dev/xvdf1 does not have a mount point (MOUNTPOINT), so this is the partition of the disk that we connected earlier.
  • Create a temporary directory to mount the partition:

    [user ~]$ sudo mkdir /mnt/tempvol
  • Mount the partition in the created directory:

    [user ~]$ sudo mount /dev/ xvdf1 /mnt/tempvol


Copy the SSH keys from the temporary instance to the mounted partition.

Attention! Use the username specified on the command line. This is necessary because, despite the fact that you have successfully connected with a standard username, which depends on the operating system (described in step 5), in the AMI from the AWS Marketplace it may be different after logging in. For example, for AMI WordPress Certified by Bitnami, when logging in via SSH, the standard Ubuntu username is ubuntu . However, the username after login is bitnami

Copy keys
For example, if the username is ubuntu , use the following command to copy:

[user ~]$ cp .ssh/authorized_keys /mnt/tempvol/home/ ubuntu /.ssh/authorized_keys

If you do not have permissions to edit files in /mnt/tempvol , then copy the files using sudo and check the permissions to make sure that you can go to the original instance:

  • Check file permissions:

    [user ~]$ sudo ls -l /mnt/tempvol/home/ ubuntu /.ssh/authorized_keys
    total 4
    -rw------- 1 200 500 392 Aug 15 00:06 authorized_keys

    In the example, 200 is the user ID and 500 is the group ID.
  • Restart the key copy command using sudo :

    [user ~]$ sudo cp .ssh/authorized_keys /mnt/tempvol/home/ ubuntu /.ssh/authorized_keys

  • Check if the file permissions have changed:

    [user ~]$ sudo ls -l /mnt/tempvol/home/ ubuntu /.ssh/authorized_keys

    If the rights have changed, restore them:

    [user ~]$ sudo chown 200 : 500 /mnt/tempvol/home/ ubuntu /.ssh/authorized_keys


Unmount the partition:

[user ~]$ sudo umount /mnt/tempvol

Step 7. Starting an instance with restored access via SSH



Step 8. Access check


Connect to the restored access instance using the generated key.

If the name of the new key pair is different from the previous one, make sure that you connect using the new private key.

Step 9. The final stage


If you created a new temporary instance to perform all operations, rather than using an existing one, stop it if you no longer need it:


Thus, you have restored access to a Linux Amazon EC2 instance.

Source: https://habr.com/ru/post/420297/


All Articles