sudo command in Linux provides a mechanism for granting administrator privileges to normal users. This allows non-root users to execute commands with 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.
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.
Login to your Ubuntu server as a root user:
$ ssh root@your_server_ip
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. If it is absolutely fine if 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
Y to accept the information.
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
$ sudo usermod -aG sudo username
Switch to the newly created user:
$ su - username
whoami command by prepending
sudo to the command:
$ sudo whoami
Is the newly created user has sudo access then the output of the
whoami command should be
To use sudo privilege, simply prefix any command with
sudo and space. Let us list the contents of
/root directory, which is something only root can do:
$ sudo ls -la /root
The first time you use sudo in a session, you will be prompted to enter the password of the user account. Enter the password you used to create the user and not the root password to proceed:
[sudo] password for username:
In this article, you have learned how to create a user account with sudo access on a Ubuntu machine. You can now login to the Ubuntu server using the newly created user account and use sudo to run administrative commands.
The same commands can be used to add and delete users on any Ubuntu-based distribution, including Debian, Kubuntu, and Linux Mint.
You may be interested in other Ubuntu articles:
👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5) .
Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.