How to Create a Sudo User in Ubuntu Linux

In this tutorial, we will see how to create a new user in Ubuntu 20.04 and how to grant sudo access to the user. As you may aware, any users who want to run commands that require root privileges need to either switch to the root account and submit the command or they should be part of the sudo group.

[ You might also like: How to Disable SSH Login to Specific User in Linux ]

Sudo allows you to run commands that require elevated privileges without switching into the root account. Also with sudo, we can control which user can have access to which set of commands.

Create a User in Ubuntu

we will create a new user named ubuntu20 and grant him sudo privilege. To create the user run the following command.

$ sudo adduser ubuntu20
Create New User in Ubuntu
Create New User in Ubuntu

Try switching as an ubuntu20 user and try running the apt command, it will fail since it requires root privilege.

$ su - ubuntu20
$ apt-get update
$ exit
User Permission Denied Error
User Permission Denied Error

Add User to Sudo Group

Now you need to add ubuntu20 to the sudo group. Any user that is part of the sudo group is granted sudo privilege.

$ usermod -aG sudo ubuntu20

To verify if a user is added to the sudo group run the id command.

$ id ubuntu20
Check User Group
Check User Group

Now you can run any command that the root user can run. Let’s try running the same apt command with sudo.

$ su - ubuntu20
$ sudo apt-get update
Run Command with Sudo Privileges
Run Command with Sudo Privileges

Change Default Sudo Password Timeout

Every time when you run sudo you will be prompted for a password. But an important thing to note is sudo will cache the password for some time once you run any command.

So if you try to run any other commands immediately you won’t be asked to enter the password. This feature is controlled by the below parameter. By default, it is set to 15 mins.

$ sudo visudo

The default is 15. You can set this parameter to 0 to always ask for a password.
e until the system is rebooted.

Defaults        env_reset,timestamp_timeout=15
Sudo Timeout Parameter
Sudo Timeout Parameter

You can also disable the password for the sudo command. To do so, you have to use the visudo command which will open the sudo configuration file.

$ sudo visudo

and add the following line as shown in the image.

ubuntu20  ALL=(ALL)       NOPASSWD: ALL
Disable Sudo Password
Disable Sudo Password

That’s it for this article. Sudo is very important in securing your Linux system. Knowing how to work with sudo is mandatory for everyone who is working with Linux OS.

Got something to say? Join the discussion.