How to Add a User to Sudoers on Ubuntu

Sudo means Super User do! This means, that when we use any Command attached with sudo, it will get you Superuser Privileges which include Installing and Removing Software, changing any Configuration in the System, etc.

If you use Debian 11, your User will not be added to the Sudoers file by default. If you are willing to add a new user to your system, this will be helpful to you too. We will discuss 3 different methods by which you can elevate root privileges in Ubuntu. So let’s start with the easiest one.

1. Adding our user to Sudo Group

This Method is the Easiest method to get your Local user Sudo Privileges. There is a Sudo Group and each Member of that Group has a sudo Privilege to perform any given task. Members of this group can execute any command using the sudo command and Authenticate themselves using a Password.

If any of your current users don’t have root access and you want to add them to the Sudo group, you will have to execute commands as root. Use the following commands to get into root:

 su 

su

Make sure that you change the username with your user that you want to allocate root privileges.

 usermod -aG sudo username 

usermod

That’s it, now your User has root privileges which are enough to perform almost every task.

To ensure that the user has root privileges, use the following commands:

 sudo whoami 

whoami

If you get output saying root, congratulations, you’ve successfully added your user to the root

2. Adding our user to Sudoers file

All the users’ and groups’ privileges are defined in the /etc/sudoers file. Adding our current User to this file will allow us to grant that user-customized privileges and avail us to configure custom security policies.

The reason why we always use visudo to edit sudoers files is that it checks the whole Syntax for any errors when you save it. If there will be any errors, your changes will not be saved.

For this tutorial, we will use nano as a text editor to change configurations.

Use the following commands:

 sudo visudo 

sudo visudo

You will be given a text file in which we are going to make changes.

Add the following command at the end of the file:

 username ALL=(ALL:ALL) ALL 

username ALL=(ALL_ALL) ALL

Remember to change your Username, in my case it’s geniusgeeks.

Use CTRL + O to save this configuration file and CRTL + X to close nano (texteditor).

Customizing privileges for granting permissions without Passwords

It feels Irritating when we are asked for a password every time while Performing any Tasks. We will show you how you can Customize your privileges by which you will be never asked for any Password.

Use given Commands to allow your user to run any sudo Commands without being asked Password:

 username ALL=(ALL:ALL) NOPASSWD:ALL 

username ALL=(ALL_ALL) NOPASSWD_ALL

To save the changes use CTRL + O and to exit this configuration file, use CTRL + X.

The reason why you are asked Password before making any changes:

Using your System continuously with root privileges makes your System more likely to run into some malicious stuff. Because when you are using the System at root the whole time, each software can make changes without being known.

When you use sudo, it will ask for a Password before making any change to the System and this is the reason why you are advised to keep your Password strong.

3. Making a new file in the sudoers.d directory

We can also grant root privileges to our users by creating a new configuration file in the/etc/sudoers.d directory. Use the following command to move further:

 echo “username ALL=(ALL:ALL) ALL” > /etc/sudoers.d/username 

(Don’t forget to change the username with your username, mine is geniusgeeks here)

username with file

Related Posts:

Which Method made more Sense to you?

All the 3 methods which we showed to you will work perfectly fine on almost every Linux desktop. There is a reason why there are 3 separate techniques. The first is for those who just want to get their job done without getting deep into configuration.

While 2nd and 3rd are just for the advanced users who want to take charge of their System.

Sagar Sharma
Sagar Sharma

Sagar always uses Linux to its core and loves to write the technical side of system administration! While he's not writing for GeniusGeeks, you can find him writing for core linux blogs like IT'SFOSS.com and LinuxHandBook.com

Articles: 56