{"id":13163,"date":"2023-04-05T02:09:52","date_gmt":"2023-04-05T08:09:52","guid":{"rendered":"https:\/\/www.supportpro.com\/blog\/?p=13163"},"modified":"2023-07-26T06:24:49","modified_gmt":"2023-07-26T12:24:49","slug":"how-to-regain-access-to-aws-ec2-linux-instance-when-key-pair-lost","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/how-to-regain-access-to-aws-ec2-linux-instance-when-key-pair-lost\/","title":{"rendered":"How to Regain access to AWS EC2 Linux instance when key pair lost?"},"content":{"rendered":"\n<p>An associated key pair ( private key and a public key) is necessary to access EC2 instance securely. In case you have lost or corrupted the key pair, here&#8217;s how you can regain access to AWS EC2 Linux instance.<\/p>\n\n\n\n<p>Let\u2019s assume that the EC2 instance running contains a website but you have lost the key pair that is associated with the EC2 instance. And here we will show the two easiest ways to regain access to AWS Linux EC2 instances.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>By using an Amazon Machine Image (AMI)<\/li><li>By swapping the volume of the EC2 instance&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>By using an AMI&nbsp;<\/strong><\/h2>\n\n\n\n<p>An Amazon Machine Image (AMI) is the exact copy of the settings and configuration of an EC2 instance. We can create an AMI from an existing running instance.&nbsp;<\/p>\n\n\n\n<p>In this method, we don\u2019t have to stop the running EC2 instance.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. The first step is to create an Amazon Machine Image of our primary web server EC2 instance.&nbsp;<\/strong><\/h3>\n\n\n\n<p>Select the instance &gt; Actions &gt; Image and templates &gt; Create image<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/rN6yf37wrSIwDg_W5TkLLX8MOC5ioyiW9ITYh-lD5yeF7CQakAWBhGKXfO328TxpVi4iwhhTOa_5cJfEw35golcRNESerwlAp5sR6yJbsx4QMlGvBj2ZAGoRy2YEwmVjpEUPFWIgTmaVLZ2zcI3F6u4\" alt=\"\"\/><\/figure>\n\n\n\n<p>Now provide the necessary details like image name, and description and create the image.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Now we can deploy a new instance by using the customized image that we have created.&nbsp;<\/strong><\/h3>\n\n\n\n<p>Follow the steps below to deploy a new instance by using the customized AMI.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Login to the <a href=\"https:\/\/www.supportpro.com\/aws_management.php\">AWS<\/a> console and navigate to the instances page.<\/li><li>Click on Launch an instance and provide the new instance name.\u00a0<\/li><li>Now click on the option My AMIs and select the AMI we have created.\u00a0<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/0svVOhNXKGjXpEQofM_AMaM3QQHWRBhz238B5_lMHWDfKZFm3k5iQTpWDO4B5FKfPOZylSCELQy0bd4dKnPbk9hMY5vvKo_FC92WKzEGbu7xbOPtqbStWZTESIYjTSEJYlCbVBu6h_q5Wwa2PlwA8as\" alt=\"\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Also create a new key pair for the new instance.&nbsp;<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/PuhK3N6HVzfFTY2e8NrVXoYF0_kci3HVzcoIQrjY9RacEy5JLx5Lbav0b8HoQhhkTK1WcQn2mL037a_7-os0d8tX7IRg6s2Iw3wmfqnodI8HUKU6HECa0qlukll4LhDaFItfaATmth49wwAi3CWYAqU\" alt=\"\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>Now click on the button \u201cLaunch instance\u201d to create the new web server instance.&nbsp;<\/li><li>We can now access the new web server instance using the new key pair and terminate the old instance.&nbsp;<\/li><\/ol>\n\n\n\n<p>This approach does not involve the retrieval of the key pair for the initial instance. Rather, it entails the customization of an image of the web server and the subsequent launch of a new web server that replicates the previous instance, albeit with a new key pair.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>By swapping the volume of the EC2 instance&nbsp;<\/strong><\/h2>\n\n\n\n<p>This approach entails the regeneration of a new SSH key pair and its attachment to the EC2 instance in order to restore its functionality. However, a major drawback of this method is that it requires the instance to be halted, thereby causing a service interruption.<\/p>\n\n\n\n<p>For this demonstration, I\u2019ve deployed two EC2 instances with different key pairs.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Instance1: maininstance&nbsp;<\/li><li>Instance2: recoveryinstance&nbsp;<\/li><\/ul>\n\n\n\n<p>The first step is to detach the volume of the main instance and attach it to the recovery instance.&nbsp;<\/p>\n\n\n\n<p>Before detaching the volume from the main instance make a note of the root device name which is \/dev\/xvda in this case and the volume ID.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/wDvV0oWH4mQyBplqSTIGAi7TjLGS9L5th4ivAED-BDaRnvlt3uMphRyUPX8f107HS8I3kDX-iJ9M2stsF8iPi2X_B-We7fNaZxj4KrkK22SoGBgEHWyihCN8ngmoAwGm59rCsIntnxeOBlXMNumnpwE\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Stop the main instance and detach the root volume from it.&nbsp;<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Navigate to the Elastic Block Store and select volumes.&nbsp;<\/li><li>Click on the volume ID of the main instance.&nbsp;<\/li><li>Click on \u201cActions\u201d and then \u201cDetach volume\u201d<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/xFlJODvQZm0n2Btssiy_o6BCPLlWEEpQ92HvHfDIWdhgQ9sevfceis_pl0DMO4qiL852dzbw4veSmO6wX6kTyrB-lVQMldBbee70RJMq7l8VGRPVd8XvxI-8OxfCTQzs5KOjIk486-ytyivdDAKdUdU\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Now we can attach the root volume of the man instance to the recovery instance.<\/strong>&nbsp;<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Navigate to the Elastic Block Store and select volumes.&nbsp;<\/li><li>Click on the volume ID of the main instance (Detached volume of the main instance).&nbsp;<\/li><li>Click on \u201cActions\u201d and then \u201cAttach volume\u201d<\/li><li>Select the recovery instance to attach the volume and the device name.&nbsp;<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/E516C-WaWckm_NF5rv-6k_zVwWdq3Ub3knrr58K_aCYXTbA2D4CFUofu25RoQSIl7ayAo5EWWozVDhQ0_2T6acJyWJg3J1HcLPTUv0dt-80BxjyFdyUTPM3ZOFuM1jFdhiDhQZgIULSNLUWRKyI8pMk\" alt=\"\"\/><\/figure>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\"><li>And click on Attach Volume button<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Now we have to SSH to the recover instance and mount the volume attached to the recovery instance.&nbsp;<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Login to the recovery instance using SSH.&nbsp;<\/li><li>List the volumes attached to the recovery instance using lsblk command.&nbsp;<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/UNGJyaxp1aJGFOSwXROgRlcmCLY4bVaf3nZF9fW63QyArkKSQPKg9_kAzbG4TjkNz-7Wf1UHNSmMA80kM7R2sRdyic5q-ETnbdq7pHId07_-qKE2VbO8v0hpvTU9edRyu6XpVqw9NpWmdxLFskWklAI\" alt=\"\"\/><\/figure>\n\n\n\n<p>Here we can find a disk named xvdf which is not mounted.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Create a directory \/mnt to mount the volume.&nbsp;<\/li><\/ol>\n\n\n\n<p># mkdir mnt<\/p>\n\n\n\n<p># mount -o nouuid \/dev\/xvdf1 \/mnt\/<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/ljijNh9Uy7tLIyapXZJSKOPPtVUQ68b6TqjCMRTrRDr0MOmaY_qwqbznW33Ehgtxy1b-Tj3hrcq1SoXdEqvDomVxqi4xzD5TMLfJZaVwzn-cWSSR-kiO7BFQ1rf0YkWnx8EDgoyylGSDzJzwoQr7yi4\" alt=\"\"\/><\/figure>\n\n\n\n<p>Use df -h or lsblk command to verify the mount point.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Now switch to the directory \/mnt\/home\/ec2-user\/.ssh and replace authorized_keys with the recovery instance key.&nbsp;<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/83q_XlLfmHlhzl82LiH-e0Sr_ag1uigz3rCkG-vkkApc0hAdE76gxEWfVbwVg1rKYqs1JKNPZVVtcLCZ6UhN8T-8s7bVDu9O2t693HLRFcjY6exKpcMkk0qTOeYZ-m_NmZDfffocVMSXV60WJa7eNvA\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Detach the volume from the recovery instance and re-attach it to the main instance.&nbsp;<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/v_BlPlx8AM8l14fwDOjQZWem5-Pux8R8lNvTz_PKpSPVnhfqFpaBLBi-uclrF0veRscPzfd9B78NnYQCLIOnPc-vUkbQLqsHzCo0UwkQX43f2_K51H7Ut4ZeWqdekt7hPHsjVNMgpiOUBCaL0FKkoe0\" alt=\"\"\/><\/figure>\n\n\n\n<p>Make sure to attach the volume to the main instance using the path \/dev\/xvda as previously noted.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Finally, we can start the main instance<\/strong><\/h3>\n\n\n\n<p>You can now access the server using the new key you just made.<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile is-vertically-aligned-center has-white-background-color has-background\"><div class=\"wp-block-media-text__content\">\n<p class=\"has-large-font-size\">Partner with <strong>SupportPRO<\/strong> for 24\/7 proactive cloud support that keeps your business secure, scalable, and ahead of the curve.<\/p>\n\n\n\n<!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper\" id=\"hs-cta-wrapper-3350a795-db50-482f-9911-301930d1b1be\"><span class=\"hs-cta-node hs-cta-3350a795-db50-482f-9911-301930d1b1be\" id=\"hs-cta-3350a795-db50-482f-9911-301930d1b1be\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/3350a795-db50-482f-9911-301930d1b1be\" ><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-3350a795-db50-482f-9911-301930d1b1be\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/3350a795-db50-482f-9911-301930d1b1be.png\"  alt=\"Contact Us today!\"\/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '3350a795-db50-482f-9911-301930d1b1be', {\"useNewLoader\":\"true\",\"region\":\"na1\"}); <\/script><\/span><!-- end HubSpot Call-to-Action Code -->\n<\/div><figure class=\"wp-block-media-text__media\"><img fetchpriority=\"high\" decoding=\"async\" width=\"904\" height=\"931\" src=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png\" alt=\"guy server checkup\" class=\"wp-image-12943 size-full\" srcset=\"https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup.png 904w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-291x300.png 291w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-768x791.png 768w, https:\/\/www.supportpro.com\/blog\/wp-content\/uploads\/2022\/09\/Free-server-checkup-585x602.png 585w\" sizes=\"(max-width: 904px) 100vw, 904px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An associated key pair ( private key and a public key) is necessary to access EC2 instance securely. In case you have lost or corrupted the key pair, here&#8217;s how&hellip;<\/p>\n","protected":false},"author":4,"featured_media":13165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[69],"tags":[20,106],"class_list":["post-13163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","tag-security","tag-server"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/13163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=13163"}],"version-history":[{"count":4,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/13163\/revisions"}],"predecessor-version":[{"id":13203,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/13163\/revisions\/13203"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media\/13165"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=13163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=13163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=13163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}