3 Ways to Give Sudo Permission to a User in Linux

Hello there, In this tutorial, We will guide you about granting Sudo privileges to a user in Linux. We are going to cover three different ways to add users to the sudoers file to grant the sudo privileges.

Here are the three ways:

  • Using Visudo Command
  • Using Usermod Command
  • By editing /etc/sudoers in nano
Adding a user in the sudoers file in Linux for superuser access

Introduction

As we know, In Linux-based operating systems we use the Sudo command to execute Linux commands with superuser privileges. If We talk about superuser privileges then we would say, by default only the root user has such privileges. But It is possible to grant it (root privileges) to other users also.

In order to perform some tasks like changing configuration settings or accessing some sort of files, the user may need to have superuser privileges. In such a situation, we can allow users to run Sudo commands by adding them to Sudoers files.

So basically, in this guide, you will learn how to add users to the Sudoers file in order to allow them to run the Sudo commands.

Adding User to The Sudoers File Using Visudo Command

The visudo command is used to edit the “etc/sudoers” which is one of the configuration files in the Linux OS. In simple words, the sudoers file is used to control who is allowed to run the Sudo command.

In order to use the visudo command we must already have superuser privileges. Or we can use the “root” user.

Follow the steps below to give the user sudo access in Linux using visudo command:

Step 1: Edit the sudoers file

In order to edit the sudoers file using visudo, run the following command-

visudo

Or if you don’t have logged in as root.

sudo visudo

When you run this command then the sudoers file will get open in a text editor.

Visudo command in Linux

Step 2: Add the user to the file

Now in the second step, you will have to add the user you want to allow the sudo access in the sudoers file.

username ALL=(ALL) ALL

Add this line in the file but replace “username” with the actual username you want to grant the sudo access.

Let’s suppose, you want to allow sudo access to the user “john” then you will have to add the following code-

john ALL=(ALL) ALL

Step 3: Save and Exit the Editor

Once you save the file then the specified user will get the sudo privileges and will be able to run sudo command with the superuser privileges or root privileges.

The Usermod command for giving a user root privileges

We can use usermod command also in order to add a user to the sudoers file to give superuser rights. To use this method you need to have a user account that does not have root privileges so that you can give it the privileges.

To use this method you need to follow these given steps:

Step 1: Open your Linux terminal and log in as the root user.

The root user or a user with Sudo rights can only execute usermod command in order to add a user to the sudoers file.

It’s Okay if you are not logged in as root but you need to already have sudo rights if you want to add a user to the sudoers files.

You can log in as root by running the su command and entering the root password.

Step 2: Now run the usermod command to add a user to the “sudo” group.

Here is the syntax:

usermod -aG sudo username

Or use sudo if you are not logged in as root-

sudo usermod -aG sudo username

Note: Replace the “username” with the actual username that you have.

Step 3: Log out and log in back

As you already have logged in as “root” or a different user, now log out and log in again but using the user name you added to the sudo group so that you can test.

Note: In some Linux distributions the “sudo” group is known as “adm”. So, In order to give users superuser rights you will have to add them to a perfect group.

Edit etc/sudoers Using Text Editor

We can edit “etc/sudoers” file in a text editor and add a user to it to give sudo access. In order to do so, you need to have a text editor installed on your Linux machine.

Nowadays, we get pre-installed text editors in almost all Linux distributions.

You can use any text editor like nano or vi to edit the sudoers file in your Linux-based operating system.

Follow the steps to edit the “etc/sudoers” file in the nano text editor:

Step 1: Open the terminal and log in as root.

Again, In order to edit the sudoers file you need to have root access or superuser privileges.

You can log in as root using the following command-

su

Or

su -

After running the su- or su command you will have to enter the root password.

Step 2: Edit the sudoers file in nano

Nano is nothing but a terminal-based text editor which can be accessed by using the nano command followed by the file path (or the file name).

Run the following command to edit your sudoers file in order to add a user for sudo access-

nano /etc/sudoers

If you are not logged in as the root user then you will have to use “sudo” for the root privileges-

sudo nano /etc/sudoers
Editing sudoers files in nano

Step 2: Now scroll down to the “# User privilege specification” section.

You can scroll down to the “# User privilege specification” using the down arrow key or you can directly jump to the section using “CTRL+W” to find the section.

Step 3: Now add a new line in the user privilege section.

Now you will have to add the following line to provide “sudo” access to a user-

username ALL=(ALL:ALL) ALL

Here, Replace the “username” with the actual user name.

Step 4: Save and exit the editor

In order to save the file, you will have to press the “CTRL+X” button from your keyboard and then press “Y” and hit enter now the sudoers file will be saved and the user you have added will get the “sudo” access.

Conclusion

In this tutorial, we explained how to give sudo access to a Linux user by adding them to the sudoers file. We have shown three methods to add a user to sudo group, you can use any method as it will not make any difference.

Leave a Comment