How To Create a Sudo User in Ubuntu (CLI)

0 comments 6.7K views 2 minutes read
Published on: June 12, 2022 | Last updated on: October 11, 2024

The sudo command is designed to allow users to run programs with the security privileges of another user, by default the root user.

In this tutorial, we’ll teach you how to create a new user on an Ubuntu system with sudo access. You may then use this user account to run administrative commands on your Ubuntu server without having to log in as root.

Prerequisites

Make sure you’ve installed Ubuntu correctly, follow this guide if you need to do it still!

Steps to Create a Sudo User

Follow the steps below to create a new user account and give it sudo access. If you want to configure sudo for an existing user, skip to step 3.

1. Log in to your server.

Log in to your system as the root user:

ssh root@server_ip_address

2. Create a new user account.

Create a new user account using the adduser command. Don’t forget to replace username with the username that you want to create:

adduser username

You will be prompted to set and confirm the new user password. Make sure that the password for the new account is as strong as possible.

Adding user `username' ...
Adding new group `username' (1001) ...
Adding new user `username' (1001) with group `username' ...
Creating home directory `/home/username' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully

Once the password has been entered, the command will create a home directory for the user, copy numerous configuration files to the home directory, and prompt you to provide the new user’s details. If you prefer to accept the defaults and leave all of this information blank, simply hit ENTER.

Changing the user information for username
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]

3. Add the new user to the sudo group

By default, on Ubuntu systems, members of the group sudo are granted with sudo access. To add the user you created to the sudo group, use the usermod command:

usermod -aG sudo username

Test the access

Switch to the newly created user:

su - username

Use sudo to run the whoami command:

sudo whoami

If the user has sudo access then the output of the whoami command will be “root”:

root

How to use sudo

To use sudo, simply prefix the command with sudo and space:

sudo ls -l /root

The first time you use sudo in a session, you will be prompted to enter the user password:

[sudo] password for username:

Conclusion

You have learned how to create a user with sudo privileges. You can now log in to your Ubuntu server with this user account and use sudo to run administrative commands.

Feel free to leave a comment if you have any questions.