Step-by-Step Guide to SSH into Amazon EC2

·

2 min read

Step-by-Step Guide to SSH into Amazon EC2

In this blog, I want to guide you on how to SSH into an EC2 instance. There are some prerequisites that need to be met:

  1. Git (In case if you are using Windows)

    Personally, I am neither a fan nor a hater of Windows. Git software makes developer life a bit easier. So, I recommend using Git for developers. Here is the link, you can download Git for free Git Software

  2. Keys

    Either we can create our own public and private key or AWS can create them for us either while creating the instance or we can create key pair individually under

    EC2 Section -> Network Security -> Key Pairs.

    Keys created by AWS

    In this scenario, the public key will already be set in .ssh/authorized_keys directory. The private key will be downloaded to our PC. Open Git where the private key is stored or you can change the directory manually using the cd command. Run the below command and there you go, you have successfully made an SSH connection.

     ssh -i privatekey localuser@ec2-IP.ap-south-1.compute.amazonaws.com
    
    💡
    localuser will be ubuntu for the ubuntu OS.

    Keys created by User

    Run the following command to generate a new SSH key pair. Replace your_email@example.com with your actual email address.

ssh-keygen -t ed25519 -C "your_email@example.com"
  • Files will be generated in the current directory. Here you can use the cat command to see both keys. Copy the public key.
cat publickey.pub #Copy the public key
  • Connect to the EC2 Instance using EC2 Instance Connect and paste it into the authorized_keys file using commands below:

cd ~/.ssh
nano authorized_keys #Paste the public key
  • Now, give the below command to restart the SSH service:
sudo systemctl restart ssh
  • Next, give the below command. Make sure you are in the same directory where private key is stored, or you can specify the path as well:
ssh -i privatekey localuser@ec2-IP.ap-south-1.compute.amazonaws.com

There you go, you have successfully made an SSH connection to the EC2 Instance.

Thank you for reading the article.