How to Regain Access to an AWS EC2 Instance Without a Private Key and Set Up SFTP with FileZilla
Method 1: Use EC2 Instance Connect
EC2 Instance Connect is a feature that allows you to connect to your instance without needing the private key.
Ensure that your instance is running Amazon Linux 2 or Ubuntu, and your instance's security group allows inbound SSH (port 22) traffic from your IP address.
Open the AWS Management Console.
Navigate to the EC2 Dashboard.
Select your instance and click on Connect.
Choose EC2 Instance Connect and click Connect.
Method-2
Here’s a step-by-step guide to detaching and attaching the root volume to another instance to regain access
Step-by-Step Guide to Attach and Detach the Root Volume
Prerequisites:
Ensure you have another EC2 instance (let's call it
HelperInstance
) in the same region and availability zone where you have access.Ensure you have SSH access to the
HelperInstance
.
Step 1: Stop the Inaccessible Instance
Open the AWS Management Console.
Navigate to the EC2 Dashboard.
Select the instance you cannot access (let's call it
TargetInstance
).Click on Instance State and select Stop.
Step 2: Detach the Root Volume
In the EC2 Dashboard, with the
TargetInstance
still selected, go to the Description tab.Find the Root device (e.g.,
/dev/xvda
).Click on the Volume ID to open the volume details page.
In the volume details page, click on Actions and select Detach Volume.
Confirm the detachment.
Step 3: Attach the Volume to the Helper Instance
Go back to the Volumes page in the EC2 Dashboard.
Select the detached volume (from the
TargetInstance
).Click on Actions and select Attach Volume.
In the Instance field, select the
HelperInstance
.Set a device name (e.g.,
/dev/xvdf
for Linux).Click on Attach.
Step 4: Modify the Volume on the Helper Instance
SSH into the
HelperInstance
.List the block devices to ensure the volume is attached:
lsblk
You should see a device, such as
/dev/xvdf
, which represents the attached volume.Mount the volume:
sudo mkdir /mnt/recover sudo mount /dev/xvdf1 /mnt/recover
Note: The device name might be
/dev/xvdf1
or similar, depending on your instance.Add a new SSH key to the authorized_keys file:
sudo nano /mnt/recover/home/ec2-user/.ssh/authorized_keys
Add your new public SSH key to this file. Save and exit the editor.
Unmount the volume:
sudo umount /mnt/recover
Step 5: Reattach the Volume to the Original Instance
Go back to the Volumes page in the EC2 Dashboard.
Select the volume you modified.
Click on Actions and select Detach Volume.
Confirm the detachment.
Once detached, select the volume again.
Click on Actions and select Attach Volume.
In the Instance field, select the
TargetInstance
.Set the device name back to its original value (e.g.,
/dev/xvda
).Click on Attach.
Step 6: Start the Original Instance
Go back to the Instances page in the EC2 Dashboard.
Select the
TargetInstance
.Click on Instance State and select Start.
Step 7: Connect to the Original Instance
- Once the instance is running, use your SSH client to connect to the
TargetInstance
using the new private key that corresponds to the public key you added.
Connecting via FileZilla
After regaining SSH access, you can set up SFTP in FileZilla:
Open FileZilla.
Go to File > Site Manager.
Click New Site.
Set Protocol to SFTP - SSH File Transfer Protocol.
Enter the Host (public IP or DNS of your
TargetInstance
).Set Logon Type to Key file.
Enter the User (e.g.,
ec2-user
).Use the new private key file you used to regain SSH access.
Click Connect.
Following these steps should allow you to regain access to your EC2 instance and set up SFTP access using FileZilla.