Learn to create a user with root privileges in Ubuntu 22.04. This guide covers user creation, sudo access, and privilege verification step-by-stepbyHasanul Haque Banna

How to Create and Manage a User in Ubuntu

Introduction

In the world of Ubuntu and other Linux distributions, managing user privileges is a crucial task. The root user has the highest level of access, with the ability to execute any command and modify any file. However, for security and administrative purposes, it's often useful to grant another user similar privileges. This guide will walk you through creating a user named "hasanulhaquebanna" in Ubuntu 22.04 and granting them the same privileges as the root user.

Prerequisites

I am using my Windows OS with Windows Subsystem for Linux (WSL) active, where I have installed Ubuntu 22.04. Additionally, I am working with a Virtual Private Server (VPS) for the remainder of this tutorial. This setup ensures flexibility and accessibility across different environments.

Blog Post Image by Hasanul Haque Banna

4 Steps to Create a User in Ubuntu

  1. Create a New User
  2. Add the User to the Sudo Group
  3. Verify User Privileges
  4. Log in with the New User

Step 1: Create a New User

To create a new user in Ubuntu, you can use the terminal. Here’s how:

  • Open your terminal or connect to your VPS via SSH
ssh vps_username@vps_ip

in my case

Blog Post Image by Hasanul Haque Banna

after that it will ask to generate some random key to access the terminal for the future, type `yes` to continue

Blog Post Image by Hasanul Haque Banna

and then the system will ask password for the following root user and then we will get access to the Virtual Private Server (VPS) like below

Blog Post Image by Hasanul Haque Banna
  • Type the following command to create a new user:
sudo adduser username

in my case i have go thorugh my own name

Blog Post Image by Hasanul Haque Banna

after that, provide a password for the newly created user and retype it. Here, You can skip the data-filling step by pressing the ENTER button each time and finally press Y to confirm.

Blog Post Image by Hasanul Haque Banna

* Let see why we run these commands and the explanation

  • sudo: Grants administrative privileges.
  • adduser: Command to add a new user.
  • hasanulhaquebanna: The username for the new user.

Finally, Let's check to verify whether the user has been created or not by typing the following command

getent passwd | grep hasanulhaquebanna

In here should see an entry confirming the creation of the user hasanulhaquebanna. Let's check it

Blog Post Image by Hasanul Haque Banna

Here we have created a user named `hasanulhaquebanna` under home directory.

* Let see why we run these commands and the explanation

  • getent: Looks into the administrative database.
  • passwd: Database containing user information.
  • grep: Searches for the specified pattern (username).

Step 2: Add the User to the Sudo Group

Adding the user to the sudo group grants them administrative privileges necessary for root-level tasks.

  • Type the following command:
sudo usermod -aG sudo username
  • and for verifying user's group membership do following
groups username

In my case, I have enter my username `hasanulhaquebanna` and verify the user is listed in sudo group by following

Blog Post Image by Hasanul Haque Banna

Here, hasanulhaquebanna listed in the sudo group, indicating that the user now has administrative privileges.

*Let see why we run these commands and the explanation

  • usermod: Modifies an existing user account.
  • -aG: Adds the user to the specified group.
  • sudo: The group that grants administrative privileges.
  • groups: Displays the groups that the specified user belongs to.

Step 3: Verify User Privileges

To ensure the new user has the necessary privileges, follow these steps:

  • Switch to the new user by typing:
su - username

after then enter the corresponding user password (in my case I have done previously so it won't ask me) we will get the following new shell for the new user

Blog Post Image by Hasanul Haque Banna

we can see from the above picture that the `root` user has been changed to `hasanulhaquebanna` e.g the shell.

  • now need to Edit the sudoers file by typing:
sudo visudo

after the command we need to provide password for the new user and then will get a nano text editor where we would add new user and give all permission as like `root`.

Blog Post Image by Hasanul Haque Banna

Now save the file by pressing CTRL+ X and then the Y buttons. Next press the ENTER button and user privileges will be set.

  • Check the user privileges by typing:
sudo whoami

If everything is set up correctly, the command should return root, indicating that hasanulhaquebanna has root privileges as like below

Blog Post Image by Hasanul Haque Banna


Step 4: Log in with the New User

To log in as the new user with root privileges, we need to know the various methods to switch users and use their privileges:

  • Using the sudo command:
sudo bash

This command will change the prompt to indicate the root user has performing as it should, let's see

Blog Post Image by Hasanul Haque Banna

  • Using the su command:
Blog Post Image by Hasanul Haque Banna

This command switches to the root user shell again from `hasanulhaquebanna`. Also there has lot of other commands to switches to root user shell like- sudo su -, sudo -i, sudo -s .

Conclusion

Granting a user root-like privileges in Ubuntu 22.04 involves a few simple steps. By creating a new user and adding them to the sudo group, you can ensure they have the necessary administrative access to manage the system effectively. Always exercise caution when granting such privileges to avoid unintended system changes.

This guide provides a straightforward approach to managing user privileges, ensuring that even beginners can follow along and successfully configure their Ubuntu system.