Accessing your EC2 instances using SSH

Amazon Elastic Compute Cloud (EC2) is a web service that offers resizable compute capacity in the cloud. It gives users the ability to quickly and easily launch virtual machines called instances, which can be used to run applications and workloads. When working with EC2 instances, it is often necessary to access them over a secure shell (SSH) connection for configuration, maintenance, and troubleshooting.

In this tutorial, we will be taking a closer look at how to access your EC2 instances using SSH. We’ll go through each step of the process, from creating a key pair to logging in to your instance. This tutorial assumes that you already have an AWS account and have created an EC2 instance.

Creating a Key Pair

The first step in accessing your EC2 instance using SSH is to create a key pair. A key pair is a set of public and private keys that are used to securely access your instance. Here’s how to create a key pair:

  1. Log in to the AWS Management Console.
  2. Navigate to the EC2 dashboard.

  3. In the navigation pane, click on “Key Pairs.”

  4. Click the “Create Key Pair” button.

  5. Enter a name for your key pair.

  6. Click the “Create” button.

  7. Your private key file will automatically be downloaded to your computer. Be sure to store it in a safe location, as you’ll need it to access your EC2 instance.

Alternatively, you can create a key pair using the AWS Command Line Interface (CLI). Here’s the command to create a key pair:

aws ec2 create-key-pair --key-name my-key-pair --query 'KeyMaterial' --output text > my-key-pair.pem

This command creates a new key pair with the name “my-key-pair” and stores the private key in a file called “my-key-pair.pem” on your local computer.

Setting Up Security Group Rules

Before you can access your instance using SSH, you need to make sure that inbound traffic is allowed on the port you’ll be using for SSH. By default, SSH traffic uses port 22.

Here’s how to add an inbound rule to your security group:

  1. Log in to the AWS Management Console.
  2. Navigate to the EC2 dashboard.

  3. In the navigation pane, click on “Security Groups.”

  4. Select the security group associated with your instance.

  5. Click on the “Inbound” tab.

  6. Click the “Edit” button.

  7. Click the “Add Rule” button.

  8. Under “Type,” select “SSH.”

  9. Under “Source,” select “My IP” to allow inbound traffic from your IP address.

  10. Click the “Save” button.

You now have a security group rule that permits inbound SSH traffic to your instance.

Accessing Your Instance Using SSH

Now that you’ve created a key pair and set up security group rules, you’re ready to access your EC2 instance using SSH.

Here’s how to SSH into your instance:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where your private key file is stored.

  3. Change the permissions of your private key file to 400:

chmod 400 my-key-pair.pem
  1. Use the following command to connect to your instance:
ssh -i my-key-pair.pem ec2-user@public-dns-name

Replace “my-key-pair.pem” with the name of your private key file and “public-dns-name” with the public DNS name of your instance. The “ec2-user” is the default username for Amazon Linux instances.

If you’re using a different operating system, the default username may be different. Here’s a list of default usernames for different operating systems:

  • Amazon Linux: ec2-user
  • Ubuntu: ubuntu
  • RHEL and CentOS: ec2-user or root
  • SUSE: ec2-user or root
  • Windows: Administrator

Once you’ve successfully connected to your instance using SSH, you’ll be able to execute commands and perform administrative tasks.

Troubleshooting SSH Connection Issues

If you’re having trouble connecting to your instance using SSH, here are a few things you can try:

  • Verify that your security group rules allow inbound traffic on port 22.
  • Check that your key pair is correctly configured and stored in the correct directory.
  • Check that your private key file has the correct permissions (400).
  • Verify that the public DNS name of your instance is correct.
  • Check that your instance is running and in a state that allows SSH connections.
  • Try connecting again after a few moments in case there was a network issue.

By following these steps, you should be able to access your EC2 instance using SSH.

Conclusion

Accessing your EC2 instances using SSH is an essential skill for anyone working with Amazon Web Services. By creating a key pair, setting up security group rules, and establishing a secure shell connection, you can quickly and easily perform administrative tasks and troubleshoot issues on your instances.

This tutorial covered the basics of accessing your EC2 instances using SSH. We looked at how to create a key pair, set up security group rules, and troubleshoot common connection issues. If you follow these steps, you should be able to access your EC2 instances with ease.

Related Post