How to find the username of the instance launched in amazon-ec2?

Learn how to find the username of the instance launched in amazon-ec2? with practical examples, diagrams, and best practices. Covers ssh, amazon-ec2 development techniques with visual explanations.

Identifying the Default Username for Your Amazon EC2 Instance

Hero image for How to find the username of the instance launched in amazon-ec2?

Learn how to determine the correct username for SSH access to your Amazon EC2 instance, covering common AMIs and troubleshooting tips.

When you launch an Amazon EC2 instance, connecting to it via SSH requires knowing the correct username. Unlike traditional servers where 'root' or 'admin' might be common, EC2 instances often use specific usernames based on the Amazon Machine Image (AMI) they are launched from. Using the wrong username is a common cause of SSH connection failures. This article will guide you through identifying the appropriate username for various popular AMIs and provide methods to confirm it.

Understanding EC2 Default Usernames

Amazon EC2 instances are provisioned with a default user account that has sudo privileges. This account is typically named after the operating system or distribution of the AMI. Knowing this default username is crucial for your initial SSH connection. If you attempt to connect with an incorrect username, you'll likely encounter a 'Permission denied (publickey)' error, even if your SSH key pair is correct.

flowchart TD
    A[Launch EC2 Instance] --> B{Select AMI}
    B --> C{Determine Default Username}
    C --> D{SSH to Instance}
    D -- Incorrect Username --> E[Permission Denied]
    D -- Correct Username --> F[Access Granted]
    E --> C

Flowchart illustrating the process of connecting to an EC2 instance and the impact of an incorrect username.

Common Default Usernames by AMI

The default username varies significantly depending on the AMI you choose. Here's a list of the most common default usernames for popular AMIs:

Amazon Linux

The default username for Amazon Linux AMIs (including Amazon Linux 2 and Amazon Linux 2023) is ec2-user.

Ubuntu

For Ubuntu AMIs, the default username is ubuntu.

RHEL

Red Hat Enterprise Linux (RHEL) AMIs use ec2-user as the default username.

CentOS

CentOS AMIs typically use centos or ec2-user.

Debian

Debian AMIs usually have admin as the default username.

SUSE Linux

SUSE Linux Enterprise Server (SLES) AMIs use ec2-user or root.

How to Find the Username in the EC2 Console

The most reliable way to find the correct username is directly through the AWS Management Console. When you select an instance, its details often include connection instructions that specify the username.

1. Navigate to EC2 Dashboard

Log in to the AWS Management Console and go to the EC2 dashboard.

2. Select Your Instance

In the left navigation pane, click 'Instances'. Select the running instance you wish to connect to.

3. View Connection Instructions

With the instance selected, click the 'Connect' button at the top of the details pane. The 'Connect to instance' page will display the recommended username and an example SSH command.

Hero image for How to find the username of the instance launched in amazon-ec2?

The 'Connect to instance' dialog in the AWS EC2 console explicitly states the username.

Connecting via SSH with the Correct Username

Once you have identified the correct username, you can use it in your SSH command. Remember to replace your-key-pair.pem with the path to your private key file and your-instance-public-ip with your instance's public IP address or DNS name.

ssh -i /path/to/your-key-pair.pem <username>@<your-instance-public-ip-or-dns>

Example SSH command using the identified username.