Connecting to an Ubuntu EC2 Instance Using a .txt Key File: A Step-by-Step Guide

Amazon Web Services (AWS) provides secure access to your EC2 instances using SSH key pairs. Typically, the private key file comes in .pem format, but sometimes you might receive it as a .txt file. In this guide, we’ll walk you through the steps to connect to an Ubuntu EC2 instance using a .txt key file.


What is a Key Pair?

A key pair consists of a public and private key used for secure SSH connections to your EC2 instance:

  • Public Key: Stored on the instance.
  • Private Key: Held by you and used for authentication during connection.

AWS typically provides the private key as a .pem file, but if it's in .txt format, don’t worry—you can still use it by converting it to .pem.


Step-by-Step Guide

Step 1: Verify the .txt Key File

  1. Open the .txt file in a text editor (e.g., VS Code or Notepad).
  2. Ensure the file contains a valid private key. It should begin and end with the following lines:
-----BEGIN RSA PRIVATE KEY-----
(key contents)
-----END RSA PRIVATE KEY-----

Step 2: Rename the .txt File to `.pem

To use the .txt key file with SSH, it must have the .pem extension. Rename it by following these steps:

  1. Open a terminal on your local machine.
  2. Use the mv command to rename the file:
mv your-key-file.txt your-key-file.pem

Verify that the renamed file exists:

ls your-key-file.pem

Step 3: Set the Correct File Permissions

For security, SSH requires that the private key file is only readable by you. Set the appropriate permissions:

  • Run the following command to restrict permissions:
chmod 400 your-key-file.pem
  • This prevents unauthorized access to the key.
  • Confirm the permissions:
ls -l your-key-file.pem
  • You should see something like:
-r--------  1 user group  1679 Dec  6 14:00 your-key-file.pem

Step 4: Locate Your EC2 Instance Details

  1. Log in to your AWS Management Console.
  2. Navigate to EC2 > Instances.
  3. Find your instance's Public IP Address or Public DNS (hostname).
  4. Example:
  • Public IP3.25.100.50
  • Public DNSec2-3-25-100-50.compute-1.amazonaws.com

Step 5: Connect to the EC2 Instance

Now, you’re ready to connect using the SSH command:

  • Open your terminal.
  • Run the following command:
ssh -i your-key-file.pem ubuntu@<ec2-public-ip>
  • Replace:
  • your-key-file.pem with the path to your .pem file.
  • <ec2-public-ip> with the public IP or DNS of your EC2 instance.

Example

If your key file is my-key.pem and your EC2 instance’s public IP is 3.25.100.50, the command would look like this:

ssh -i my-key.pem ubuntu@3.25.100.50

Troubleshooting Common Issues

1. Permission Denied Errors

  • Ensure the key file permissions are correct:
chmod 400 your-key-file.pem
  • Double-check that you’re using the correct username for your instance:
  • Ubuntu Instances: Use ubuntu.
  • Amazon Linux Instances: Use ec2-user.

2. Key Format Error

  • Ensure the .txt file was renamed correctly and retains its private key format:
-----BEGIN RSA PRIVATE KEY-----
(key contents)
-----END RSA PRIVATE KEY-----

3. No Route to Host

  • Verify the instance is running in the AWS Console.
  • Check the Security Group associated with the instance. Ensure it allows inbound SSH traffic (port 22) from your IP.

4. Connection Timeout

  • Ensure your local machine has internet access.
  • Double-check the instance’s Elastic IP or Public DNS.

Step 6: Automate Key File Management (Optional)

To avoid manual key file handling, consider:

  • AWS Systems Manager Session Manager: A service that enables SSH-free connections to EC2 instances.
  • Bastion Hosts: Set up a dedicated jump server for secure connections.

Conclusion

Connecting to an Ubuntu EC2 instance using a .txt key file is straightforward once you convert it to .pem format. By following the steps outlined in this guide, you can securely access your instance and start managing your server.

With proper setup and troubleshooting, you'll have seamless access to your EC2 instance in no time.

Related Posts