Automated Root SSH Access Setup via Shell Script on EC2


☁️ Step-by-Step EC2 Instance Setup with Root SSH Access
✅ 1. Launching a New EC2 Instance
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
✍️ 2. Add User Data Script (Root SSH Setup)
- Scroll down to Advanced Details
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@123
to your secure custom password
🔒 3. Configure Security Group
Add Inbound Rule:
Type: SSH
Port:
22
Source:
My IP
orAnywhere
(for testing only)
4. Generate or Use Existing Key Pair
Create a new key pair (
.pem
) or use an existing one.Save it securely — required for connecting via SSH.
🚀 5. Launch the Instance
Click Launch and wait for the instance to start.
Once running, note the Public IPv4 DNS or Public IP.
6. Connect to EC2 via SSH
- Open terminal or PowerShell
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:
🧩 1. Launch a Second EC2 Instance
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
🔗 2. Connect to Second EC2 (the “Client”)
- SSH into the second EC2 from your local machine:
ssh -i "second-ec2-key.pem" ec2-user@<Second-EC2-Public-IP>
🔐 3. SSH into First EC2 from Second (Using Password Login)
- From the second EC2 terminal, run:
ssh <main-server-private>
#enter passwd:-demo@123
✅ Access granted! You’re now logged in as
root
to the first EC2 from the second.
🛡️ Security Tip
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
Subscribe to my newsletter
Read articles from Gujjar Apurv directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Gujjar Apurv
Gujjar Apurv
Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journey—making complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."