# Mastering EC2 User Management: A Comprehensive Guide

Welcome to the world of AWS EC2 instances, where efficient user management is the key to unlocking the full potential of your cloud computing experience. In this blog, we'll dive deep into adding, deleting, and fine-tuning user accounts on your EC2 instances. Buckle up for a journey through the ins and outs of EC2 user management.

**1\. Create an EC2 instance**

Our ec2 instance named **"ubuntuinstance"** has been created in the AWS cloud.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696151844273/39c884ac-9a0d-41a7-b6d7-17ed9137a2b6.png align="center")

**2\. Connect to the ec2 instance via ssh**

We are connected to our ec2 instance successfully.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696152018527/df0a5cf4-e627-4f0b-80bb-23ec17707cd1.png align="center")

> Before creating a new user let's create a key pair named "**ubuntu2-kp.pem**" which will be used to generate the public key later. In the end, we will be able to log into the instance as a new user **(ubuntu2)** instead of the default user (**ubuntu**)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696154831014/fcacc728-a789-4e3e-b9f4-da6fb14fa9d8.png align="center")

**3\. Add the new user in the ec2 instance**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696152612799/b0658297-4141-4c2d-9100-7a000356b507.png align="center")

**Verify the user**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696152857635/faf8e0d3-9fdf-437c-b137-31ad0b9f7b88.png align="center")

1. **Switch to the new user so that the directory and file that you create will have the proper ownership.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696153011414/c3ebc3dd-6b73-49dc-a07b-3bf3c0bd7d6a.png align="center")
    
    The prompt changes from `ubuntu` to `ubuntu2` to indicate that you have switched the shell session to the new user.
    
2. **Add the SSH public key to the user. First, create a directory in the user's home directory for the SSH key file, then create the key file, and finally paste the public key into the key file, as described in the following sub-steps.**
    

a.Create a `.ssh` directory in the `newuser` home directory and change its file permissions to `700`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696153715619/f52f1298-60e2-44e2-be61-ebe44e48f589.png align="center")

b. Create a file named `authorized_keys` in the `.ssh` directory and change its file permissions to `600`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696153812650/f3251ae6-64be-488a-bd57-d00202958b2e.png align="center")

c. Open the `authorized_keys` file using your favorite text editor and paste the public key

First, we need to generate the public key for the private key created earlier.We can see our public key generated here. We will copy this key and paste it to <mark>.ssh/authorized_keys</mark>

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696154114740/f295b1e4-282e-41d8-b682-490f2a7e0018.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696154342228/491e048c-325a-4962-896a-e01740495bac.png align="center")

We pasted the public key that we generated earlier.

Now we should be able to log into the **<mark>ubuntu2</mark>** user on the instance, using the private key that corresponds to the public key that we added to the `authorized_keys` file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696155086495/7a713298-44a8-4774-ad3c-3291ee2be33f.png align="center")

Hurray!! We are successfully logged in as a **ubuntu2** user using the private key that we created separately.

#### Modifying User Permissions

Fine-tune a user's permissions by adjusting their group memberships and file permissions. For example, to change a file's owner:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696155942919/c2344c6a-6289-4aec-ba03-2d4f9ddd0f6f.png align="center")

#### Changing User's Group

Switching a user from one group to another is a breeze with the `usermod` command:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696156124875/545492a9-06fe-407d-beb0-27d28cc48729.png align="center")

Here ubuntu user is added to **<mark>ubuntu2</mark>** group.

#### Deleting a User

If the time comes to bid farewell to a user, use the `userdel` command:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696156416569/60a875db-3e19-40af-b963-84589a5093cd.png align="center")

In conclusion, each AWS professional must possess mastery of EC2 user management. You may create a safe and effective cloud computing environment by adding, removing, and modifying user accounts. To take control of your EC2 instance and improve your AWS experience, adhere to the following procedures and best practices. Cloud computing success!

Happy Learning!!
