Day 4 [part :- 2] :- 🔗 Connecting Two EC2 Instances via SSH: A Step-by-Step Guide 🚀

Day 4 [part :- 2] :- 🔗 Connecting Two EC2 Instances via SSH: A Step-by-Step Guide 🚀

·

3 min read

• How to Connect One EC2 Instance to Another EC2 Instance

In this guide, we'll walk through the steps to connect one EC2 instance to another using SSH. This is a common task when you need to manage multiple instances or transfer files between them.

Prerequisites

  • Two running EC2 instances.

  • SSH key pair for accessing the instances.

  • Security group rules allowing SSH access.

Step-by-Step Guide

In the example, we have two EC2 instances:

  1. 172.31.33.186 (Linux)

  2. 172.31.22.225 (Server) - We want to connect to this server.

First, on the 172.31.33.186 (Linux) instance, create an SSH private and public key using the following command:

ssh-keygen

Navigate to the .ssh directory using the cd command:

Copy the public key (id_ed255519.pub):

Next, on the 172.31.22.225 (Server) instance, navigate to the .ssh directory and paste the public key from 172.31.33.186 (Linux) into the authorized_keys file:

Copy the public DNS of the 172.31.22.225 (Server):

On the 172.31.33.186 (Linux) terminal, use the following command to connect:

ssh -i #private_key# ubuntu@public_dns#

• How to Copy and Transfer Files to the Jump Server

To transfer files to the jump server, use the following command:

scp -i #private_key# ubuntu@#public_dns#:/hpme/ubuntu/path

You can then verify the files on the server:

• What is systemctl ? explain with example

is a command-line utility in Ubuntu (and other Linux distributions using systemd) that allows you to control the systemd system and service manager. It is used to manage and inspect system services, check the status of services, enable or disable services to start on boot, and more.

Example: Managing Docker with systemctl

use given below command for installation of docker

sudo apt-get update
sudo apt install docker.io
  1. Check the Status of the Docker Service:

     sudo systemctl status docker
    

    This command checks the status of the Docker service, showing whether it is active, inactive, or failed.

  2. Start the Docker Service:

     sudo systemctl start docker
    

    This command starts the Docker service if it is not already running.

  3. Stop the Docker Service:

     sudo systemctl stop docker
    

    This command stops the Docker service.

  4. Restart the Docker Service:

     sudo systemctl restart docker
    

    This command restarts the Docker service, which can be useful if you have made configuration changes.

  5. Enable Docker to Start on Boot:

     sudo systemctl enable docker
    

    This command configures the Docker service to start automatically when the system boots.

  6. Disable Docker from Starting on Boot:

     sudo systemctl disable docker
    

    This command prevents the Docker service from starting automatically at boot.

  7. Reload Docker Service Configuration:

     sudo systemctl reload docker
    

    This command reloads the Docker service configuration without stopping the service.

By using systemctl, you can effectively manage the Docker service on your Ubuntu system, ensuring it runs smoothly and starts automatically when needed.

Thank you for reading!

© 2024 Anand Raval. All rights reserved.

Â