How to Connect to an EC2 Instance from Your Mac - Explained Simply!

Abdul FirozAbdul Firoz
3 min read

If you have ever wanted to talk to a computer living in a big computer house called Amazon EC2, here's how you can do it using your Mac! Imagine EC2 as a big, powerful computer that can do lots of work for you, and your Mac is like a magic door that lets you speak to it.

Step 1: Get the Keys to the House!

Before you can talk to the EC2 computer, you need the keys. This key is a special file called a "key pair". It's like a secret code that lets only you talk to your EC2 computer. You download this key when you create your EC2. Remember to keep this key safe and don't lose it!

Remember! This key should end with .pem and you’ll save it somewhere safe on your Mac.

Step 2: Use the Terminal

The Terminal on your Mac is like a magic wand that lets you type commands to do things on your computer. You will use it to tell your Mac how to talk to the EC2 computer.

  1. Open your Terminal. You can find it by going to your Applications folder, then Utilities, and you'll see Terminal.

  2. You need to move to the place where you kept your key. If you put it in a folder called "MyKeys", you type:

     cd MyKeys
    

Step 3: Prepare Your Key

Before you use your key, you need to make sure it's safe and only you can look at it. You tell your Mac this by typing:

chmod 400 my-key.pem

Here, my-key.pem is your key's name. This command is like telling your Mac, "Only let me look at this key!"

Common AMIs and Their Default Usernames

When you connect to an EC2 instance, the username you use can depend on the Amazon Machine Image (AMI) that the instance is based on. Each type of AMI has a default username. Here’s a simple guide on what username to use for some of the most common AMIs:

  1. Amazon Linux AMI - The default username is ec2-user.

  2. Ubuntu - The default username for Ubuntu servers is ubuntu.

  3. Red Hat Enterprise Linux - Use ec2-user as the default username.

  4. CentOS - This also uses ec2-user as the default username.

  5. Fedora - The username is fedora.

  6. Debian - For Debian servers, the default username is admin.

  7. SUSE - Use ec2-user or root as the default username for SUSE servers.

How to Use the Username in Your Connection Command

When you use the ssh command to connect to your EC2 instance, include the appropriate username for your AMI. Here's how you format your command:

ssh -i your-key-name.pem username@your-instance-address

Replace your-key-name.pem with the name of your key file, username with the correct username for your AMI, and your-instance-address with the public DNS or IP address of your instance.

Step 4: Connect to Your EC2 Computer!

Now, you're ready to talk to your EC2 computer! You will need the EC2 computer's address from Amazon. It looks something like ec2-123-45-678-910.compute-1.amazonaws.com.

You type this command in your Terminal:

ssh -i my-key.pem ec2-user@ec2-123-45-678-910.compute-1.amazonaws.com

Replace my-key.pem with your key's name and the big address with your EC2’s address.

When you press Enter, your Mac will ask if you really want to talk to this EC2 computer. You type yes, and you're in! You’re now talking to your EC2 computer.

0
Subscribe to my newsletter

Read articles from Abdul Firoz directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abdul Firoz
Abdul Firoz

I am a DevOps engineer from India.