Automated Root SSH Access Setup via Shell Script on EC2

Search for a command to run...

No comments yet. Be the first to comment.
Learn AWS from basics to advanced with daily hands-on tasks and real-world projects. Master core services like EC2, S3, IAM, Lambda, and more all explained in a practical, job-ready DevOps format.
📑 Table of Contents 🌟 Introduction: Why ERPNext? 🎯 Aim of this Blog 🛠️ Prerequisites Before You Begin ⚙️ Step 1: Server Setup – Preparing Ubuntu for ERPNext 📦 Step 2: Install Required Packages 🗄️ Step 3: Configure MySQL Server 🔧 Step 4:...
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices 📖 Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs 📖 Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml 📖 Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices 📖 Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

Go to AWS Management Console > EC2 Dashboard
Click on Launch Instance
Choose an Amazon Machine Image (AMI) — e.g., Amazon Linux 2
Select the desired Instance Type (e.g., t2.micro)
Click Next to move to configuration
Paste the following User Data (shell script):
#!/bin/bash
# Set root password
echo "root:Apurv@123" | chpasswd
# Enable root login and password authentication
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
# Restart SSH service
if systemctl list-units --type=service | grep -q sshd.service; then
systemctl restart sshd
elif systemctl list-units --type=service | grep -q ssh.service; then
systemctl restart ssh
fi
📌 Note: Change
demo@123to your secure custom password
Add Inbound Rule:
Type: SSH
Port: 22
Source: My IP or Anywhere (for testing only)
Create a new key pair (.pem) or use an existing one.
Save it securely — required for connecting via SSH.
Click Launch and wait for the instance to start.
Once running, note the Public IPv4 DNS or Public IP.

ssh -i "task-3-insatnce-key.pem" ec2-user@ec2-<public ip>.compute-1.amazonaws.com
Once the root login and password authentication were enabled using User Data, here’s how I accessed the instance from a second EC2:
Go to AWS EC2 Dashboard again
Launch another instance (can be same AMI and type)
Ensure both instances are in the same VPC or same region (for private IP access)
Add a Security Group rule to allow SSH from the second EC2
ssh -i "second-ec2-key.pem" ec2-user@<Second-EC2-Public-IP>
ssh <main-server-private>
#enter passwd:-demo@123

✅ Access granted! You’re now logged in as
rootto the first EC2 from the second.
This method is useful for internal testing, jump server setup, or remote debugging
Always ensure restricted access via security groups and rotate passwords regularly
For production, prefer using SSH key pairs and non-root users