How To Create a New Sudo-enabled User on Ubuntu
When managing a server, you’ll sometimes want to allow users to execute commands as “root,” the administrator-level user. The sudo
command provides system administrators with a way to grant administrator privileges — ordinarily only available to the root user — to normal users.
In this Demo, you’ll learn how to create a new user with sudo
access on Ubuntu 20.04 without having to modify your server’s /etc/sudoers
file.
Step 1: Login to your server — SSH into your server as the root user:
ssh root@your_server_ip_address
Step 2: Add a New User to the System - Use the adduser
command to add a new user to your system:
adduser megha
It’ll show you the below output.
adding user ‘megha' ...
adding new group “megha' (1001) ...
adding new user ~megha' (1001) with group “megha' ...
Creating home directory ~/home/megha' ...
copying files from ~/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Enter the new value, or press ENTER for the default
Full Name []: Megha
Room Number []:
Work Phone []:
Taoyuan led
Other []:
Is the information correct? [Y/n] Y
Step 3: Add the User to the sudo Group — Use the usermod
command to add the user to the sudo group.
usermod -aG sudo megha
Step 4: Test sudo
Access — To test that the new sudo
permissions are working, first use the su
command to switch to the new user account.
su - megha
As the new user, verify that you can use sudo
by prepending sudo
to the command that you want to run with superuser privileges
sudo command_to_run
For example, you can list the contents of the /root
directory, which is normally only accessible to the root user
sudo ls -la /root
The first time you use sudo
in a session, you will be prompted for the password of that user’s account. Enter the password to proceed:
Output:
[sudo] password for megha:
Note: This is not asking for the root password! Enter the password of the sudo-enabled user you just created.
If your user is in the proper group and you entered the password correctly, the command that you issued with sudo
will run with root privileges.
Woohoo! we created a new user account and added it to the sudo group to enable
sudo
access.