AWS

How to Connect to an EC2 Instance Using SSH In Linux and macOS

Have you ever launched an EC2 instance and don’t know how to log in to your brand-new instance? It may sound difficult for a beginner, but it’s simple. I will help connect to an EC2 instance using SSH, whether you are using Linux or macOS.

What do you Need to Connect to an EC2 Instance with SSH in Linux?

  • SSH Key (.pem file) provided by Amazon. This SSH key is provided by AWS when you launch the instance.
  • Public IP address: IP address assigned to your EC2 instance.
  • Username. The username depends on the Linux distro you just launched. Usually, these are the usernames for the most common distributions:
  • Ubuntu: ubuntu
  • Amazon Linux: ec2-user
  • Centos: root

How to Connect to an EC2 Instance using SSH in Linux and macOS

1. Open your terminal and change the directory with the command cd, where you downloaded your pem file. In this demonstration, pem file is stored in the downloads folder.


 cd Downloads

This is the explanation of the previous command:

2. Locate the required values in order to connect to your EC2 Instance:

  • SSH Key: This key is usually located in the Downloads folder when the instance was launched. The name was provided during the instance launch process. In this example, the key name is called “ec2_tuturial” with the extension .pem. In the CLI , you can find it like this:

In the macOS Finder

EC2 Public IP Address: AWS assigned a public IP address to the instance, if you’re unable to locate it, go to the EC2 dashboard in the AWS console and go to the instance launched, then go Details and copy the Public IPv4 address, in this example, the value is 3.208.71.105

OS User: Depending on which OS you used on the instance, the default OS user may vary, for Ubuntu distributions use “ubuntu”, for Amazon Linux OS images, use “ec2-user” and for other Linux distributions like CentOS use “root”. 

3. Type the SSH command with this structure:

ssh -i instance_key_file.pem
instance_os_user@instance_public_ip_address

For example, using the values obtained in the previous step, the command would look like this:

ssh -i ec2_tutorial.pem ubuntu@3.208.71.105

This is the explanation of the SSH command:

  • ssh: Command to use SSH protocol
  • -i: Flag that specifies an alternate identification file to use for public key authentication.

4.  Press enter, you will be prompted to add the host to your known_hosts file. Type yes and press enter. This will help to recognize the host each time you’re trying to connect to your instance..

5. And that’s it! Now you’re logged in on your AWS instance

Linux & macOS Troubleshooting

If you struggled to connect to your brand new instance, it is probably that you found some errors, here’s two common errors and how to fix them

This means that the key downloaded does not have proper permissions ,  this is fixable by running this command:

chmod 400 key_file.pem
  1. Replace the “key_file.pem” with your key and run it

Then run the SSH command again and you should be in!

2. Connecting is timing out

This error usually means that the port assigned to the SSH protocol, port number 22, is not allowed to receive incoming connections. Let’s fix this error by adding our Public IP address to the Instance 

3. First, go the AWS console, go to the EC2 Service

4. Go to the Security groups and select the security group attached to the instance, usually AWS names the security groups as “launch-wizard-number” by default. 

5. Go to the inbound rules, and click on Edit inbound rules

6. Click Add rule

7. Type SSH and select SSH Type, by default Protocol and port will be automatically assigned.

8. In the source column, select “My IP”, this will add your Public IP to the security group

9. Finally, click on Save rules

You might also like: What is AWS ECS? – Run Docker in Production and AWS Security Tools

This blog is also available on our DZone profile

Common Questions about Connecting to EC2 Instances Using SSH

What is Amazon EC2?

EC2 (Elastic Cloud Compute) is a service where you can create servers to launch your websites or applications. One of the first things you will find when you create an EC2 instance is the instance type/size. Learn more about creating and choosing the best EC2 instance for your project here.

Why use Amazon EC2?

Since many other providers on the network can host your app, we choose Amazon EC2 because it offers a lot of possibilities. You can start with a small instance, then, when you get more traffic, grow it up to a bigger option, making EC2 more flexible and adaptable than other technologies to host your application.

What are the characteristics of an EC2?

– High availability level.
– High-reliability level.
– Scalable in memory space and server size.
– The hard disk space is independent of the instance size, so it can be set according to your requirements and increased using the AWS service EBS (Elastic Block Store).
– It offers “double” security since, in addition to the default firewall, AWS Security Groups restrict the ports you prefer.

What is SSH, and why is it used to connect to an EC2 instance?

Secure Socket Shell (SSH) enables public-key-based authentication and facilitates encrypted connections between two endpoints in a network. Generating SSH keys to access containers and servers is highly recommended for ​​securing your IT resources, and it is one of many DevOps security best practices.
It’s commonly used to connect to EC2 instances on AWS to manage servers, transfer files, and perform administrative tasks because it provides a versatile and efficient way to remotely manage EC2 instances, configure servers, execute commands, and transfer files, which are essential tasks for maintaining cloud infrastructure on AWS

What should I do if I can’t connect to my EC2 instance using SSH?

Check for common issues such as incorrect security group settings, missing SSH key pair, wrong instance state, or incorrect private key permissions. Ensure that the EC2 instance’s firewall rules allow SSH access.

How can I manage multiple SSH key pairs for different EC2 instances?

You can create and manage multiple SSH key pairs in AWS, associating each one with a specific EC2 instance. You can also use the AWS Systems Manager Session Manager for more secure access without needing an SSH key

Published by
Alfonso Valdes

Recent Posts

End-to-end AI Application Development for Plutoshift

At ClickIT, we deliver high-quality solutions that empower businesses to innovate and scale efficiently. One…

2 hours ago

GitHub Copilot Agent Mode vs Traditional Copilot: How They Differ

GitHub Copilot has transformed how developers approach coding by providing AI-driven suggestions that enhance efficiency…

1 day ago

Deepseek R1 vs OpenAI o1: Complete Comparison

There’s always something new in artificial intelligence. For the last few weeks (and possibly for…

1 week ago

Claude vs GPT: A Detailed Comparison of AI Models

Have you ever considered which AI model would best serve your needs: Claude vs GPT?…

2 weeks ago

AI Agent Frameworks for Advanced Agentic Systems

Just like most software developers build complex applications with trusted development frameworks, Artificial Intelligence engineers…

3 weeks ago

How to Choose Between Tensorflow vs PyTorch in 2025 | Video

Today, we will look at a debate interesting to machine learning enthusiasts: Pytorch vs Tensorflow.…

3 weeks ago