{"id":4160,"date":"2023-11-04T23:14:06","date_gmt":"2023-11-04T23:14:06","guid":{"rendered":"http:\/\/localhost:10003\/accessing-your-ec2-instances-using-ssh\/"},"modified":"2023-11-05T05:47:58","modified_gmt":"2023-11-05T05:47:58","slug":"accessing-your-ec2-instances-using-ssh","status":"publish","type":"post","link":"http:\/\/localhost:10003\/accessing-your-ec2-instances-using-ssh\/","title":{"rendered":"Accessing your EC2 instances using SSH"},"content":{"rendered":"

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.<\/p>\n

In this tutorial, we will be taking a closer look at how to access your EC2 instances using SSH. We\u2019ll 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.<\/p>\n

Creating a Key Pair<\/h3>\n

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\u2019s how to create a key pair:<\/p>\n

    \n
  1. Log in to the AWS Management Console.<\/p>\n<\/li>\n
  2. \n

    Navigate to the EC2 dashboard.<\/p>\n<\/li>\n

  3. \n

    In the navigation pane, click on \u201cKey Pairs.\u201d<\/p>\n<\/li>\n

  4. \n

    Click the \u201cCreate Key Pair\u201d button.<\/p>\n<\/li>\n

  5. \n

    Enter a name for your key pair.<\/p>\n<\/li>\n

  6. \n

    Click the \u201cCreate\u201d button.<\/p>\n<\/li>\n

  7. \n

    Your private key file will automatically be downloaded to your computer. Be sure to store it in a safe location, as you\u2019ll need it to access your EC2 instance.<\/p>\n<\/li>\n<\/ol>\n

    Alternatively, you can create a key pair using the AWS Command Line Interface (CLI). Here\u2019s the command to create a key pair:<\/p>\n

    aws ec2 create-key-pair --key-name my-key-pair --query 'KeyMaterial' --output text > my-key-pair.pem\n<\/code><\/pre>\n

    This command creates a new key pair with the name \u201cmy-key-pair\u201d and stores the private key in a file called \u201cmy-key-pair.pem\u201d on your local computer.<\/p>\n

    Setting Up Security Group Rules<\/h3>\n

    Before you can access your instance using SSH, you need to make sure that inbound traffic is allowed on the port you\u2019ll be using for SSH. By default, SSH traffic uses port 22.<\/p>\n

    Here\u2019s how to add an inbound rule to your security group:<\/p>\n

      \n
    1. Log in to the AWS Management Console.<\/p>\n<\/li>\n
    2. \n

      Navigate to the EC2 dashboard.<\/p>\n<\/li>\n

    3. \n

      In the navigation pane, click on \u201cSecurity Groups.\u201d<\/p>\n<\/li>\n

    4. \n

      Select the security group associated with your instance.<\/p>\n<\/li>\n

    5. \n

      Click on the \u201cInbound\u201d tab.<\/p>\n<\/li>\n

    6. \n

      Click the \u201cEdit\u201d button.<\/p>\n<\/li>\n

    7. \n

      Click the \u201cAdd Rule\u201d button.<\/p>\n<\/li>\n

    8. \n

      Under \u201cType,\u201d select \u201cSSH.\u201d<\/p>\n<\/li>\n

    9. \n

      Under \u201cSource,\u201d select \u201cMy IP\u201d to allow inbound traffic from your IP address.<\/p>\n<\/li>\n

    10. \n

      Click the \u201cSave\u201d button.<\/p>\n<\/li>\n<\/ol>\n

      You now have a security group rule that permits inbound SSH traffic to your instance.<\/p>\n

      Accessing Your Instance Using SSH<\/h3>\n

      Now that you\u2019ve created a key pair and set up security group rules, you\u2019re ready to access your EC2 instance using SSH.<\/p>\n

      Here\u2019s how to SSH into your instance:<\/p>\n

        \n
      1. Open your terminal or command prompt.<\/p>\n<\/li>\n
      2. \n

        Navigate to the directory where your private key file is stored.<\/p>\n<\/li>\n

      3. \n

        Change the permissions of your private key file to 400:<\/p>\n<\/li>\n<\/ol>\n

        chmod 400 my-key-pair.pem\n<\/code><\/pre>\n
          \n
        1. Use the following command to connect to your instance:<\/li>\n<\/ol>\n
          ssh -i my-key-pair.pem ec2-user@public-dns-name\n<\/code><\/pre>\n

          Replace \u201cmy-key-pair.pem\u201d with the name of your private key file and \u201cpublic-dns-name\u201d with the public DNS name of your instance. The \u201cec2-user\u201d is the default username for Amazon Linux instances.<\/p>\n

          If you\u2019re using a different operating system, the default username may be different. Here\u2019s a list of default usernames for different operating systems:<\/p>\n