How to Add a User to the Sudoers File in Linux

October 24, 2025 / Servers, Hosting & Email

To give a user admin privileges in Linux, you must allow them to run commands using sudo. This can be done by adding the user to an administrative group or by configuring access in the sudoers file.

Add a User to the Sudo/Wheel Group (Recommended)

Most Linux distributions grant sudo access through a default administrative group.

  • On Ubuntu and Debian, the group name is sudo.
  • On CentOS, RHEL and Fedora, the group name is wheel.
  1. To add the user, run:
    sudo usermod -aG sudo <username>
  2. For CentOS/RHEL-based systems:
    sudo usermod -aG wheel <username>

    After running the command, ask the user to log out and log back in for changes to take effect.

  3. To confirm the group membership:
    groups <username>

Grant Access Using the Sudoers File

If needed, you can manually configure sudo privileges inside the sudoers file.

Important: Always use visudo to prevent misconfiguration.

  1. Open the sudoers file:
    sudo visudo
  2. Add this line at the end:
    <username> ALL=(ALL) ALL
  3. Save and exit to enable sudo access.

Configure Individual Rules in /etc/sudoers.d

A more organised and secure method is to create a separate configuration file for the user.

  1. Open a dedicated sudoers rule file:
    sudo visudo -f /etc/sudoers.d/<username>
  2. Add:
    <username> ALL=(ALL) ALL
  3. Save the file to apply the configuration.

Optional: Enable Passwordless Sudo

  1. Only enable this if required for automation or scripts, as it reduces security protections:
    <username> ALL=(ALL) NOPASSWD:ALL

Test Sudo Access

  1. Switch to the user:
    su - <username>
  2. Run a test command:
    sudo whoami
  3. Expected output:
    root

    If the command succeeds, the user now has sudo privileges.

Conclusion

This way, you can add a user to the sudoers file in Linux to grant a user administrative tasks on the system. The recommended approach is to add the user to the sudo or wheel group, as it’s easier to manage and maintain. If you need to edit the sudoers file directly, always use visudo to avoid syntax errors that could impact system security.

Managing Linux users and permissions?
Get full control over your server with a Linux VPS solution.

Need to give full access to a user in cPanel? Check out our guide on How to grant all privileges to the user in cPanel

Spread the love