Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can't seem to find the option to assign a newly created key pair to an amazon ec2 instance after deleting away the original key pair. I wanted to "deny" the first key pair from access to the instance, which i think i succeeded, however, i am unable to reassign the instance with another key pair.

share|improve this question
up vote 9 down vote accepted

When you specify a keypair on running an instance, most public AMIs will copy the public ssh key for that keypair into a .ssh/authorized_keys file in the primary user account's home directory. This is only done on first boot.

The user account varies depending on the Linux distro and the AMI publisher. Some common user accounts include root, ec2-user, and ubuntu, so the default file might be one of:


Note: The above described behavior is not enforced by EC2, but is simply a de facto standard implemented by most popular public AMIs.

If you want to prevent the initial keypair from having access to a running instance, simply edit the .ssh/authorized_keys file, remove that public ssh key entry, and add the public ssh key you want to have access.

This is standard ssh key management, not specific to EC2. This is the key to your server's security so it is important to understand what you are doing. Read up on ssh to make sure you do this safely.

Warning! Make sure you test ssh with the new key in a separate terminal before you terminate your existing ssh session that was used to edit the file! If you break the authorized_keys file you risk not being able to connect to the instance.

Make sure you add the public ssh key to the authorized_keys file, not the private ssh key! If you saved your private ssh key to, say, KEYPAIR.pem, then here's a command that will output the corresponding public key:

ssh-keygen -y -f KEYPAIR.pem

If the above seems too complicated to you, then it is fine to start a new instance with a new keypair specified. You should always have a procedure in place to be able to start replacement instances anyway as your existing instance could fail at any time.

In a related topic, I recommend using your own ssh keys instead of having Amazon generate keypairs for you. It makes things easier all around. Here's an article I wrote on the topic:

Uploading Personal ssh Keys to Amazon EC2

share|improve this answer

I don't believe there is a way to assign a new key-pair to an instance, but you could create an AMI image from the current instance, then create an instance of the new AMI, assigning it the new key-pair upon creation. Once the new instance is up and running, just stop/terminate the old instance.

The new instance will have a different IP address to the old instance. If that's a problem, you may want to assign an elastic IP to the old instance, then, when you're ready, re-assign the elastic IP to the new instance.

It is my understanding that Amazon only inserts your key-pair's public key into an instance upon creation of a new instance.

share|improve this answer
I was presuming you no longer had root access on the system. Eric's answer is correct and thorough if you do. – Mike Sep 14 '11 at 20:32

Second option is to replace a lost key pair on an EC2 instance as long as it's an EBS backed instance. The following link may help:


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.