How to Add User to Sudoers in Linux: Comprehensive Guide

Add User to Sudoers
Rate this post

Linux is a powerful operating system. It gives full control to users and system administrators. In Linux, some tasks need special permissions. These are called “administrative” or “root-level” tasks. Regular users cannot perform these tasks. Only users with “sudo” access can. This article will explain how to add a user to sudoers. You will also learn some safety tips. Let’s get started.

What is Sudo in Linux?

Sudo stands for “Superuser Do.” It lets a user run commands as the root user. It is safer than logging in as root.

With sudo, users can perform tasks like:

  • Installing software
  • Changing system settings
  • Accessing protected files

Only trusted users should get sudo access.

What is the Sudoers File?

The sudoers file controls who can use sudo. It also defines what commands they can run.

This file is located at:

bashCopyEdit/etc/sudoers

Never edit this file directly using a normal text editor. Always use visudo. It checks for syntax errors before saving. A mistake in this file can lock out all sudo access.

Step-by-Step Guide to Add User to Sudoers

Here’s how you can give sudo access to a user.

Step 1: Open a Terminal

First, open the terminal. You need root access or an existing sudo user for this.

Step 2: Add the User (if not already added)

If the user does not exist, create one. Use this command:

bashCopyEditsudo adduser username

Replace username with the actual name you want to give.

This creates the user and sets a password.

Step 3: Add User to the Sudo Group

Most Linux distributions have a sudo group. Adding a user to this group gives sudo access.

Use this command:

bashCopyEditsudo usermod -aG sudo username

Again, replace username with the new user’s name.

This command does two things:

  • -aG adds the user to the sudo group
  • It keeps existing group memberships intact

After this, the user can use sudo to run commands.

Step 4: Verify the Sudo Access

Log in as the new user. Then try this command:

bashCopyEditsudo whoami

If it returns root, the setup is correct.

If not, double-check the steps or check group memberships with:

bashCopyEditgroups username

You should see sudo in the output.

Alternative: Add User to Sudoers File Manually

Sometimes you may want to add custom rules. In that case, you can edit the sudoers file.

Step 1: Open Sudoers Safely

Use the visudo command:

bashCopyEditsudo visudo

This opens the file in a safe mode. It also prevents syntax errors.

Step 2: Add a Custom Rule

Scroll to the bottom and add this line:

bashCopyEditusername ALL=(ALL:ALL) ALL

Replace username with the actual user’s name.

This gives the user full sudo access.

Press Ctrl+O to save and Ctrl+X to exit (if using nano editor).

Step 3: Save and Exit

After saving, the user will have sudo rights. Test it as shown before.

Security Tips When Giving Sudo Access

Giving sudo access is a serious decision. Follow these best practices:

  1. Only add trusted users.
    Never give sudo access to users you don’t trust.
  2. Use visudo.
    Always edit the sudoers file using visudo.
  3. Limit commands (optional).
    You can limit which commands a user can run with sudo.
  4. Monitor sudo usage.
    Logs are stored in /var/log/auth.log (on Debian) or /var/log/secure (on Red Hat).

How to Remove User from Sudoers

If you want to remove a user’s sudo rights, follow this:

Option 1: Remove from Sudo Group

Use this command:

bashCopyEditsudo deluser username sudo

This works on Debian-based systems like Ubuntu.

For Red Hat-based systems, use:

bashCopyEditsudo gpasswd -d username wheel

The wheel group is the sudo group on Red Hat systems.

Option 2: Remove Custom Rule

If you edited the sudoers file manually, open it again:

bashCopyEditsudo visudo

Find the line with the user and delete it.

Save and exit.

Example Use Case

Imagine you are setting up a new Linux server. You want a teammate to help manage it.

Instead of giving them the root password, you create a user:

bashCopyEditsudo adduser jack

Then you run:

bashCopyEditsudo usermod -aG sudo jack

Now Jack can install software and manage files with:

bashCopyEditsudo apt update  
sudo apt install nginx

This is safer and more secure than using root directly.

Why Add User to Sudoers?

Let’s now repeat the main reason:

To give safe and controlled root access, add user to sudoers instead of sharing the root account.

This way, every sudo action gets logged. You can see who ran what command and when.

Summary

Adding a user to sudoers in Linux is simple but powerful. It lets users manage the system safely.

You can do this by:

  • Adding them to the sudo group
  • Or editing the sudoers file with visudo

Always be careful when giving sudo access. Only trusted users should get it.

To repeat, here are the key commands:

  • sudo adduser username – to create a new user
  • sudo usermod -aG sudo username – to add them to sudo group
  • sudo visudo – to edit the sudoers file safely

Now you know how to add user to sudoers safely and correctly.

Final Thoughts

Linux is a secure system. But it needs careful management. By using sudo, you avoid using the root account directly. This protects your system from mistakes. When you add a user to sudoers, you share access—but in a safe way. With these steps, your Linux system stays both powerful and secure.