In this tutorial, you’ll learn how to create a new user with sudo access on Ubuntu 22.04 without editing your /etc/sudoers file. The sudo command provides a way for system administrators to grant administrator privileges – usually available only to the root user – to normal users.
Prerequisites
- Ubuntu Desktop/server operating system
- Access to a terminal/command line
- sudo privileged user account
Adding a new user on Ubuntu
Use the adduser command to add a new user to your system:
sudo adduser test
Be sure to replace the test with whatever username you want to create. You will be asked to create and verify a password for the user:
Adding user `test' ... Adding new group `test' (1002) ... Adding new user `test' (1002) with group `test' ... Creating home directory `/home/test' ... Copying files from `/etc/skel' ... New password: Retype new password: passwd: password updated successfully
Next, you will be asked to provide information about the new user. You can accept the defaults and leave this information blank:
Changing the user information for test Enter the new value, or press ENTER for the default Full Name []: TEST USER Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Y
Adding the User to the sudo Group
Use the usermod command to add the user to the sudo group:
sudo usermod -aG sudo test
Again, be sure to replace the test with the username you just added. By default on Ubuntu, all members of the sudo group have all sudo privileges.
Testing sudo Access
To test that the new sudo permissions work, first log out from your current user accountant login with test <your username> user account. And if you are using a server then you have to use the su command to switch to the new user account:
su test
As a new user, verify whether your new user account has switched or not. So you can verify the current login user using the whoami command:
whoami
As a new user, verify that you can use sudo by appending sudo to the command you want to run with superuser privileges:
sudo command_to_run
For example, you can update your ubuntu, which is normally accessible only to the root user:
sudo apt update
The first time you use sudo in a session, you will be prompted for that user’s account password. Enter the password to continue:
[sudo] Password for test:
If your user is in the correct group and you entered the password correctly, the command you issue with sudo will run with root privileges.
Conclusion
In this tutorial, you’ll learn how to create a new user with sudo access on Ubuntu 22.04. We created a new user account and added it to the Sudo group to enable sudo access.
If you have any questions please leave them in the comments below