# 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 &gt; 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):

```powershell
#!/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` or `Anywhere` (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**.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751105547140/2811b3bf-ef4b-45b3-8d65-22f76f1f4ad3.png align="center")

#### **6\. Connect to EC2 via SSH**

* Open terminal or PowerShell
    

```powershell
ssh -i "task-3-insatnce-key.pem" ec2-user@ec2-<public ip>.compute-1.amazonaws.com
```

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">🔁 <strong>Accessing One EC2 from Another Using Root Password SSH</strong></div>
</div>

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:
    

```powershell
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:
    

```powershell
ssh <main-server-private>
#enter passwd:-demo@123
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751106839400/fbec4f0c-b980-4ae5-99b2-33d4b2903b5d.png align="center")

> ✅ **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**
