<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sourabh Choudhary on Medium]]></title>
        <description><![CDATA[Stories by Sourabh Choudhary on Medium]]></description>
        <link>https://medium.com/@sourabh.choudhary_96635?source=rss-13532bfc17b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*45pfNT82-zia7Nzy</url>
            <title>Stories by Sourabh Choudhary on Medium</title>
            <link>https://medium.com/@sourabh.choudhary_96635?source=rss-13532bfc17b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 16:32:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sourabh.choudhary_96635/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Access Recovery to AWS Instances if .Pem Key/File is lost]]></title>
            <link>https://medium.com/globant/access-recovery-to-aws-instances-if-pem-key-file-is-lost-928c63301643?source=rss-13532bfc17b------2</link>
            <guid isPermaLink="false">https://medium.com/p/928c63301643</guid>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[globant]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-instance]]></category>
            <category><![CDATA[aws-ec2]]></category>
            <dc:creator><![CDATA[Sourabh Choudhary]]></dc:creator>
            <pubDate>Tue, 10 Aug 2021 17:04:51 GMT</pubDate>
            <atom:updated>2021-08-10T17:49:32.106Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Access Recovery to AWS Instances if .Pem Key/File is lost</strong></h3><h3><strong>Introduction</strong></h3><p>In AWS, the Linux instances are accessed with .pem key/file, .pem is a private key for which it’s corresponding public key is stored on the server/instance. When we access the instance via SSH, authentication uses the private and public key and if these two match we receive the access to the instance.</p><h3><strong>Background</strong></h3><p>Sometimes System Admin or Developers launch instances in AWS. They used to login to Instance via .pem key but sometimes the system got corrupted or people left the organization. In such a situation we lost our .pem file too and ultimately lost access to Instance.This article will help you to understand the different ways to connect your Instance.</p><h3><strong>Points covered</strong></h3><ol><li>Prerequisites</li><li>Solutions</li><li>Summary</li><li>References</li></ol><h3><strong>1) Prerequisites</strong></h3><ul><li>Access to AWS Platform</li><li>Write access to EC2, EBS,IAM service</li></ul><h3><strong>2) Problem Statement</strong></h3><p>In a hypothetical scenario if you misplaced/lost the .pem key for an AWS linux instance and you would like to access your server for any purpose then can we get it ?</p><h3><strong>3) Solution the .Pem Key/File Lost Problem</strong></h3><p>We can gain access to the instance in case of missing .pem key using either of the below solutions.</p><p>A. AMI</p><p>B. Volume Exchange</p><p>C. SSM Agent</p><p>D. User-data</p><p>E. EC2 connect (Compatible with Amazon Linux 2.2 and above or ubuntu 16.04 and above)</p><h3><strong>3-A. AMI:</strong></h3><ul><li>Which stands for Amazon Machine Image. Through AMI, create a new AWS instance and launch it.</li><li>Choose/select your current instance.</li><li>Right click on it and choose option create AMI.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7c5Vv2UBxRhD2t8o" /></figure><ul><li>Give a name to the AMI and it will create a copy of your server.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vz6GKyDXUDAxJMIx" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mvdu9UmkGbtbUbM1" /></figure><ul><li>After creation of AMI , select AMI and right click on it to select the launch option.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ec0cY_ds0KTMaNMb" /></figure><ul><li>Launch process will get started.</li><li>Now we have to choose appropriate details for launching AMI as it will create a new server for us.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*V5eG4BDS_LeRBdGj" /></figure><ul><li>Details like VPC,Subnet need to select properly from the Configure Instance option (see below screenshot for reference) and after that Add Storage.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nzUaXzKqoPpwlRHf" /></figure><ul><li>Choose to create a new key pair, provide a name for the key pair and launch the instance”.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9dzTy9iJrVcijawG" /></figure><ul><li>Download the key pair before we try to use that with our ssh connection.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JwFcbySSnmrub5gv" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9TH6gU4YOxH4Dwik" /></figure><h3><strong>3-B. Volume Exchange</strong></h3><ul><li>Suppose you have one server called primary (let’s say it is production) server to be recovered via volume exchange.</li><li>Create a Recovery server.</li><li>Stop the prod server and remove/detach the root volume of the prod server. (make a note of root device attachment e.g. here it is dev/xvda)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LlXbR1dJY1InFD10" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/0*qQTA2AA7ou9Zak8x" /></figure><ul><li>Attach that primary server volume(root volume) as secondary to your recovery server</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rGIJUQ3Lrp2wao4-" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*13KpSFno6zLT85_s" /></figure><ul><li>Login to Recovery server and go to the volume of prod server which is attached as secondary here, find out available volumes using below command.</li></ul><blockquote>lsblk -f</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4HM19-7CxFEVtKUh" /></figure><p>Create new directory for the mount purpose</p><blockquote>sudo mkdir /tmp/tmpvolume</blockquote><p>To mount on old Amazon Linux:</p><blockquote>sudo mount /dev/xvdf1 /tmp/tmpvolume</blockquote><p>To mount on new Amazon Linux:</p><blockquote>sudo mount -o nouuid /dev/xvdf1 /tmp/tmpvolume</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GAClyII3zfWhAyoA" /></figure><ul><li>Copy the Recovery instance’s private key to the mounted volume of Primary Instance authorized_keys and you can see the same via cat command</li></ul><blockquote>cp .ssh/authorized_keys /tmp/tmpvolume/home/ec2-user/.ssh/authorized_keys</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ubY3E5hCdhTcz75T" /></figure><ul><li>Unmount and detach the volume that we mounted as secondary from the Recovery server.</li><li>Command for unmount:</li></ul><blockquote>sudo umount /tmp/tmpvolume</blockquote><p>For Detaching the Volume:</p><ul><li>Select the volume , Right click and select Detach volume.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QS72Mbs7MRmKGL84" /></figure><ul><li>Click on the Yes, Detach button.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IqCriu_Ya86_tlGP" /></figure><ul><li>Attach the volume back to the prod server as root volume. (e.g. dev/xvda)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SKtt04tYiXtGxShI" /></figure><ul><li>Start the prod server again.</li><li>Try to ssh via the newly copied private key, In this case login to the Primary server with the Recovery server’s pem file.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wl8EOHVyCn3ONK7w" /></figure><h3><strong>3-C. SSM</strong></h3><ul><li>Create an IAM Role for the EC2 instance</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ztjhm1-WINddgsOq" /></figure><ul><li>Attach the AmazonSSMFullAccess policy to the IAM Role</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*k6fwigHzOaLtt9RI" /></figure><ul><li>Attach the role to the EC2 instance</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*C295su6lBuz3I1-v" /></figure><ul><li>Wait up to 30 minutes before the SSM agent on the instance tries to reconnect Or<strong> </strong>reboot it to trigger the re-try.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oN-EqMidOCiBbjB4" /></figure><h3><strong>3-D. User data</strong></h3><ul><li>To create a new key pair using AWS CLI.</li></ul><blockquote>aws ec2 create-key-pair — key-name my-key-pair — query “KeyMaterial” — output text &gt; my-key-pair.pem</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jO8OF4Md9fdlwExn" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yadfzrSGP4q6hknC" /></figure><ul><li>If you create the private key in the Amazon EC2 console, then retrieve the public key for the key pair.</li></ul><blockquote>ssh-keygen -y -f /pathofpemfile/pemfile</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TN-1pK9rKI1qExFm" /></figure><ul><li>Open the Amazon EC2 console.</li><li>Stop your instance.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YNXvPTYS6dZMMz0q" /></figure><ul><li>Choose Actions, Instance settings, Edit user data.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HlRma3cTbCI4EdPh" /></figure><ul><li>Copy the following script into the Edit user data dialog box:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UTRGKYk4zSbdV6Ze" /></figure><p>Below is userData File</p><blockquote>Content-Type: multipart/mixed; boundary=”//”<br>MIME-Version: 1.0<br> — //<br>Content-Type: text/cloud-config; charset=”us-ascii”<br>MIME-Version: 1.0<br>Content-Transfer-Encoding: 7bit<br>Content-Disposition: attachment; filename=”cloud-config.txt”<br><em>#cloud-config</em><br>cloud_final_modules:<br>- [users-groups, once]<br>users:<br>- name: username<br>ssh-authorized_keys:<br>- Your Public Key pair</blockquote><ul><li>Replace username with your user name, such as ec2-user. You can either enter the default user name, or enter a custom user name.</li><li>Replace PublicKeypair with the public key retrieved from the above step. Be sure to enter the entire public key, starting with ssh-rsa.</li><li>Choose Save.</li><li>Start your instance.</li><li>It’s important to remove the script from the User data field since it contains a key pair.</li><li>Stop your instance.</li><li>Choose Actions, Instance Settings, Edit user data.</li><li>Delete all the text in the Edit user data dialog box, and then choose Save.</li><li>Start your instance.</li></ul><h3><strong>3-E. Ec2- connect</strong></h3><p><strong>Limitations</strong></p><ul><li>Supported Linux distributions are supported:</li><li>Amazon Linux 2 (any version)</li><li>Ubuntu 16.04 or later</li><li>You need to check permissions for your user from which you have login to system(AWS Console) — Your user should have EC2 Connect permissions.If not then you will get an error message while connecting to Ec2 via ec2 connect</li><li>You can add permissions to your user</li><li>Go to IAM</li><li>Select your user</li><li>Click on Add permissions</li><li>Select proper permissions e.g.here- EC2InstacnceConnect</li><li>Click on add permissions.</li></ul><p>Once added It will look like below..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VyNZ1tZwJpIvHWQc" /></figure><ul><li>To connect using the Amazon EC2 console (browser-based client), the instance must have a public IPv4 address.</li><li>If the instance does not have a public IP address, you can connect to the instance using a SSH client or the EC2 Instance Connect CLI, Over a private network, Such as from a machine within the same VPC or from a machine connected to the VPC over VPN or AWS Direct Connect.</li><li>EC2 Instance Connect does not support connection using an IPv6 address.</li><li>Open the Amazon EC2 console at<a href="https://console.aws.amazon.com/ec2/"> https://console.aws.amazon.com/ec2/.</a></li><li>In the navigation pane, choose Instances.</li><li>Select the instance and choose Connect.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*osGq9E6UvDppACOp" /></figure><ul><li>Choose EC2 Instance Connect.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pi4D9MDCd-wtDsAy" /></figure><ul><li>Verify the user name and choose Connect to open a terminal window.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3YhA-nczFUefItZo" /></figure><ul><li>Now once you get connected to the instance via the browser you can copy your system’s pub key or create a new key via ssh-keygen and save it to authorized_keys under the .ssh folder.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yAdnbNSDzdpXD4qs" /></figure><ul><li>Now you can connect with instance via ssh</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jNV2_9Xdf-tYf0ia" /></figure><p>This way you can get connected to your instance without restarting it.</p><h3><strong>4) Conclusion</strong></h3><p>Based on the article following can be the priorities for solutions to choose from first to last.</p><ul><li>Ec2-Connect:Will be the highest priority as in this we can avoid downtime.</li><li>SSM:Can be chosen as the next priority as we need to install an SSM agent in this method also we can avoid downtime.</li><li>User-Data:In this instance needs to be rebooted to apply changes to user data.</li><li>Volume Exchange:This method will take time to complete and will require some downtime too.</li><li>AMI: Will be the last method to follow as it will change on IP address and Instance ID.</li></ul><h3><strong>5) References</strong></h3><ol><li><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/session-manager.html">https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/session-manager.html</a></li><li><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html">https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html</a></li><li><a href="https://aws.amazon.com/premiumsupport/knowledge-center/ec2-windows-replace-lost-key-pair/">https://aws.amazon.com/premiumsupport/knowledge-center/ec2-windows-replace-lost-key-pair/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=928c63301643" width="1" height="1" alt=""><hr><p><a href="https://medium.com/globant/access-recovery-to-aws-instances-if-pem-key-file-is-lost-928c63301643">Access Recovery to AWS Instances if .Pem Key/File is lost</a> was originally published in <a href="https://medium.com/globant">Globant</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>