Changing the Default Password on an EC2 Ubuntu Instance
Tags: webdev, javascript, beginners, programming
Published: 2024-06-02 13:00:54

# Changing the Default Password on an EC2 Ubuntu 14 Instance

Amazon EC2 (Elastic Compute Cloud) is a popular web service that provides resizable compute capacity in the cloud. It's often used for hosting applications, websites, and other workloads. When launching an EC2 instance with Ubuntu 14.04, the default user is typically `ubuntu`, and by default, this user doesn't have a password set. Instead, you access the instance using SSH key pairs. However, there might be situations where you need to set or change the password for the `ubuntu` user or other users. This guide will walk you through the process of setting and changing the password on an EC2 Ubuntu 14 instance.

## Prerequisites

- An AWS account.
- An EC2 instance running Ubuntu 14.04.
- SSH key pair associated with your EC2 instance.
- SSH client (e.g., PuTTY for Windows or Terminal for macOS/Linux).

## Step-by-Step Guide

### Step 1: Access Your EC2 Instance

1. **Open your terminal or SSH client.**
   
2. **Connect to your EC2 instance using SSH.** Replace `<your-key-pair>.pem` with your actual key pair file and `ec2-user@your-instance-public-dns` with your instance's public DNS.

   ```sh
   ssh -i <your-key-pair>.pem ubuntu@your-instance-public-dns
   ```

   Example:

   ```sh
   ssh -i my-key-pair.pem ubuntu@ec2-203-0-113-25.compute-1.amazonaws.com
   ```

### Step 2: Set or Change the Password for the `ubuntu` User

Once you are logged in to your instance, you can set or change the password for the `ubuntu` user.

1. **Set the password using the `passwd` command.**

   ```sh
   sudo passwd ubuntu
   ```

2. **Enter a new password when prompted.** You will need to confirm the password by typing it again.

   ```sh
   Enter new UNIX password:
   Retype new UNIX password:
   passwd: password updated successfully
   ```

### Step 3: Verify the Password Change

To ensure that the password change was successful, you can try switching to the `ubuntu` user and using the new password.

1. **Switch to the `ubuntu` user.**

   ```sh
   su - ubuntu
   ```

2. **Enter the new password when prompted.**

   ```sh
   Password:
   ```

If you are not prompted for the password or if you want to log in using the password, you may need to adjust the SSH settings to allow password authentication.

### Step 4: Enable Password Authentication (Optional)

By default, EC2 instances use key pair authentication, and password authentication might be disabled. To enable password authentication:

1. **Edit the SSH configuration file.**

   ```sh
   sudo nano /etc/ssh/sshd_config
   ```

2. **Find the following lines and modify them as shown:**

   ```sh
   PasswordAuthentication yes
   PermitRootLogin yes
   ```

3. **Save the file and exit the editor (Ctrl+O, Enter, Ctrl+X in nano).**

4. **Restart the SSH service to apply the changes.**

   ```sh
   sudo service ssh restart
   ```

### Step 5: Test Password Authentication

1. **Open a new terminal or SSH client window.**

2. **Connect to your EC2 instance using the `ubuntu` username and the new password.**

   ```sh
   ssh ubuntu@your-instance-public-dns
   ```

   You should now be able to log in using the password you set.

## Conclusion

Changing the default password on an EC2 Ubuntu 14 instance is straightforward. By following these steps, you can ensure that your instance is secure and that you have the necessary access methods in place. Always remember to revert the SSH settings to disable password authentication if it is no longer needed to maintain security best practices.