Steps to login to EC2 Linux Instance without SSH keypair

Steps to login to EC2 Linux Instance without SSH keypair

In this blog, we will show you the steps to login to EC2 Linux Instance without SSH Keypair.

ENVIRONMENT OVERVIEW

  • We have created an EC2 Linux Instance without a key pair.
steps to login to EC2 Linux Instance without SSH keypair

CREATING IAM ROLE

  • Open the IAM service from the AWS Management Console.
steps to login to EC2 Linux Instance without SSH keypair
  • Click on Roles Option.
steps to login to EC2 Linux Instance without SSH keypair
  • Then click on Create role button.
steps to login to EC2 Linux Instance without SSH keypair
  • Select the EC2 service and click on the Next button.
steps to login to EC2 Linux Instance without SSH keypair
  • Search for AmazonEC2RoleforSSM and select it and click on Next.
steps to login to EC2 Linux Instance without SSH keypair
  • We haven’t added any Tags for this demo. Click on Next.
steps to login to EC2 Linux Instance without SSH keypair
  • Provide the name and description for this role and click on Create role button.
steps to login to EC2 Linux Instance without SSH keypair
  • The role has been created successfully.
steps to login to EC2 Linux Instance without SSH keypair

ADDING INLINE POLICY

  • Open the newly created role and click on Add Inline Policy.
steps to login to EC2 Linux Instance without SSH keypair
  • Click on JSON Tab and paste the below script.
steps to login to EC2 Linux Instance without SSH keypair

{
   “Version”: “2012-10-17”,
   “Statement”: [
     {
       “Effect”: “Allow”,
       “Action”: [
         “ssm:PutParameter”
       ],
       “Resource”: [
         “arn:aws:ssm:region:account_id:parameter/EC2Rescue/Passwords/<instanceid>
       ]
     }
   ]
}

  • Update the region, account ID and Instance ID and click on Review policy.
steps to login to EC2 Linux Instance without SSH keypair

Note:  You can find the above code in this URL: https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2rw-ssm.html

  • Provide a name and click on create policy.
steps to login to EC2 Linux Instance without SSH keypair
  • The In-policy is created successfully.
steps to login to EC2 Linux Instance without SSH keypair

CREATING AN ENCRYPTION KEY

  • Search for KMS in the AWS Management console.
steps to login to EC2 Linux Instance without SSH keypair
  • Click on the Customer Managed Keys.
steps to login to EC2 Linux Instance without SSH keypair
  • Click on the create key button.
steps to login to EC2 Linux Instance without SSH keypair
  • Provide the Alias and Description and click on the Next button.
steps to login to EC2 Linux Instance without SSH keypair
  • No tags were provided for this key. Click Next to continue.
steps to login to EC2 Linux Instance without SSH keypair
  • Search for the role which we created earlier and select it.
steps to login to EC2 Linux Instance without SSH keypair
  • Select the same IAM role which we created earlier to Define key usage permissions and click Next.
steps to login to EC2 Linux Instance without SSH keypair
  • Review the policy and click on the Finish button.
steps to login to EC2 Linux Instance without SSH keypair

ATTACH AN IAM ROLE TO AN INSTANCE

  • Open the EC2 Management Dashboard and click on the Instances link.
steps to login to EC2 Linux Instance without SSH keypair
  • Select the instance and click on Actions – Instance Settings – Attach/Replace IAM Role link.
steps to login to EC2 Linux Instance without SSH keypair
  • Select our newly created IAM role from the drop-down list and click on the Apply button.
steps to login to EC2 Linux Instance without SSH keypair
  • IAM Role has been added successfully.

ASSIGNING THE KMS TO SESSION MANAGER

  • Search for SSM in AWS Management Console.
steps to login into EC2 Linux Instance without SSH key pair
  • Select the Session Manager link.
  • Then click on the Preferences Tab.
  • Then select the Edit Button.
  • Click on the Key Management Service (KMS) checkbox under the preferences section.
  • Scroll down and select our newly created key in KMS. In addition, click on the Save button.
  • Now our KMS key has been associated with the Session Manager.

RESET THE ROOT USER PASSWORD

  • Search for SSM in AWS Management Console.
steps to login into EC2 Linux Instance without SSH key pair
  • Click on Managed Instances under Instances & Nodes topic and make sure that your instance is available in the list.
  • Select the instance and click on the Reset password.
  • It will prompt you to update the SSM client. Click on the update button.
  • After a few seconds, It will prompt you the text box to provide the username. Type root and click on Submit.
  • Now type the new password twice for the root account and click Done.
  • Scroll down and click on the session manager link under Instances & Nodes section.
  • Click on the Start session.
  • Now select the instance and again click on the start session.
  • A session window will open for your selected instance.

ENABLING THE ROOT USER ACCESS

  • First, change to root user using su – root command.
steps to login into EC2 Linux Instance without SSH key pair
  • Go to /etc/ssh/ and open sshd_config through VI Editor.
  • Permit the root login by removing the # tag.
  • Also, please enable password authentication in addition to saving the changes.
  • Save and exit the Editor.
  • Restart the SSHD service using the below command.

systemctl restart sshd.service

VERIFICATION

  • Access the Linux EC2 Instance using public IP through SSH.
steps to login into EC2 Linux Instance without SSH key pair
  • We are able to login to the EC2 Linux instance using root account without SSH key Pair.

REFERENCE

Keypair

Thanks for reading this blog. We hope it was useful for you to know about the steps to login to EC2 Linux Instance without SSH keypair.

Loges

Leave a Reply

Your email address will not be published. Required fields are marked *