Edmund Sulzanok
Edmund Sulzanok

Reputation: 1973

Amazon key pair deleted

I think I deleted serverside key pair by and now am getting:

Using username "ubuntu".
Server refused our key

I am running a Amazon EC2 server for my customer. How do I restore access to the server?

Upvotes: 0

Views: 351

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269490

While you are unable to login to the Amazon EC2 instance, do not panic. All your data is safe!

The instance will need a new keypair loaded into the /users/ubuntu/.ssh/authorized_users file.

This can be done in two ways:

  • By creating a new instance that will trigger the automated mechanism that installed the key initially, or
  • By copying a file onto that disk volume

Method 1: Create a new instance

This method makes an AMI (Amazon Machine Image) of your boot volume and launches a new Amazon EC2 instance from that image. This will trigger the same mechanism that is used to install a key when your instance was initially launched.

  • Select your instance and select Image > Create Image from the Actions menu
  • In the AMIs section of the console, wait until your instance is Running, then select it and choose Launch.
  • Enter the configuration information (eg instance type, network)
  • After clicking the final Launch button, choose a keypair

The boot process will recognize that it is running on a new Amazon EC2 instance (with a different InstanceId) and will copy your nominated keypair to the authorized_users file. You can then login.

Method 2: Copy a keypair to the disk

This method involves attaching the disk to another instance, copying the keypair, then booting it again.

  • Stop your instance ("Instance A")
  • Start another Amazon EC2 instance, or use an existing one if you already have it ("Instance B")
  • Detach the EBS volume from Instance A
  • Attach the EBS volume to Instance B
  • Login to Instance B, access the newly-attached disk and copy a keypair to the /users/ubuntu/.ssh/authorized_users file
  • Reverse the above: Detach the EBS Volume, reattach it to Instance A, start Instance A
  • Login and be happy

This is similar to the rather dated document: How to Recover an Unreachable Linux Instance

Upvotes: 2

XINFENG XIE
XINFENG XIE

Reputation: 276

  1. You can stop (not terminated) the server and make an image from it.

  2. Then you can start a new server based on the image you just made. When you create the new server, you can create a new key pair.

  3. Finally, you can log in the newly created server and use it as usual.

Upvotes: 0

Related Questions