The sudo
command in Linux provides a mechanism for granting administrator privileges to normal users. This allows non-root users to execute commands with the security privileges of another top-level root, by default, the root user.
This guide explains how to create a new user on a Ubuntu machine and grant it sudo privileges. You can then use this new user account to run administrative commands without logging in as a root user.
Steps to create a sudo user
Follow the below steps to create a new user account and grant it sudo access. If you want to configure sudo access for an existing user, jump to step 3.
Step 1 — Login to your account
Login to your Ubuntu server as a root user:
$ ssh root@your_server_ip
Step 2 — Create a new user account
Use the adduser
command to create a new user account. Be sure to replace username
with the user name that you want to create:
$ sudo adduser username
You will be prompted to enter and confirm the new user password. Enter a strong password for the new user:
Adding user `username' ...
Adding a 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
After setting up the password, the command will create the home directory for the user, copy some configuration files into it, and again prompt you to set the new user's information.
It is alright to choose not to enter anything and accept the defaults by leaving all these blank:
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] Y
Enter Y
to accept the information.
Step 3 — Add the new user to the sudo group
On Ubuntu and other Linux systems, the members of the sudo group are granted permission to use the sudo
command. To add the newly created user to the sudo group, issue the following usermod
command:
$ sudo usermod -aG sudo username
Test the sudo user access
Switch to the newly created user:
$ su - username
Run the whoami
command by prepending sudo
to the command:
$ sudo whoami
If the newly created user has sudo access, then the output of the whoami
command should be root
.
How to use sudo
To use sudo privilege, prefix any command with sudo
and space. Let us list the contents of the /root
directory, which is something only the root user can do:
$ sudo ls -la /root
The first time you use sudo in a session, you will be prompted to enter the password. Enter the password you used to create the user and not the root password to proceed:
[sudo] password for username:
Conclusion
In this article, you have learned how to create a user account with sudo access on a Ubuntu machine. You can now log in to the Ubuntu server using the newly created user account and use sudo to run administrative commands.
The same commands can add and delete users on any Ubuntu-based distribution, including Debian, Kubuntu, and Linux Mint.
Further Reading
You may be interested in other Ubuntu articles:
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.