How to Create Sudo User in Ubuntu

How to Create Sudo User in Ubuntu

The sudo command is one of the most popular and powerful commands in Linux distros. It originally stood for superuser do, and it allows a user to execute commands as root or an administrative-level user.

A sudo user is a non-root or normal user who has administrative privileges ordinarily reserved only for the root user. Using the sudo command is more secure than using the root account for a number of reasons, including:

  • It prompts for a password when trying to run a command with elevated privileges. This can reduce the damage that you’d do to your system if you’re not careful. It can give you that extra pause needed to reflect the command you’re running.
  • If you’re allowing another user on your system, they don’t need to know the root password. They can be granted extra privileges temporarily and then they can be taken away.
  • When auditing/logging you can trace who did what because the original user that ran the commands is logged

In this article, we will discuss the step-by-step method to create a sudo user in Ubuntu.

Creating a New Sudo User in Ubuntu

Step 1 – Log in to Your Ubuntu Server

Log into your server as a root user or another sudo user (if you are using a server to remote machine):

ssh [email protected]

Step 2 – Create a New User Account

Create a new user to your system via the adduser command. It allows you to modify the configuration settings of a new user. Replace username with the name of the user you want to create:

adduser username

After adding the user, you will be prompted to create and verify a password for the user. Make sure that the password is strong, and includes both letters and numbers.

The output should look like this:

New password:
Retype password:
passwd: password updated successfully

After verifying the password, you will be asked a series of questions like full name and phone number. All that information is optional. You can just leave the fields blank, and accept defaults by pressing enter (Marked with ⏎ in our example).

Changing the user information for mruser
Enter the new value, or press ENTER for the default
        Full Name []:⏎
        Room Number []:⏎
        Work Phone []:⏎
        Home Phone []:⏎
        Other []:⏎
Is the information correct? [Y/n] Y

The command will create a home directory and copy multiple configuration files from /etc/skel directory to it.

Step 3 – Add The New User to The sudo Group

In Ubuntu and Debian-based systems, members of the sudo group are granted sudo access. To add a user to the sudo group we use the usermod command, which allows you to modify a user’s login information.

The syntax to add a user to a group is:

usermod -aG [group name] [username]

To add a user to the sudo group, run:

usermod -aG sudo username

Step 4 – Test the sudo User Access

After creating the sudo user, you need to test if the sudo access is working. To do that, use the su command to switch over to the new user:

su username

Now, run the whoami command with sudo to confirm the username. If the username has sudo access, then the whoami command will print the word root on the screen.

sudo whoami

If the user does have sudo access, then the output of the whoami command will be root.

Output:

root

Example of me running the command with a newly created sudo user:

[email protected]:~$ sudo whoami
[sudo] password for mruser: <entered password here>
root

Using the sudo Command

Now that you have created a new sudo user, it’s time to check the sudo access. To use the sudo command, prefix any other command with the sudo:

sudo ls -l /root

You will be asked to enter the password the first time you use sudo access. Simply enter the password and continue.

Adding an Existing User to the sudo Group in Ubuntu

So far, we’ve created a new user and added them to the sudo group, but it’s worth pointing out that you can also add existing users to the sudo group.

You can confirm that the username exists using the grep command and /etc/passwd file:

grep '^username' /etc/passwd

Example output:

[email protected]:~$ grep '^mruser' /etc/passwd
mruser:x:1003:1003:,,,:/home/mruser:/bin/bash

Next, add the user to the sudo group with the usermod command:

usermod -aG sudo username

Now, verify sudo access with the whoami command:

sudo whoami

The output should be:

root

Conclusion

Once you’ve created the sudo user in Ubuntu, you can log in and use the sudo command for administrative tasks.

Feel free to contact us or leave a comment if you’ve encountered any issues or have any questions.

0 Shares:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
You May Also Like