Paul Taylor
Paul Taylor

Reputation: 13130

Cannot create amazon key pair for right user for connecting to EC2 instance from Putty

I use PuTTY to connect to Amazon EC2 instanace, my computer failed and it turns out I didnt have the PuTTY ppk file saved anywhere so I could conect to EC2 from a different computer.

So I followed the instructions at https://repost.aws/knowledge-center/user-data-replace-key-pair-ec2 to create a new Amazon Key Pair and then add it to the user-data of the EC2 instance so that this new pair can be used to connect from PuTTY

It seemed to work, I could login to my EC2 from PuTTY. However I realized i has set name to ec2-user and my actual username on this intance was ubuntu so i didnt login to the correct account and attempts to use su - ubuntu failed as well because dont know the password. So I did the following:

but it doesnt work when I try to connect using puTTY it just complains Server refused our key, what am I doing wrong ?

I then try as an experiment modifying name to rubbish and restarting. This has no effect i couldnt login as rubbish but I could continue to login as ec2-user!

Update

I managed to recover my original .ppk file from my failed drive and so I can now use PuTTY ssh to EC2 using the original key pair.

However, for the benefit of others Im still interested in why the user-data approach doesnt seem to work ?

Upvotes: 0

Views: 115

Answers (3)

Alex
Alex

Reputation: 21

In response to your question: "However, for the benefit of others Im still interested in why the user-data approach doesnt seem to work ?"

By default, user data scripts and cloud-init directives run only during the boot cycle when you first launch an instance. You can update your configuration to ensure that your user data scripts and cloud-init directives run every time you restart your instance.

source: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#:~:text=By%20default%2C%20user%20data%20scripts,time%20you%20restart%20your%20instance.

Upvotes: 0

macosmi
macosmi

Reputation: 165

Some ways how to access Amazon EC2 instance if SSH key pair was lost: https://repost.aws/knowledge-center/user-data-replace-key-pair-ec2

If you cannot issue: sudo su - ubuntu, you can try to log from ec2-user into root account (only if ec2-user has sudo permissions), and issue a sudo -i and then from the root account: su - ubuntu.

We would be happy to know if it worked.

Upvotes: 0

Ansh Bhatia
Ansh Bhatia

Reputation: 65

You can also have an option to create an AMI for that instance and launch the instance from AMi with your selected key. And you will use ubuntu as your username for login.

Regards

Upvotes: 0

Related Questions