How to Add and Delete Users on EC2 Ubuntu 22.04

Managing users on an Ubuntu 22.04 system is a common administrative task, especially when dealing with Amazon EC2 instances. Below, you’ll find a step-by-step guide on how to add and delete users on your EC2 Ubuntu 22.04 instance.

Adding Users on Ubuntu 22.04

Step 1: Connect to Your EC2 Instance

Make sure you are logged into your EC2 instance. You can do that using SSH:

ssh -i /path/to/your-key.pem ubuntu@your-ec2-ip

Replace /path/to/your-key.pem with the path to your private key file and your-ec2-ip with your instance’s public IP address or DNS name.

Step 2: Add a New User Account

To create a new user, use the adduser command followed by the username:

sudo adduser newusername

You will be prompted to set and confirm a new password for the account and fill out any additional information, which is optional.

Step 3: Grant Sudo Privileges (Optional)

If you want the new user to have sudo privileges, add them to the sudo group:

sudo usermod -aG sudo newusername

This command appends the user to the sudo group (which has superuser privileges).

Step 4: Verify User Creation

Check if the user has been added successfully:

id newusername

Deleting Users on Ubuntu 22.04

When a user’s access is no longer required, it’s important to remove their account.

Step 1: Delete a User Account

To delete a user without deleting their files, use the deluser command:

sudo deluser username

If you also want to remove the user’s home directory and mail spool, add the --remove-home option:

sudo deluser --remove-home username

Step 2: Remove the User’s Sudo Privileges

Verify that the user no longer has sudo privileges by trying to switch to that user:

sudo -i -u username

You shouldn’t be able to switch to a deleted user.

Step 3: Verify User Deletion

Check that the user is no longer in the system:

getent passwd username

There should be no output, signifying that the user is indeed removed.

Conclusion

User management is essential for maintaining the security and organization of your server. By following these steps, you can easily add or delete users on your EC2 Ubuntu 22.04 instance. Remember to only grant sudo privileges to trusted users who require them for their tasks. Regularly review the user accounts on your instance to maintain optimal security.

That’s all! Now you’re ready to efficiently manage users on your Ubuntu server.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.