Understanding the SSH Command and Connecting Two EC2 Instances

Understanding the SSH Command and Connecting Two EC2 Instances

Secure Shell (SSH) is a powerful tool used for accessing and managing remote servers securely over an encrypted connection. It's a staple in the toolkit of system administrators, developers, and anyone who needs to remotely control a machine. In this blog, we will explore the basics of the SSH command and demonstrate how to connect two EC2 instances using SSH.

What is SSH?

SSH stands for Secure Shell, a cryptographic network protocol used to securely connect to a remote server. It provides a secure channel over an unsecured network by using encryption to protect the data being transferred.

Key Features of SSH

  • Secure Connection: Encrypts all data transmitted between the client and server.

  • Remote Command Execution: Allows for executing commands on a remote machine.

  • File Transfer: Enables secure copying of files to and from the remote server.

  • Port Forwarding: Forwards network traffic securely through the SSH connection.

Basic SSH Command Syntax

The basic syntax of the SSH command is:

shCopy codessh [username]@[hostname]
  • username: The username for logging into the remote server.

  • hostname: The domain name or IP address of the remote serve

Connecting to an EC2 Instance Using SSH

To connect to an EC2 instance, you need:

  1. An EC2 instance running and accessible via the internet.

  2. The private key (.pem) file associated with the EC2 instance.

Step-by-Step Guide

  1. Launch an EC2 Instance:

    • Go to the AWS Management Console.

    • Launch a new EC2 instance.

    • Choose an Amazon Machine Image (AMI).

    • Select an instance type.

    • Configure instance details.

    • Add storage.

    • Add tags (optional).

    • Configure security group (allow SSH traffic on port 22).

    • Review and launch the instance.

    • Download the key pair (.pem file) when prompted.

  2. Connect to the EC2 Instance:

    • Open a terminal (Linux/macOS) or Git Bash (Windows).

    • Change the permissions of your key pair file:

        chmod 400 your-key-pair.pem
      
    • Use the SSH command to connect:

        ssh -i "your-key-pair.pem" ec2-user@your-ec2-public-ip
      

      Replace your-key-pair.pem with the path to your key pair file and your-ec2-public-ip with the public IP address of your EC2 instance.

Connecting Two EC2 Instances

Connecting two EC2 instances involves setting up one instance to accept SSH connections from another instance. Here’s how to do it:

  1. Launch Two EC2 Instances:

    • Follow the same steps as above to launch two instances: Instance A and Instance B.

Step-by-Step

  1. EC2-1 Instance:

    • Generate key-pair :ssh-keygen

    • ls -a

    • cd .ssh

    • ls

    • cat idxxxx.pub

    • copy public key

  2. EC2-2 Instance:

    • ls -a

    • cd .ssh

    • vim authorised_key

    • public key paste it and save

    • cat authorised_key

    • ssh -i private_key_path username@ec2-2 instance_dns